3.2.  Referencing EJB3 Session Beans from non-EJB3 Beans

3.2.  Referencing EJB3 Session Beans from non-EJB3 Beans

JBoss Enterprise Application Platform 5 will fully support the entire Java 5 Enterprise Edition specification. In the meantime JBoss Enterprise Application Platform 4.2.0.CP01 implements EJB3 functionality by way of an EJB MBean container running as a plugin in the JBoss Application Server. This has certain implications for application development.

The EJB3 plugin injects references to an EntityManager and @EJB references from one EJB object to another. However this support is limited to the EJB3 MBean and the JAR files it manages. Any JAR files which are loaded from a WAR (such as Servlets, JSF backing beans, and so forth) do not undergo this processing. The Java 5 Enterprise Edition standard specifies that a Servlet can reference a Session Bean through an @EJB annotated reference, however this is not implemented in JBoss Enterprise Application Platform 4.2.0.CP01.

In order to access an EJB3 Session Bean from a Servlet or JSF Backing Bean you will need to do one of two things:

  1. Without Seam - JNDI Lookup

    Without utilizing the Seam framework that is part of JBoss Enterprise Application Platform you will need to use an explicit JNDI lookup to access the EJB3 Session Bean. You can see an example of this being done in the TodoBean.java file in the jsfejb3 example application, described in Chapter 5, Sample JSF-EJB3 Application.

    private TodoDaoInt getDao () {
    	try {
    	InitialContext ctx = new InitialContext();
    	return (TodoDaoInt) ctx.lookup("jsfejb3/TodoDao/local");
    	} catch (Exception e) {
    	e.printStackTrace();
    	throw new RuntimeException("couldn't lookup Dao", e);
    	}
    	}
    

    ctx.lookup("jsfejb3/TodoDao/local"); is the method used to reference the EJB3 Session Bean. The form is: AppName/SessionBeanName/local.

  2. With Seam - Leave it to the Seam Framework

    When you are using the Seam Framework you don't need to worry about this. Because the Seam framework manages the interaction of Beans anyway, it already automates this type of interaction.

    Refer to Chapter 6, Using Seam for a more detailed explanation of achieving this using the Seam framework.