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 and the test client (this project). The rest of this document describes the test client.
The test client uses an embeddable EJB container to test the EJB in an environment that supports the same basic services that would be available in a Java EE runtime: injection, access to a component environment, container-managed transactions, the Java Persistence API, etc.
The client uses the following methods defined in the EJBContainer
class
in the javax.ejb.embeddable
package to create the embeddable EJB container
and to look up the stateless session bean from the EJB module in the JNDI:
public static EJBContainer createEJBContainer(Map<?,?> properties)
public Context getContext()
public void close()
The EJB module (ejb-embedded.jar
) is located in the JVM classpath.
The test client implementation is the following:
public class TestClient { private static String appName; public static void main(String[] s) throws Exception { TestClient t = new TestClient(); t.test(); } private void test() throws Exception { EJBContainer c = null; try { c = EJBContainer.createEJBContainer(); Context ic = c.getContext(); System.out.println("Looking up EJB..."); SimpleEjb ejb = (SimpleEjb) ic.lookup("java:global/ejb-embedded/SimpleEjb"); System.out.println("Invoking EJB..."); System.out.println("Inserting entities..."); ejb.insert(5); System.out.println("JPA count returned: " + ejb.verify()); System.out.println("Done calling EJB"); } finally { if (c != null) { System.out.println("Closing container ..."); c.close(); System.out.println("Done Closing container"); } } System.out.println("..........FINISHED Embedded test"); } }
The test client demonstrates the following key features:
EJBContainer.createEJBContainer()
EJBContainer.getContext()
EJBContainer.close()
Perform the following steps to build, deploy, and run the test client:
asadmin start-database
ejb-embedded-server
project as a dependency.
If you have not yet installed ejb-embedded-server
and its parent, please run the following commands:
mvn install
under samples_install_dir/javaee7/ejb/ejb-embedded/ejb-embedded-server
. This command will install ejb-embedded-server
project into your local repository.mvn -N install
under samples_install_dir/javaee7/ejb/ejb-embedded
, samples_install_dir/javaee7/ejb
and samples_install_dir/javaee7
. This command will install ejb-embedded-server
project's parent into local repository.
mvn install
under samples_install_dir/javaee7
, but this command will install all the samples.app_dir
is the sample application base
directory: samples_install_dir/javaee7/ejb/ejb-embedded/ejb-embedded-client
.
Change directory to app_dir.
exec:exec
will start up a JVM to run TestClient
class. At this time, the artifact(ejb-embedded.jar
) of ejb-embedded-server
project will be located in the JVM classpath, and then deployed to the embeddable EJB container.
mvn clean verify exec:exec
If you have problems when running the application, refer to the troubleshooting document.
Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.