11 Replies Latest reply on Jan 16, 2015 5:18 AM by baseaditi

    Using ejb-ref to mantain old JNDI bindings in code

    mylos78

      Hi all!

      I'm porting an application to JBoss AS 7. I have an issue with EJB: my EJBs, written for JBoss 4, and use a JNDI binding which is not accepted by JBoss AS 7:

       

      final Context context = new InitialContext(jndiProperties);

      return (TelcoInfo) context.lookup("ejb/TelcoInfo");

       

      Now I have added the new naming prefix to all EJB lookups:

       

      final Hashtable jndiProperties = new Hashtable();

      jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

       

      final Context context = new InitialContext(jndiProperties);

      return (TelcoInfo) context.lookup("ejb/TelcoInfo");

       

      I'd like to keep for the moment the old JNDI binding so I've added a jboss-ejb3.xml file containing the correct reference:

       

         <session>

             <session-type>Stateless</session-type>

               <ejb-name>TelcoInfo</ejb-name>

                <ejb-class>com.telco.ejb.TelcoInfoBean</ejb-class>

               

                <ejb-ref>

                  <ejb-ref-name>ejb/TelcoInfo</ejb-ref-name>

                  <lookup-name>ejb:/telecom//TelcoInfoBean!com.telco.ejb.TelcoInfo</lookup-name>

               </ejb-ref>

             

            </session>

       

      Unfortunately it does not work. I get as result:

      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

       

      (If I change manually the lookup in the code to ejb:/telecom//TelcoInfoBean!com.telco.ejb.TelcoInfo it works)

      Is the jboss-ejb3.xml still able to alias the old JNDI binding to the new one ?

      Thanks a lot

      Mylos

        • 1. Re: Using ejb-ref to mantain old JNDI bindings in code
          sfcoy

          An ejb-ref-name, indeed any *-ref-name is a name in the ENC (environment naming context).

           

          If you declare:

           

          {code:xml}<ejb-ref-name>ejb/TelcoInfo</ejb-ref-name>{code}

           

          then in code you must:

           

          {code:java}return (TelcoInfo) context.lookup("java:comp/env/ejb/TelcoInfo");{code}

           

          ie. There is always an implicit "java:comp/env/".

           

          For what it's worth, this is always one of the first steps I perform in a code migration. I ensure that JNDI lookups are using ENC names everywhere and verify correct operation on the original source platform. JNDI ENC lookups are 100% portable across server versions and vendors.

           

          In your case, these ENC names would be mapped to their true global names in the jboss.xml file (on JBoss 4). You will need to subsequently convert your jboss.xml file to the equivalent jboss-ejb3.xml file.

           

          Finally, if your EJBs are all running inside the same Java VM, you can do away with the jndiProperties.

           

          There's more information available in AS71 EJB 3 Reference Guide.

          • 2. Re: Using ejb-ref to mantain old JNDI bindings in code
            mylos78

            Thanks for your reply Stephen,

            I've tried looking up the EJB with:

            return (TelcoInfo) context.lookup("java:comp/env/ejb/TelcoInfo");

            however it produced the same error. Also tried with

            return (TelcoInfo) context.lookup("java:global/ejb/TelcoInfo");

            with no luck.

             

            Finally, by setting the JNDI binding via configuration file didn't help too....

            <bindings>

                            <lookup name="ejb/TelcoInfo" lookup="ejb:/telecom//TelcoInfoBean!com.telco.ejb.TelcoInfo"/>

                   

            </bindings>

            I don't know what else I could try :-(

            Thanks

            Mylos

            • 3. Re: Using ejb-ref to mantain old JNDI bindings in code
              jaikiran

              Which exact version of JBoss AS7? Where is that lookup being done? Please post the entire exception stacktrace.

              • 4. Re: Using ejb-ref to mantain old JNDI bindings in code
                mylos78

                Hello,

                so the release I'm using is 7.1.1 final release and the stacktrace available on the client is

                 

                Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

                    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)

                    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)

                    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)

                    at javax.naming.InitialContext.lookup(InitialContext.java:392)

                    at com.telcosuite.ejbclient.RemoteEJBClient.lookupInfoEJB(RemoteEJBClient.java:144)

                 

                The line 144 of RemoteEJBClient is

                return (TelcoInfo) context.lookup("java:comp/env/ejb/TelcoInfo");

                 

                Thanks

                Mylos

                • 5. Re: Using ejb-ref to mantain old JNDI bindings in code
                  nickarls

                  What do you stick in jndiProperties at

                   

                  final Context context = new InitialContext(jndiProperties);

                   

                  ?

                  • 6. Re: Using ejb-ref to mantain old JNDI bindings in code
                    sfcoy

                    This won't work without a correctly set up jboss-ejb3.xml file:

                     

                    {code:xml}

                    <?xml version="1.0"?>

                    <jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"

                                      xmlns="http://java.sun.com/xml/ns/javaee"

                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                                      xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd

                                         http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"

                                      version="3.1"

                                      impl-version="2.0">

                       <enterprise-beans>

                         

                          <session>

                             <ejb-name>TelcoInfo</ejb-name>

                             <ejb-ref>

                                <ejb-ref-name>ejb/TelcoInfo</ejb-ref-name>

                                <jndi-name>ejb:/telecom//TelcoInfoBean!com.telco.ejb.TelcoInfo</jndi-name>

                             </ejb-ref>

                          </session>

                             

                       </enterprise-beans>

                    </jboss:ejb-jar>

                    {code}

                    • 7. Re: Using ejb-ref to mantain old JNDI bindings in code
                      jaikiran

                      You really mean a standalone remote client, from where this lookup is happening? The ENC namespace (java:comp) is only visible to the component in which you have configured that ejb-ref and it isn't visible outside of it, let alone a remote standalone client.

                      • 8. Re: Using ejb-ref to mantain old JNDI bindings in code
                        mylos78

                        @Nicklas

                        Here's my initial context lookup:

                         

                        final Hashtable jndiProperties = new Hashtable();

                        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

                        • 9. Re: Using ejb-ref to mantain old JNDI bindings in code
                          mylos78

                          @Stephen

                          Thanks, however I'm afraid the jboss-ejb3.xml you posted is not supported.

                          I've got some exceptions

                          Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBA

                          S014551: <session-type> not specified for ejb TelcoInfo. This must be present in ejb-jar.xml

                           

                          Once adding the session-type attribute

                           

                          Caused by: java.lang.IllegalArgumentException: JBAS011084: componentClassName is  null

                           

                          So, I've ended up with:

                           

                          <jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"

                                   xmlns="http://java.sun.com/xml/ns/javaee"      

                                           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     

                                     xmlns:c="urn:clustering:1.0"   

                                     xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"      

                                            version="3.1"               impl-version="2.0">

                           

                             <enterprise-beans>

                           

                                  <session>

                                   <session-type>Stateless</session-type>

                                   <ejb-name>TelcoInfo</ejb-name>

                                    <ejb-class>com.telco.ejb.TelcoInfoBean</ejb-class>

                                   <ejb-ref>

                                      <ejb-ref-name>ejb/TelcoInfo</ejb-ref-name>

                                      <jndi-name>ejb:/telecom//TelcoInfoBean!com.telco.ejb.TelcoInfo</jndi-name>

                                      <lookup-name>ejb:/telecom//TelcoInfoBean!com.telco.ejb.TelcoInfo</lookup-name>

                                   </ejb-ref>

                                </session>

                           

                             </enterprise-beans>

                          </jboss:ejb-jar>

                           

                          However the issue stayed the same :-(

                          • 10. Re: Using ejb-ref to mantain old JNDI bindings in code
                            sfcoy

                            Can we clarify where your EJBs are deployed and where the client code is deployed please.

                             

                            Also, reading the above again I've realized that the ejb-refs are self referencing? That's probably not what you intended. An ejb-ref is a reference from within the declaring enterprise bean to a different enterprise bean.

                            • 11. Re: Using ejb-ref to mantain old JNDI bindings in code
                              baseaditi

                              so there has any other way to manage JNDI binding from JBOSS5 to Jboss7?

                              Please let me know.