Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Build, deploy, and test EJB components in just a few seconds

OpenEJB simplifies Enterprise JavaBeans development

  • Print
  • Feedback

Page 5 of 6

 jar xvf C:\dev\myapp\deploy\myapp-ejb.jar


Note: You must ensure that openejb-jar.xml is added to the application jar file every time you make a new build. For example, if you use Ant, make sure Ant always includes openejb-jar.xml.

Use the same naming convention in the application, the test, and openejb-jar.xml. Doing so ensures that the same application and the same test will run unmodified in both OpenEJB and your production application server.

For example, in the above test, we used the following convention:

 public final static String CAR_HOME = "ejb/car/Car"; 


Therefore, we must make sure that the configuration in openejb-jar.xml looks like this:

 <ejb-deployment ejb-name="TheCar" deployment-id="ejb/car/Car" container-id="Default Stateless Container">
</ejb-deployment> 


Run the test with embedded OpenEJB

Here comes the magic. To run your tests, you just need to add the following command line arguments to your JUnit tests:

 -Djava.naming.factory.initial=org.openejb.client.LocalInitialContextFactory 
-Djava.naming.provider.url=ormi://localhost:4201 
-Dopenejb.home=c:\java\openejb\openejb-0.9.2 
-Dopenejb.configuration=c:\java\openejb\openejb-0.9.2\conf\openejb.conf 
-Dopenejb.loader=embed 
-Dopenejb.localcopy=true 


If you have configured OpenEJB with a username and password, the following arguments must also be added:

 -Djava.naming.security.principal=adminusername
-Djava.naming.security.credentials=password 


Let's walk through the above arguments.

The following two arguments tell the JVM to use OpenEJB as the JNDI provider:

 -Djava.naming.factory.initial=org.openejb.client.LocalInitialContextFactory 
-Djava.naming.provider.url=ormi://localhost:4201 


In fact, the first time your application asks for a JNDI resource, LocalInitialContextFactory initializes and starts an instance of OpenEJB. OpenEJB then deploys and starts the jar files specified in openejb.conf.

The following two arguments give the location of both OpenEJB's home and the configuration file:

 -Dopenejb.home=c:\java\openejb\openejb-0.9.2 
-Dopenejb.configuration=c:\java\openejb\openejb-0.9.2\conf\openejb.conf 


As you can see, only the absolute path is used; otherwise, OpenEJB does not initialize properly.

Finally, the following lines ensure OpenEJB starts in the embedded mode:

 -Dopenejb.loader=embed 
-Dopenejb.localcopy=true 


Here is how CarEjbTest runs with embedded OpenEJB in Eclipse:

Figure 4. VM arguments are provided to a JUnit test that starts OpenEJB in embedded mode. Click on thumbnail to view full-sized image.

Debug the test with embedded OpenEJB

Out of the box, it is difficult to use an IDE debugger with an EJB container. However, with OpenEJB in embedded mode, things are simple. Simply choose "debug" instead of "run" with the above VM arguments. Remember that OpenEJB, EJB components, and JUnit tests all start in the same JVM and the debugger sees them as one big Java application.

Run the test on the remote production server

About 90 percent of the time, you will develop and test using an embedded OpenEJB container, since doing so is fast and easy. When you feel that a component is ready for integration testing, the application is deployed to the application server chosen for production. To run the tests on the remote production server, see your EJB container's documentation. With Oracle OC4J, running the tests is simple:

  • Print
  • Feedback

Resources