I have a multi-ear project. Some EJB’s reside in project1.ear, and I need to use it in project2.ear.
I’m trying the following:
@Remote
public interface MyService {
public static final String JNDI_NAME = "service/MyService";
// methods
}
@Stateless(name = MyService.JNDI_NAME, mappedName = MyService.JNDI_NAME)
public class MyServiceImpl implements MyService {
// implementations...
}
Then, in some project2.ear class, I inject the EJB like this:
@Stateless(name = "other/Service")
public class OtherServiceImpl implements OtherService{
@EJB(mappedName = MyService.JNDI_NAME)
private MyService service;
}
But I got a lot of “FailedToConstructException” and EJB related exception, with the following cause:
Caused by: javax.naming.NameNotFoundException: service/MyService -- service jboss.naming.context.java.service.MyService
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:113)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214)
at javax.naming.InitialContext.lookup(InitialContext.java:411) [rt.jar:1.7.0_05]
at org.jboss.as.ejb3.deployment.processors.EjbLookupInjectionSource$1.getReference(EjbLookupInjectionSource.java:82) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
... 104 more
What am I doing wrong?
Thanks in advance.
Try to browse JNDI tree to see whether your
service/MyServiceexists. If you’re using JBoss 6, you can find instructions here. Not sure if they added it into higher versions.Next, check packages – interface
MyServiceneeds to be in the same package in both ears, otherwise lookup will fail.Finally, there’s no need to have both
nameandmappedNameattributes in@Statelessannotation,mappedNameis enough. You can also try to injectMyServicewith@EJBannotation without any attribute, if project1.ear is only one that implements that interface, application server will inject it: