java – WebSphere 7. Inject EJBs from another application
I tried to inject EJB with @ EJB annotation:
>When I inject an EJB into another EJB in the same ear, it works normally. > When I inject an EJB from another ear into another EJB server, I get an exception:
I am using WebSphere 7 and EJB 3.0 When I do a JNDI search myself, it works normally How do I let the container know where to inject remote beans?
myapp1. Ear contains the following: myapp1 Jar (where is EJB)
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 is EJB)
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"; } }
Solution
Specify the binding when deploying the application (1,2), or include meta-inf / ibm-ejb-jar-bnd.exe in the EJB module of the client (myapp1) xml. It looks 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>
If you use @ EJB (name = "myrefname"), you can simplify EJB ref name = "..." If your myapp2 Jar contains < interface class = "..." binding-name =“...”/> The binding name can be simplified Components