5 Replies Latest reply on Nov 21, 2011 5:08 AM by Wolf-Dieter Fink

    JNDI lookup in JBoss 7

    Hiep Le Canh Newbie

      I am upgrading my application from JBoss 5 to jboss-as-7.0.2.Final. It seems that I have to change all JNDI name in my application. I do know how to change them, but I have a performance issue now:

       

      Eg. I have about 10 ejb modules in my ear file. Some beans in this ejb module and some beans in the other ejb module. In JBoss 5, I use only:

       

      ic.lookup("myBeanName/local");     // Context ic = getContext();

       

      but in JBoss 7 I must use:

       

      ic.lookup("myEJBModuleName/myBeanName");     // Context ic = getContext();

       

      so when I look up one specific bean, I have to loop all of the EJB modules to find out that bean, so it is very slow to get a bean by JNDI Name. Do you have any solution (or configuration) to back to using old JNDI mane rules?

       

      Thanks

      Hiep

        • 1. Re: JNDI lookup in JBoss 7
          jaikiran pai Master

          Hiep Le Canh wrote:

           

           

          so when I look up one specific bean, I have to loop all of the EJB modules to find out that bean, so it is very slow to get a bean by JNDI Name. Do you have any solution (or configuration) to back to using old JNDI mane rules?


          I'm not sure why you are doing that. Why would you need a loop (and loop over what?) when you know which bean to lookup? Can you show us some code which has this loop?

          • 2. Re: JNDI lookup in JBoss 7
            Hiep Le Canh Newbie

            Hi jaijiran,

             

            Below is my src code which was run in JBoss 5:

             

            public <T> T getBean(Class<T> clazz) {

                final String jndiName = clazz.getSimpleName() + "Bean";

                    try {

                        Context ic = getContext();

                        Object obj;

                        try {

                            obj = ic.lookup("myearname/" + jndiName + "/local");

                        } catch (NamingException x) {

                            obj = ic.lookup("myearname/" + jndiName + "/remote");

                        }

                        return (T) obj;

                    } catch (NamingException e) {   

                    .......

             

            The param clazz is the interface which placed in ejbModule1, but the instance of it is in ejbModule2. I can not find the name of ejbModule2, so I need to loop all of the ejbModule deplyed to find the instance of it.

            • 3. Re: JNDI lookup in JBoss 7
              Riccardo Pasquini Novice

              Is the use case a several bean implementation for the same interface deployed contextually in your application and fetch the first available?

               

              If not (as i expect) and if you are in a managed context, you should consider injection for the following reasons:

              • as7.0 doesn't support remoting, so the catch block is not safe;
              • no need to perform a lookup.

               

              Otherwise, you should reconsider the first available policy becuase it is too... dangerous...

               

              If you need a god-method to fetch any EJB then you can create a flyweight map using class/string pairs to be used as index for the getBean method, the map used as index can validate input (contains key) and the jndi name is the value the application expects.

               

              Moreover if the map is loaded at start time from a configuration file, it becomes more maintenable.

               

              i hope this can help.

               

              PS the syntax used in the code is not portable (global AS jndi access for JEE < 6 ), so code fixes aren't something strange for this situation...

              • 4. Re: JNDI lookup in JBoss 7
                Hiep Le Canh Newbie

                Thanks Riccardo, that is what I am thinking, I will create a flyweight map for this case.

                • 5. Re: JNDI lookup in JBoss 7
                  Wolf-Dieter Fink Master

                  For me it is not apparent why you use such complex lookup. As you mentioned all ejb-jars are included in your ear and you should know how it is.

                  Also DI (injection) is free of code for this.

                   

                  It sounds a bit random if you deploy the same class with different implementations in different ejb components.