EARs and WARs

Ears and wars are created just as they were in J2EE 1.4/EJB 2.1. In the application.xml file, JBoss still requires that the filename suffix be .ejb3.

<application>
   <display-name>EJB3 Ear tutorial</display-name>

   <module>
      <web>
         <web-uri>tutorial.war</web-uri>
         <context-root>/tutorial</context-root>
      </web>
   </module>
   <module>
      <ejb>tutorial.ejb3</ejb>
   </module>
</application>

Lookup of EJBs

Unfortunately, the J2EE specification is not official yet on how EJB3s references should be declared in XML or annotated within servlet classes. So, we cannot update Tomcat yet for this new mapping. JBoss/Tomcat integration may lag behind for new EJB3 refs. So for now, you must lookup the EJB via its global JNDI name. This is not compliant, but if you abstract out enough you'll be fine. To give you a preview of how injection will work inside servlets/jsps, we've simulated the code. Take a look at CalculatorActionServlet.java. You can use this pattern until the XML Schema is updated for J2EE 1.5 for injection, or until Tomcat supports EJB injection annotations.


   public void init() throws ServletException
   {
      super.init();
      try
      {
         InitialContext ctx = new InitialContext();

         // J2EE 1.5 has not yet defined exact XML <ejb-ref> syntax for EJB3
         CalculatorLocal calculator = (CalculatorLocal) ctx.lookup(CalculatorLocal.class.getName());
         setCalculator(calculator);
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);
      }

   }

   private CalculatorLocal calculator;

   /**
    * The @EJB annotation is similated.  Tomcat does not yet understand injection annotations.
    * @param calculator
    */
   @EJB(name = "calculator")
   public void setCalculator(CalculatorLocal calculator)
   {
      this.calculator = calculator;
   }

Looking up EntityManagers from within an EAR/WAR

Currently, EntityManagers are scoped and only available for injection inside EJBs defined in a deployment JAR. This means that there you can only access Entity beans from within Session/MDBs and thse Session/MDBs must be in the same JAR as the entities they are accessing. The specification has not yet defined any concept of identity. It is currently being discussed within the specification. JBoss EJB3 will have a concept of EntityManager identity and lookup whether it is eventually defined int he specification or not.

Building and Running

To build and run the example, make sure you have ejb3.deployer installed in JBoss 4.0.x and have JBoss running. See the reference manual on how to install EJB 3.0.
Unix:    $ export JBOSS_HOME=<where your jboss 4.0 distribution is>
Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is>
$ ant

After building, you can then goto calculator.jsp