Java EE 7 SDK |
This sample application demonstrates how to use the embeddable EJB container.
The EJB 3.2 Embeddable API Sample Application demonstrates how to use the embeddable EJB container defined in the Enterprise JavaBeans 3.2 specification. You can launch the embeddable EJB container from your code to run tests on EJBs outside of an application server environment.
The sample application consists of two parts: an EJB module (this project) and the test client. The rest of this document describes the EJB module.
The EJB module contains a stateless session bean with a no-interface view and a Java entity object. The client code creates the embeddable EJB container and deploys the EJB module, which consists of the following files:
SimpleEntity.java
is a Java entity object. The persistence.xml
file defines a persistence unit which uses jdbc/__default
as
its data source and the java2db
feature of GlassFish to create tables.
SimpleEjb.java
is an EJB that uses the Java Persistence API to
store and retrieve entities from the data source.
The stateless session bean has a no-interface view with the following two business methods:
public int verify()
public void insert(int num)
The implementation of the stateless session bean is the following:
@Stateless public class SimpleEjb { @PersistenceContext(unitName="embedded_test") EntityManager em; @PermitAll public int verify() { String result = null; Query q = em.createNamedQuery("SimpleEntity.findAll"); Collection entities = q.getResultList(); int s = entities.size(); for (Object o : entities) { SimpleEntity se = (SimpleEntity)o; System.out.println("Found: " + se.getName()); } return s; } @PermitAll public void insert(int num) { for (int i = 1; i <= num; i++) { System.out.println("Inserting # " + i); SimpleEntity e = new SimpleEntity(i); em.persist(e); } } }
The sample application only requires the persistence deployment descriptor.
The global JNDI name of the stateless session bean is
java:global/ejb-embedded/SimpleEjb
, where the EJB module name
ejb-embedded
corresponds to the unqualified name of the .jar
file when building the application.
The EJB in the sample application demonstrates the following key features:
@PersistenceContext
@Stateless
app_dir
is the sample application base
directory: samples_install_dir/javaee7/ejb/ejb-embedded/ejb-embedded-server
.
Change directory to app_dir.
mvn clean install
samples_install_dir/javaee7/ejb/ejb-embedded/ejb-embedded-client
.If you have problems when running the application, refer to the troubleshooting document.
Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.