WebSphere 7. Inject EJB from another application

Denis Tulskiy picture Denis Tulskiy · Feb 17, 2012 · Viewed 9.6k times · Source

I am trying to inject an EJB with the @EJB annotation:

  • when I inject an EJB into another EJB in the same ear it works fine.
  • when I inject an EJB into another EJB from another ear in the same server I get an exception:

EJB threw an unexpected (non-declared) exception during invocation of method "sayHello". Exception data: javax.ejb.EJBException: Injection failure; nested exception is: com.ibm.ejs.container.EJBNotFoundException: EJB with interface com.mycompany.myapp.ejb.test2 not present in application myapp1

I'm using WebSphere 7 and EJB 3.0. When I'm doing a jndi lookup myself, it works fine. How do I let the container know from where to inject my remote beans?

myapp1.ear contains the following : myapp1.jar (where the EJB is)

myapp1 EJB :

package com.mycompany.myapp1.ejb.test1;

@Remote
public interface HelloEjb1 { 
    public String sayHello();
}

EJB Impl :

package com.mycompany.myapp.ejb.test1;

@Stateless
public class HelloEjbImpl1 implements HelloEjb1 {

    @EJB
 HelloEjb2 helloEjb2;

    @Override
    public String sayHello() {
        return HelloEjb2.sayHello();
    }
}

myapp2.ear contains the following : myapp2.jar (where the EJB is)

myapp2 EJB :

package com.mycompany.myapp2.ejb.test2;

@Remote
public interface HelloEjb2 { 
    public String sayHello();
}

EJB Impl :

package com.mycompany.myapp2.ejb.test2;

@Stateless
public class HelloEjbImpl2 implements HelloEjb2 {

    @Override
    public String sayHello() {
        return "Hello";
    }
}

Answer

Brett Kail picture Brett Kail · Feb 18, 2012

Specify the binding when deploying the application (1, 2), or include a META-INF/ibm-ejb-jar-bnd.xml in your client (myapp1) EJB module. It would look something like this:

<ejb-jar-bnd
    xmlns="http://websphere.ibm.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd"
    version "1.0">
  <session name="HelloEjbImpl1">
    <ejb-ref name="com.mycompany.myapp.ejb.test1/helloEjb2"
        binding-name="myapp2/myapp2.jar/HelloEjbImpl2#com.mycompany.myapp1.ejb.test1.HelloEjb1"
  </session>  
</ejb-jar-bnd>

The ejb-ref name="..." can be simplified if you use @EJB(name="myrefname"). The binding-name can be simplified if your myapp2.jar includes a similar binding file with an <interface class="..." binding-name="..."/> element.