JNDI Lookup of local EJB (no @EJB)

BackSlash picture BackSlash · Nov 27, 2014 · Viewed 8.2k times · Source

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.

Answer

slwk picture slwk · Nov 27, 2014

Have you tried below code?

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

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