6 Replies Latest reply on Mar 12, 2008 9:54 AM by chawax

    How to retrieve an EJB from a JAR used in many EAR ?

    chawax

      Hi,

      I work with EJB3 (I am a newbie) and I have problems about state of the art way to retrieve an EJB from a JAR that will be used on many EAR projects.

      I have the following EJB (generated from AndroMDA) :

      <session>
       <description>
       <![CDATA[
      
       ]]>
       </description>
       <ejb-name>ServiceEmployeBean</ejb-name>
       <remote>myCompany.ServiceEmployeRemote</remote>
       <ejb-class>myCompany.ServiceEmployeBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Container</transaction-type>
      </session>


      My JAR contains Seam pojo classes that need to retrieve this EJB. I could do it this way :

      protected ServiceAbsenceRemote getServiceAbsence() throws Exception
      {
       if (this.serviceAbsence == null) {
       InitialContext initialContext = new InitialContext();
       this.serviceAbsence = (ServiceAbsenceRemote)
       initialContext.lookup("t4-ihm-self-ear-1.0-SNAPSHOT/ServiceAbsenceBean/remote");
       }
       return this.serviceAbsence;
      }


      As you can see, the problem is that I need to prefix the EJB name with the EAR name. If I don't do that I have a "ServiceAbsenceBean" not bound message. So it looks like this JAR can be used only in one EAR and I ask myself how it could work in another EAR ?

      I am sure I do something wrong, so please tell me ;)

      Thanks in advance

        • 1. Re: How to retrieve an EJB from a JAR used in many EAR ?
          jaikiran

          You can create a jboss.xml file and mention the jndi-name of your choice for the EJB. Something like this:

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
          
          
          <jboss>
          
           <enterprise-beans>
           <session>
           <ejb-name>ServiceEmployeBean</ejb-name>
           <jndi-name>MyBean</jndi-name>
          
           </session>
           </enterprise-beans>
          
          
          
          </jboss>
          


          Place this jboss.xml in the META-INF folder of your ejb jar (same place as ejb-jar.xml). Then use this jndi-name (in this case "MyBean") in your code, to lookup the bean.


          P.S: You mentioned that you use EJB3. In EJB3 you no longer need the xmls to define the beans or the jndi-name. You can use annotations to do this. See this EJBTrail for more details http://trailblazer.demo.jboss.com/EJB3Trail/


          • 2. Re: How to retrieve an EJB from a JAR used in many EAR ?
            chawax

            Thanks for your response. I guess this jboss.xml file works only for JBoss application server ? The problem is that my app has to work on other AS such as IBM Webpshere. Is there a standard solution for this ?

            P.S. : I saw you can use annotations with EJB3 but this part of my application is generated from AndroMDA so it's not my own choice ;)

            • 3. Re: How to retrieve an EJB from a JAR used in many EAR ?
              jaikiran

               

              "chawax" wrote:
              Thanks for your response. I guess this jboss.xml file works only for JBoss application server ? The problem is that my app has to work on other AS such as IBM Webpshere. Is there a standard solution for this ?



              Yes the jboss.xml file is JBoss specific. Each server provides it own way to specify jndi-names for EJBs. While porting this application to some other server, you will have to use that application server specific files to specify the jndi-name.


              • 4. Re: How to retrieve an EJB from a JAR used in many EAR ?
                chawax

                Well, I hope there will be a standard solution for this in next EJB specifications. Do you have an idea how difficult it is to do the same with Webpshere or other application servers ?

                • 5. Re: How to retrieve an EJB from a JAR used in many EAR ?
                  jaikiran

                   

                  "chawax" wrote:
                  Do you have an idea how difficult it is to do the same with Webpshere or other application servers ?


                  Instead of jboss.xml file they have their own xml files (for websphere it is ibm-ejb-jar-bnd.xmi).


                  • 6. Re: How to retrieve an EJB from a JAR used in many EAR ?
                    chawax

                    OK, thanks a lot, you were very helpful.