简体   繁体   中英

JNDI Lookup of local EJB (no @EJB)

I have a requirement where I'm asked to load both remote and local EJBs with a JNDI lookup, so without the @EJB annotation.

My EJB is defined as follows:

@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.
}

I'm using this code to lookup the remote EJB:

private MyObjectInterfaceRemote getEJB() throws NamingException {
    InitialContext context = new InitialContext();
    return (MyObjectInterfaceRemote) context.lookup(MyObjectInterfaceRemote.class.getName());
}

It works fine, but if I make another method like this:

private MyObjectInterfaceLocal getLocalEJB() throws NamingException {
    InitialContext context = new InitialContext();
    return (MyObjectInterfaceLocal) context.lookup(MyObjectInterfaceLocal.class.getName());
}

I get

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]
[...]

What am I missing? Do I have to use something different to lookup a local ejb?

Note: If I use

@EJB
MyObjectInterfaceLocal ejb;

The local ejb gets succesfully loaded.

Have you tried below code?

context.lookup("ejblocal:"+MyObjectInterfaceLocal.class.getName())

Webshpere uses different default binding pattern for remote and local interfaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM