我在 Tapestry 应用程序中检索 EJB 时遇到问题。应用程序的另一部分在 OSGi 包中,并且全部部署在 Glassfish 上。
目前,这就是我的东西的样子:
我的界面(打包为jar):
@Remote
public interface MyEJBInterface {
public static final String JNDI_NAME = "ejb/MyEJBInterface";
public String sayHello(String name);
}
我的实现(打包为捆绑包):
@Stateless
@EJB(name = MyEJBInterface.JNDI_NAME, beanInterface = MyEJBInterface.class)
public class MyEJBImplementation implements MyEJBInterface {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
我在 AppModule 类中的 Tapestry 构建方法:
public MyEJBService buildMyEJBService() {
MyEJBInterface myEjb = new JndiLookupHelper<MyEJBInterface>().lookupNoPrefix(MyEJBInterface.JNDI_NAME);
MyEJBService service = new MyEJBServiceMock(myEjb);
return service;
}
我是这样查找的:
public class JndiLookupHelper<T> {
private T remoteObject;
public T lookupNoEjbPrefix(String name) {
String jndiPath = "java:comp/env/" + name;
try {
InitialContext ic = new InitialContext();
remoteObject = (T) ic.lookup(jndiPath);
} catch (NamingException ne) {
ne.printStackTrace();
}
return remoteObject;
}
}
我的问题是找不到 EJB 并引发异常:
javax.naming.NamingException: Lookup failed for 'java:comp/env/ejb/MyEJBInterface' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: No object bound to name java:comp/env/ejb/MyEJBInterface]