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

Bac*_*ash 4 java ejb java-ee ejb-3.0 websphere-8

我有一个要求,我被要求使用JNDI查找加载远程和本地EJB,因此没有@EJB注释.

我的EJB定义如下:

@Remote
public interface MyObjectInterfaceRemote extends MyObjectInterface {

}
Run Code Online (Sandbox Code Playgroud)
@Local
public interface MyObjectInterfaceLocal extends MyObjectInterface {

}
Run Code Online (Sandbox Code Playgroud)
public interface MyObjectInterface {
    // A bunch of methods which both remote and local ejbs will inherit
}
Run Code Online (Sandbox Code Playgroud)
@Remote(MyObjectInterfaceRemote.class)
@Local(MyObjectInterfaceLocal.class)
public class MyObjectEJB implements MyObjectInterfaceLocal, MyObjectInterfaceRemote {
    //implementation of all methods inherited from MyObjectInterface.
}
Run Code Online (Sandbox Code Playgroud)

我正在使用此代码来查找远程EJB:

private MyObjectInterfaceRemote getEJB() throws NamingException {
    InitialContext context = new InitialContext();
    return (MyObjectInterfaceRemote) context.lookup(MyObjectInterfaceRemote.class.getName());
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,但如果我做另一个这样的方法:

private MyObjectInterfaceLocal getLocalEJB() throws NamingException {
    InitialContext context = new InitialContext();
    return (MyObjectInterfaceLocal) context.lookup(MyObjectInterfaceLocal.class.getName());
}
Run Code Online (Sandbox Code Playgroud)

我明白了

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]
[...]
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我是否必须使用不同的东西来查找本地ejb?

注意:如果我使用

@EJB
MyObjectInterfaceLocal ejb;
Run Code Online (Sandbox Code Playgroud)

本地ejb成功加载.

小智 6

你试过下面的代码吗?

context.lookup("ejblocal:"+MyObjectInterfaceLocal.class.getName())
Run Code Online (Sandbox Code Playgroud)

Webshpere对远程和本地接口使用不同的默认绑定模式.