4

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.

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • Yes, to lookup local interface you have to prefix it with `ejblocal:`. Check here [What's the default JNDI name of an EJB in Websphere](http://stackoverflow.com/a/24005631/3701228) and notice ejblocal prefixes. You should see these names in the log during application startup. – Gas Nov 27 '14 at 11:33

1 Answers1

7

Have you tried below code?

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

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

slwk
  • 587
  • 3
  • 7