本地EJB的JNDI查找(没有@EJB)

4

我有一个需求,要求我使用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
1个回答

7
你试过下面的代码吗?
context.lookup("ejblocal:"+MyObjectInterfaceLocal.class.getName())

WebSphere使用不同的默认绑定模式来处理远程和本地接口。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接