Reference EJB3 Beans from EJB2 Beans

Version 4

    It is possible to reference an EJB3 bean from an EJB2 bean just like an EJB2 bean would reference any other EJB2 bean as long as the EJB3 bean is deployed with an EJB 2 client adaptor. Please note that EJB2 and EJB3 beans cannot reside on the same jar file and hence, the EJB2 beans must reside in a different jar to the EJB3 beans.

     

    The following example explains how to reference an EJB3 SLSB bean locally from an EJB2 SLSB bean. Producing similar examples that use remote references rather than a local ones, or using SFSBs rather than SLSBs, is trivial once the example below is understood and hence they're not included below:

     

    Step 1. Create a business and a home interface for the EJB3 bean:

     

    Business interface

    public interface SystemTimeLocal
    {
       long getCurrentTimeMillis();
    }
    

     

    Home interface

    import javax.ejb.CreateException;
    import javax.ejb.EJBLocalHome;
    ...
    public interface SystemTimeLocalHome extends EJBLocalHome
    {
       public SystemTimeLocal create() throws CreateException;
    }
    

     

    Step 2. Add @LocalHome and @Local to the EJB3 bean:

    import javax.ejb.Local;
    import javax.ejb.LocalHome;
    import javax.ejb.Stateless;
    ...
    @Stateless
    @Local(SystemTimeLocal.class)
    @LocalHome(SystemTimeLocalHome.class)
    public class SystemTimeBean implements SystemTimeLocal
    {
       public long getCurrentTimeMillis()
       {
          return System.currentTimeMillis();
       }
    }
    

     

    Step 3. Reference EJB3 bean from EJB2 descriptors bearing in mind that by default, EJB3 local home interfaces are bound to <bean-name>/localHome JNDI name. You can define your JNDI name via @LocalBinding annotation:

     

    META-INF/ejb-jar.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
              http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
             version="2.1">
      <enterprise-beans>
        <session>
          <ejb-name>TimeTellerEjb</ejb-name>
          <home>ejb2.interop.slsb.TimeTellerHome</home>
          <remote>ejb2.interop.slsb.TimeTeller</remote>
          <ejb-class>ejb2.interop.slsb.TimeTellerBean</ejb-class>
          <session-type>Stateless</session-type>
          <transaction-type>Container</transaction-type>
          <ejb-local-ref>
            <ejb-ref-name>ref/SystemTimeEjb</ejb-ref-name>
            <ejb-ref-type>Session</ejb-ref-type>
            <local-home>ejb3.interop.slsb.SystemTimeLocalHome</local-home>
            <local>ejb3.interop.slsb.SystemTimeLocal</local>
          </ejb-local-ref>
        </session>
      </enterprise-beans>  
    </ejb-jar>
    

     

    META-INF/jboss.xml

    <?xml version="1.0"?>
    <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN"
            "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">        
    <jboss>
      <enterprise-beans>
        <session>
          <ejb-name>TimeTellerEjb</ejb-name>
          <jndi-name>ejb/TimeTellerEjb</jndi-name>
          <ejb-local-ref>
            <ejb-ref-name>ref/SystemTimeEjb</ejb-ref-name>
            <local-jndi-name>SystemTimeBean/localHome</local-jndi-name>
          </ejb-local-ref>
        </session>
      </enterprise-beans>
    </jboss>

     

    Step 4. Finally, the EJB2 bean looks up the reference defined:

    Context ctx = new InitialContext();
    SystemTimeLocalHome home = (SystemTimeLocalHome)
       ctx.lookup("java:comp/env/ref/SystemTimeEjb");
    SystemTimeLocal local = home.create();
    Date date = new Date(local.getCurrentTimeMillis());
    log.info("Current time is: "+ date  + ", caller: " + caller);