我有一个需求,要求我使用JNDI查找来加载远程和本地的EJB,而不使用@EJB注释。
我的EJB定义如下:
@Remote
public interface MyObjectInterfaceRemote extends MyObjectInterface {
}
@Local
public interface MyObjectInterfaceLocal extends MyObjectInterface {
}
public interface MyObjectInterface {
// A bunch of methods which both remote and local ejbs will inherit
}
@Remote(MyObjectInterfaceRemote.class)
@Local(MyObjectInterfaceLocal.class)
public class MyObjectEJB implements MyObjectInterfaceLocal, MyObjectInterfaceRemote {
//implementation of all methods inherited from MyObjectInterface.
}
我正在使用以下代码查找远程 EJB:
private MyObjectInterfaceRemote getEJB() throws NamingException {
InitialContext context = new InitialContext();
return (MyObjectInterfaceRemote) context.lookup(MyObjectInterfaceRemote.class.getName());
}
它可以正常工作,但如果我再创建一个类似这样的方法:
private MyObjectInterfaceLocal getLocalEJB() throws NamingException {
InitialContext context = new InitialContext();
return (MyObjectInterfaceLocal) context.lookup(MyObjectInterfaceLocal.class.getName());
}
我明白了
javax.naming.NameNotFoundException:
Context: Backslash-PCNode03Cell/nodes/Backslash-PCNode03/servers/server1,
name: MyObjectInterfaceLocal: First component in name MyObjectInterfaceLocal not found.
[Root exception is org.omg.CosNaming.NamingContextPackage.NotFound:
IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
[...]
我可能漏掉了什么?我需要使用其他方法查找本地ejb吗?
注意:如果我使用标签,它会导致HTML格式错误。
@EJB
MyObjectInterfaceLocal ejb;
本地ejb已成功加载。
ejblocal:
前缀。请参阅此处 What's the default JNDI name of an EJB in Websphere 并注意 ejblocal 前缀。您应该在应用程序启动期间的日志中看到这些名称。 - Gas