7

I have a EJB defined as this:

package com.foo;
@Stateless (mappedName="HelloWorld")
public class HelloWorldBean implements HelloWorld, HelloWorldLocal
....

When it's deployed to Weblogic (WL), it gets the name myBean. I'm not sure if this is important.

I try to call the bean with this code:

Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
ic = new InitialContext(ht);
tp = (HelloWorld) ic.lookup("HelloWorld#com.foo.HelloWorldBean");

Anyone know why I get the following error?

javax.naming.NameNotFoundException: While trying to lookup 'HelloWorld#com.foo.HelloWorldBean' didn't find subcontext 'HelloWorld#com'.
 Resolved '' [Root exception is javax.naming.NameNotFoundException: While trying
 to lookup 'HelloWorld#com.foo.HelloWorldBean' didn't find
 subcontext 'HelloWorld#com'. Resolved '']; remaining name 'HelloWorld#com/foo/HelloWorldBean'
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Sajee
  • 4,317
  • 14
  • 46
  • 54
  • 1
    Does your bean have multiple Remote Business Interface? – Pascal Thivent Oct 24 '09 at 17:03
  • Yes, the HelloWorld interface. Sorry for lame names. I'm trying to get familiar with EJBs. – Sajee Oct 24 '09 at 17:22
  • That's only one, not multiple :) I've answered both cases anyway. – Pascal Thivent Oct 24 '09 at 17:29
  • Not sure you were notified so I'm pasting my answer here: In @Stateless, @name() is the annotation equivalent of in ejb-jar.xml. If no @Stateless name() is specified, it defaults to the unqualified bean class name. So I don't know from where "myBean" comes from. Then, yes, your EJB should definitely be visible in the JNDI tree as "HelloWorld" (the name you specified). Are you sure that the deployment went well and that your EJB is deployed? – Pascal Thivent Oct 24 '09 at 18:30
  • Yes, it appears my bean was deployed correctly. No errors from WL and the Deployment page in the Admin console shows the "MyBean" deployment of type "Library" having a state of "Active". – Sajee Oct 24 '09 at 19:07
  • myBean is coming from the jar name: myBean.jar – Sajee Oct 24 '09 at 19:10
  • +1. I'm struggling with the very same problem :-( – Leonel Mar 08 '10 at 16:35

1 Answers1

10

To lookup a Remote Interface of a Session Bean with multiple Remote Business interfaces (e.g.com.acme.FooBusiness1, com.acme.FooBusiness2), you need to lookup a name derived from the combination of the target ejb's global JNDI name (the mappedName() in @Stateless) and the specific Remote Business Interface, separated by a "#":

InitialContext ic = new InitialContext();
FooBusiness1 bean1 = (FooBusiness1) ic.lookup("FooEJB#com.acme.FooBusiness1");
FooBusiness2 bean2 = (FooBusiness2) ic.lookup("FooEJB#com.acme.FooBusiness2");

In the typical case of a bean only having one Remote Business Interface, this fully-qualified form is not needed. In that case, the bean's JNDI name can be used directly :

FooBusiness bean = (FooBusiness) ic.lookup("FooEJB");

That was the theoretical part. Now the practice. In your case, from what I can see, you are accessing the EJB from Weblogic so I'd rather use the no-arg InitialContext() constructor (and use a jndi.properties configuration file for other environments) but this is just a side note. Then, you should look up com.foo.HelloWorld, the Remote Interface, not com.foo.HelloWorldBean, the implementation:

InitialContext ic = new InitialContext();
(HelloWorld) ic.lookup("HelloWorld#com.foo.HelloWorld");

And if your bean has only one Remote Business Interface, this should work:

(HelloWorld) ic.lookup("HelloWorld");
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Pascal: I tried ic.lookup("HelloWorld#com.foo.HelloWorld") and I get the same error as above. I tried ic.lookup("HelloWorld") and I get the same error as above: javax.naming.NameNotFoundException: Unable to resolve 'HelloWorld'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'HelloWorld'. Resolved '']; remaining name 'HelloWorld' – Sajee Oct 24 '09 at 17:32
  • I deployed the EJB to Weblogic and it appears as myBean in the Deployments page in the WL Administration Console. Yet when I look at the JNDI tree, I can't find myBean or HelloWorld anywhere in that tree. Perhaps that's a clue? – Sajee Oct 24 '09 at 17:40
  • In @Stateless, @name() is the annotation equivalent of in ejb-jar.xml. If no @Stateless name() is specified, it defaults to the unqualified bean class name. So I don't know from where "myBean" comes from. Then, yes, your EJB should definitely be visible in the JNDI tree as "HelloWorld" (the name you specified). Are you sure that the deployment went well and that your EJB is deployed? – Pascal Thivent Oct 24 '09 at 18:30
  • Pascal: Something was screwed up w/ my .jar file. Once I fixed that, everything worked. Thanks for your help. – Sajee Oct 24 '09 at 19:36