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 4 of 6

 public class CarEjbTest extends TestCase {

private Context context; private Car mockCar; public final static String CAR_HOME = "ejb/car/Car";

protected void setUp() throws Exception { super.setUp(); context = new InitialContext();

/* * You have to write some file reader that reads * the XML representing the car object */ String xml = SomeFileReader.getCarXML(); mockCar = (Car) (new XStrem()).fromXML(xml); }

protected void tearDown() throws Exception { super.tearDown(); } public void testCar() throws Exception { try { /* * Get the reference to home object */ Object objref = context.lookup(CAR_HOME);

CarReservationHome carReservationHome = (CarReservationHome) PortableRemoteObject.narrow(objref, CarReservationHome.class);

CarReservation carReservation = carReservationHome.create(); boolean ok = reservation.reserveCar(mockCar); assertTrue(ok); } catch (NamingException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } }


Interestingly, the above test can be run both inside and outside an EJB container, with the following alternatives:

  1. Test calls EJB components that are deployed and run as part of an embedded OpenEJB container
  2. Test makes remote calls to EJB components deployed to an EJB container
  3. Test runs in an EJB container and makes local calls to EJB components

Install OpenEJB

OpenEJB can be installed as either a standalone server or an embedded container:

  1. Download OpenEJB.
  2. Unzip it to a folder, say to c:\java\openejb.
  3. Create a new configuration from scratch:
    1. Remove the old configuration move conf conf.bak.
    2. Create an empty folder called conf, mkdir conf.
    3. In the recently created conf folder, create the following files:
      1. openejb.conf
      2. logging.conf
      3. myapp.cmp.global-database.xml
      4. myapp.cmp.local-database.xml
      5. myapp.cmp.or-mapping.xml
    4. Edit openejb.conf and make sure that it correctly references myapp.cmp.global-database.xml, myapp.cmp.local-database.xml, and myapp.cmp.or-mapping.xml. Note that the path is calculated from c:\java\openejb. For example, the path to myapp.cmp.global-database.xml would be conf/myapp.cmp.global-database.xml in openejb.conf. In all the above files, you must define various container-managed persistence and bean-managed persistence containers. Furthermore, you must define connectors for various datasources.
  4. Add your jar file to the OpenEJB configuration. In openejb.conf, add a reference to your jar file. For example: <Deployments jar="C:\dev\myapp\deploy\myapp-ejb.jar"/>.
  5. Make the necessary modifications in each of the above files.

Deploy to OpenEJB

Deploy your application jar file only once to OpenEJB. OpenEJB will add a file called openejb-jar.xml to your jar file. The openejb-jar.xml file contains configuration information specific to OpenEJB. If you add or remove EJB beans, you must deploy again.

Here is the syntax for deploying a jar file:

 openejb deploy -a -c C:\dev\myapp\deploy\myapp-ejb.jar


Now, you need to back up openejb-jar.xml. You will find it under the application jar file's META-INF folder. Simply go to the command prompt and run the following to extract the files:

  • Print
  • Feedback

Resources