0

I've an EAR app contains two modules.

EJB Module and Web Module.

In web module, I am trying to get a ref to some EJB SLSB, I don't use injection cause the class the I need to invoke the method in is not managed.

I am using the following code from the web module:

IFooBarService service = InitialContext.doLookup("IFooBarService");

IFooBarService: the the Local Interface that defined as ( in the ejb module):

@Local
public interface IFooBarService
{
    // ...
}

Do I miss something? (should I supply environment info?)

Community
  • 1
  • 1
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219

2 Answers2

1

Are you sure that IFooBarService is the JNDI name the IFooBarService service is bound to? JBoss for example shows the JNDI names in the boot log. You can then use it for lookup purposes.

In general, if you want your app to be portable you shouldn't rely on the server mechanism to generate JNDI names, as the Java EE spec has its own. You should be able to do:

IFooBarService service = InitialContext.doLookup("java:comp/env/IFooBarService");

If you are using newer versions of Java EE (Java EE 6), and want to lookup an EJB which is in the same app but in a different module, you can do:

IFooBarService service = InitialContext.doLookup("java:app/[module name]/IFooBarService");

More info on standard names here.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • In `Ocj4`, I can lookup whatever name I put in `@Stateless(name="IFooBarService")`. In JBoss I can remember it was something like `{moudleName}/{ServiceName}/{local|remote}` (please correct me if I missing some thing).. – Muhammad Hewedy Jan 18 '12 at 09:24
  • Can you provide me a like about portable names for jee5? – Muhammad Hewedy Jan 18 '12 at 09:37
  • java:app/[module name]/IFooBarService is the spec definition for the lookup, not the JBoss definition. I haven't worked with OC4J myself, but I would expect it to be JEE compliant and therefore you should be able to used the method described above. – Gonzalo Garcia Lasurtegui Jan 18 '12 at 09:44
  • I've tried JEE 5 JNDI Portable JNDI name -provided above- but It seems not working! – Muhammad Hewedy Jan 18 '12 at 10:29
0

From Here: https://forums.oracle.com/forums/thread.jspa?threadID=476903

The solution is:

fooBarService = (FooBarService) ((StatelessSessionDefaultLocalHome)
    new InitialContext().lookup("EJBModuleName_FooBarServiceLocal")).create();
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219