4 Replies Latest reply on Jan 17, 2014 12:46 PM by Marcos Souza

    Cross reference between .ear

    Marcos Souza Newbie

      This is my scenario:

      app1.ear

      public abstract class A {
           @EJB
           private X x;
      }
      @Stateless
      public class B extends A {
           // some methods
      }
      @Stateless
      public class X {
           // some methods
      }
      

       

      app2.ear

      @Stateless
      public class C extends A {
           @EJB
           private D d;
      }
      @Stateless
      public class D {
      }
      

       

      The app2.ear is dependent of the artifact 'client' of the ear1.ear, that removes the implementations of the EJBs.

      To deploy the app2.ear, I have to create the file META-INF/ejb-jar.xml and to declare all dependencies of my EJBs pointing to the app1.ear. By default, all classes that extends from class 'A', will have the field 'x' injected. To make all this works, I have to declare all the beans of the app2.ear in the ejb-jar.xml and to declare <ejb-ref-name> to all Beans.

      Is there some alternative to do all the injections of th app2.ear automatically/programmatically? The both .ear are in the same server/JVM.

        • 1. Re: Cross reference between .ear
          Wolf-Dieter Fink Master

          You might use jboss-all.xml, see here for more information.

          In that case set the lookup or beanname attribut for @EJB should do the work

          • 2. Re: Re: Cross reference between .ear
            Marcos Souza Newbie

            To the problem of the order of deployments, I resolved with this descriptor (jboss-all.xml).

            But what I want, using the refered example (http://www.mastertheboss.com/jboss-deploy/solving-ear-deployment-dependencies-in-jboss-as-7) it is:

            I have EJBs in app1.ear that have fields annotated with @EJB of the type of the EJBs from app2.ear. To the injection to work properly, I have to declare, in app1.ear, in file ejb-jar.xml all my EJBs configuring the dependencies explicitly. Example:

            @Stateless
            public class App1Foo implements IApp1Foo {
                 @EJB(name="bar")
                 private App2Bar bar;
            }
            

             

            This injection only works if I create the ejb-jar.xml, in app1.ear, app1-ejb.jar with this:

            <ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee" 
                     version = "3.1"
                     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
                     xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
                <enterprise-beans>
                    <session>
                        <ejb-name>App1Foo</ejb-name>
                        <ejb-ref>
                            <ejb-ref-name>bar</ejb-ref-name>
                            <lookup-name>java:global/app2/app2-ejb/BarBean</lookup-name>
                        </ejb-ref>
                    </session>
                <enterprise-beans>
            </ejb-jar>
            

             

             

            I am searching for an alternative to do this manually. My first option would be the container have some configuration to make it lookup in global JNDI tree first, without ejb-jar.xml obviously. The second option would be the some tool (maven plugin, etc) that generates the file (ejb-jar.xml) automatically, inspecting all my ejb with @EJB annotation.

            • 3. Re: Cross reference between .ear
              Wolf-Dieter Fink Master

              What if you add lookup="java:global/app2/app2-ejb/BarBean" to your @EJB annotation?

              • 4. Re: Cross reference between .ear
                Marcos Souza Newbie

                I could add this, but I would have to modify all my code to this. Besides, the code would be 'ugly'.

                In Glassfish v3 ou v4, I don't need to do this. In some way, he discovers the dependency and do the injection. This is a good alternative (to me) because the code remains clean.

                If this is not possible in Wildfly, I would like to generate the ejb-jar.xml reading some configuration file, with some default configuration, and instrospecting my beans looking for @EJBs.