6 Replies Latest reply on May 31, 2013 3:57 AM by stylpe

    Use plain @EJB for remote EJB lookup?

    stylpe

      I've been using the guide at https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+server+instance to make a standalone war that uses beans that are deployed in an ear running on a separate AS. I got it working by using an explicit lookup name in the @EJB annotation, like this:

       

      @EJB(lookup = "ejb:earname/modulename/BeanClass!fully.qualified.RemoteInterface")

      private RemoteInterface bean;

       

      Writing this for all the remote EJBs is getting tedious, and refactoring with it is even worse. Is there a way to tell the default context which app name and module name to use, so that I can use plain @EJB annotations without parameters?

       

      I know I could create a helper class and use InitialContext.lookup, or alias each bean I plan to use in xml (I forget which one), but I want to be sure there isn't an easier, cleaner way to do this.

        • 1. Re: Use plain @EJB for remote EJB lookup?
          jaikiran

          Mikal, welcome to the forums.

           

           

          Mikal Henriksen wrote:

           

          I've been using the guide at https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+server+instance to make a standalone war that uses beans that are deployed in an ear running on a separate AS. I got it working by using an explicit lookup name in the @EJB annotation, like this:

           

          @EJB(lookup = "ejb:earname/modulename/BeanClass!fully.qualified.RemoteInterface")

          private RemoteInterface bean;

           

          Writing this for all the remote EJBs is getting tedious, and refactoring with it is even worse. Is there a way to tell the default context which app name and module name to use, so that I can use plain @EJB annotations without parameters?

           

          The server cannot guess what the appname, module name and bean name would be for that target bean (which resides on a different server) to be. It's the user's application which has that idea. One way to reduce the tediouness is to use the ejb-jar.xml to set up the injection (ejb-jar xsd has the necessary details) such that you can then use something like this in the ejb-jar.xml:

           

           <lookup-name>${myfoo.bar.system.property}</lookup-name>
          

           

          and set the system property (typically by passing -D<system-property-name>=<system-property-value>) while launching the server. Note that the system property replacement in deployment descriptors is a JBoss application server specific features and may not be portable across different server vendors.

          • 2. Re: Use plain @EJB for remote EJB lookup?
            stylpe

            Thanks for the quick reply!

            jaikiran pai wrote:

             

            The server cannot guess what the appname, module name and bean name would be for that target bean (which resides on a different server) to be. It's the user's application which has that idea.

            I know it can't guess the appname and module name, that's what I wanted to know if I could configure somewhere. I guess my idea is that I should be able to tell the web app "Hey, use this server, this appname and this module name for ejb lookups," and the servers figure out the rest from there.

             

            But your answer made me realize that we also need to know the name for the implementing class, since that's part of every standard jndi name. Thinking about it, it seems weird that the client should have to know about the name of the server-side class implementing the interface (the exception is of course multiple implementations of the same interface).

             

            Is this just a limitation of jndi and remote ejb? Other than mapping these session beans to custom, shorter jndi names, is there no simpler way to accomplish something like this?

            • 3. Re: Use plain @EJB for remote EJB lookup?
              jaikiran

              Mikal Henriksen wrote:

               

               

              But your answer made me realize that we also need to know the name for the implementing class, since that's part of every standard jndi name. Thinking about it, it seems weird that the client should have to know about the name of the server-side class implementing the interface (the exception is of course multiple implementations of the same interface).

               

              You don't need to know the class name of the implementing bean class. That's just a default that gets used. i.e. the bean name defaults to the simple class name of the implementing class. It can always be overriden using the name attribute of the appropriate annotation (@Stateless, @Stateful, @Singleton) or via the ejb-name element of ejb-jar.xml.

              • 4. Re: Use plain @EJB for remote EJB lookup?
                stylpe

                jaikiran pai wrote:

                 

                Mikal Henriksen wrote:

                 

                 

                But your answer made me realize that we also need to know the name for the implementing class, since that's part of every standard jndi name. Thinking about it, it seems weird that the client should have to know about the name of the server-side class implementing the interface (the exception is of course multiple implementations of the same interface).

                 

                You don't need to know the class name of the implementing bean class. That's just a default that gets used. i.e. the bean name defaults to the simple class name of the implementing class. It can always be overriden using the name attribute of the appropriate annotation (@Stateless, @Stateful, @Singleton) or via the ejb-name element of ejb-jar.xml.

                I've done some more research, and I guess what it boils down to is that the client needs to know that name (whether default or custom); the interface alone plus the connection to the server is not enough to lookup and connect to the remote implementation.

                 

                Are there other approaches I can use to streamline this? Is this something domain deployment would help with?

                • 5. Re: Use plain @EJB for remote EJB lookup?
                  jaikiran

                  Mikal Henriksen wrote:

                  I've done some more research, and I guess what it boils down to is that the client needs to know that name (whether default or custom); the interface alone plus the connection to the server is not enough to lookup and connect to the remote implementation.

                  Of course it has to know the name of the bean it is trying to communicate with. A remote interface can be implemented by multiple beans. So it's upto the client to tell the server which bean it is interested in communicating with.

                  Mikal Henriksen wrote:

                   

                  Are there other approaches I can use to streamline this? Is this something domain deployment would help with?

                  Why are you trying to avoid the bean name usage?

                  • 6. Re: Use plain @EJB for remote EJB lookup?
                    stylpe

                    Okay, so while reading through the EJB 3.0 spec, I've realized that dependency injection and jndi lookup are independent concepts, and I've been researching the wrong concept So we've more or less been talking past each other the whole time. Sorry about that.

                     

                    The actual question I'm trying to answer is: Is there a way to do dependency injection cross-server? Essentially binding my client war to the server's context. I don't know if this involves jndi naming in any way, so it probably belongs in one of the other subforums, right?