6 Replies Latest reply on Mar 30, 2006 3:27 AM by oglueck

    EJB3 session bean with EJB2 session dependency

    javasutra

      Hi all,

      I have an EJB3 stateless bean that needs to reference an EJB2 session bean that is part of a third party application deployed in a separate jar. The catch is that the lookup of the EJB2 bean is done inside a vendor supplied POJO helper that my bean uses.

      I've tried several approaches with the usual result being a NamingException complaining about env not being bound or an NPE in org.jnp.interfaces.NamingContext.resolveLink. I think the problem lies with separate ENCs for the EJB3 beans and the EJB2 beans but I'm obviously not that swift with JNDI.

      The vendor's jboss.xml looks essentially like this:

      ...
       <session>
       <ejb-name>FooEJB</ejb-name>
       <local-jndi-name>ejb/local/FooEJB</local-jndi-name>
       <jndi-name>ejb/remote/FooEJB</jndi-name>
       </session>
      ...


      The vendor recommends creating a local-ref in my DD like this:
      (I tried this, BTW and got the env not bound exception)

      <ejb-local-ref>
       <ejb-ref-name>ejb/FooEJB</ejb-ref-name>
       <ejb-ref-type>Session</ejb-ref-type>
       <local-home>
       com.foo.ejb.FooLocalHome
       </local-home>
       <local>com.foo.ejb.FooLocal</local>
       <ejb-link>foo.jar#FooEJB</ejb-link>
      </ejb-local-ref>
      


      I've seen a lot of information on getting to EJB3 beans from EJB2 beans but not the other way around. I would just like to find out if this can be done and the general approach. Thanks!

      I'm running 4.0.3RC2.

        • 1. Re: EJB3 session bean with EJB2 session dependency
          javasutra

          This is from the EJB3 tutorial docs:

          @EJB and @Resource also create an entry within the JNDI ENC of the bean. So, the above @EJB injection will create an entry for the reference calculator bean under "java:comp.ejb3/env/ejb/calculator". Note, the ENC name SHOULD be "java:/comp/env", but the JBoss Application server is currently being refactored in this area, so an EJB3 specific namespace is being used.


          I think this is my problem. Is there any way to force the ENC name for the injection to be in java:/comp/env?

          • 2. Re: EJB3 session bean with EJB2 session dependency
            sandello

            Did you solve your problem?

            • 3. Re: EJB3 session bean with EJB2 session dependency

              I am trying to do the same but without any success.

              I want to inject the EJB 2 business interface into an EJB 3 MDB. So I write:

               @EJB(name="ejb/Esb")
               EsbAdapter adapter;
              


              In the ejb-jar.xml I declare:
              <message-driven>
               <ejb-name>OutflowMdb</ejb-name>
              ....
               <ejb-ref>
               <ejb-ref-name>ejb/Esb</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <home>com.vendor.EsbAdapterHome</home>
               <remote>com.vendor.EsbAdapter</remote>
               </ejb-ref>
              </message-driven>
              


              Finally the jboss.xml contains:
              
               <message-driven>
               <ejb-name>OutflowMdb</ejb-name>
               <ejb-ref>
               <ejb-ref-name>ejb/Esb</ejb-ref-name>
               <jndi-name>someVendor/EsbAdpater</jndi-name>
               </ejb-ref>
               </message-driven>
              



              But I get For EJB OutflowMdb could not find jndi binding based on interface only for @EJB(com.vendor.EsbAdapter) not used by any EJBs

              The same happens when I leave away the declaration in the ejb-jar.xml

              I can't find any docs on how to correctly do this.



              • 4. Re: EJB3 session bean with EJB2 session dependency

                A step further:

                It seems that the ejb-ref in ejb-jar.xml is not necessary (as expected) and this is equivalent to specifying a @EJB annotation.

                Furthermore as the two EARs use separate class loaders we need this in the jboss.xml

                 <ejb-ref>
                 <ejb-ref-name>ejb/Esb</ejb-ref-name>
                 <jndi-name>jnp://localhost:1100/someVendor/Adpater</jndi-name>
                 </ejb-ref>
                
                


                however now I get:

                Unable to inject jndi dependency: env/ejb/Adapter into field protected com.vendor.EsbAdapterHome test.OutflowMdb.sifHome

                with a reason:

                javax.naming.NameNotFoundException: com.vendor.EsbAdapterHome not bound as a result of org.jnp.interfaces.NamingContext.resolveLink

                • 5. Re: EJB3 session bean with EJB2 session dependency
                  bdecoste

                  Here's an example of how to reference an EJB2.x bean from an EJB3.0 bean in two ways. The first is using annotations for the ejb reference and the other using the deployment descriptor.

                  The following is the EJB3 bean:

                  @Stateless(name="Test3")
                  @Remote(Test3.class)
                  @RemoteBinding(jndiBinding="Test3")
                  @EJBs({@EJB(name="injected/Test2", mappedName="Test2")})
                  public class Test3Bean
                   implements Test3
                  {
                   private static final Logger log = Logger.getLogger(Test3Bean.class);
                  
                   @EJB(name="ejb/Test2")
                   private Test2Home test2Home=null;
                  
                   public void testAccess() throws Exception
                   {
                   Test2 test2 = test2Home.create();
                   try {
                   InitialContext jndiContext = new InitialContext();
                   Test2Home home = (Test2Home)jndiContext.lookup(Container.ENC_CTX_NAME + "/env/injected/Test2");
                   test2 = home.create();
                   } catch (Exception e)
                   {
                   e.printStackTrace();
                   }
                   }
                  }
                  


                  The following is the EJB2.x ejb-jar.xml:
                  <ejb-jar>
                   <enterprise-beans>
                   <session>
                   <ejb-name>ejb/Test2</ejb-name>
                   <home>org.jboss.ejb3.test.reference21_30.Test2Home</home>
                   <remote>org.jboss.ejb3.test.reference21_30.Test2</remote>
                   <ejb-class>org.jboss.ejb3.test.reference21_30.Test2Bean</ejb-class>
                   <session-type>Stateless</session-type>
                   <transaction-type>Container</transaction-type>
                   </session>
                   </enterprise-beans>
                  </ejb-jar>
                  


                  And the EJB2.x jboss.xml:

                  <jboss>
                   <enterprise-beans>
                   <session>
                   <ejb-name>ejb/Test2</ejb-name>
                   <jndi-name>Test2</jndi-name>
                   </session>
                   </enterprise-beans>
                  </jboss>
                  


                  Now the EJB3 ejb-jar.xml
                  <ejb-jar>
                   <enterprise-beans>
                   <session>
                   <ejb-name>Test3</ejb-name>
                   <ejb-ref>
                   <ejb-ref-name>ejb/Test2</ejb-ref-name>
                   <ejb-ref-type>Session</ejb-ref-type>
                   </ejb-ref>
                   </session>
                   </enterprise-beans>
                  </ejb-jar>
                  


                  And the EJB3 jboss.xml:
                  <jboss>
                   <enterprise-beans>
                   <session>
                   <ejb-name>Test3</ejb-name>
                   <ejb-ref>
                   <ejb-ref-name>ejb/Test2</ejb-ref-name>
                   <jndi-name>Test2</jndi-name>
                   </ejb-ref>
                   </session>
                   </enterprise-beans>
                  </jboss>


                  • 6. Re: EJB3 session bean with EJB2 session dependency

                    Thanks. This is exactly what I am doing. But my 2.x bean is in an isolated EAR. And that seems to be a problem. I have to use the deployment descriptor reference because I must not hard code the JNDI name. Any ideas?