• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jndi lookup for an ejb through a servlet.

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My ear project contains an EJB project and a web project

I am trying to lookup an ejb reference through JNDI as follows...
This I am doing through a servlet.

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL,
"corbaloc:iiop:localhost:2812");
Context initialContext = new InitialContext(env);
java.lang.Object ejbHome =
initialContext.lookup(
"java:comp/env/ejbs/MyBeanOneHome");

MyBeanOne is defined in package "ejbs".

But i am getting following exception.
javax.naming.NameNotFoundException: Name comp/env/ejbs not found in context "java:".

Do I have to declare some ejb references in web.xml as well.
(I have read in some posts by ranchers that i need to add some reference in web.xml)
So far I haven't added anything to web.xml.

Please help me with it.

Regards,
Giriraj.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try passing the fully qualified class name of the interface to the lookup method.

Normally, the application server binds the session beans to its fully qualified class name in global scope when you don't specify the mappedName

I am really not sure why you want a lookup in the JNDI since you are accessing the orb in the localhost. Therefore, you are operating in the same JVM.

You could declare the following



as instance variables assuming MyBeanOneHome is the interface you are exposing. Dependency injection would take care of it for you.

Regards
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jerwin for your reply.
I tried it the way you suggested.
But this gives me following...

javax.naming.NameNotFoundException: Context: /servers/server1, name: ejbs.MyBeanOneHome: First component in name ejbs.MyBeanOneHome not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]


The reason I am using JNDI is because my ejbs might belong to a distributed environment.
I am trying this as a sample to verify that it works.
And I am compelled to use EJB 2.x specifications (not sure whether annotations could be used with it)

Regards,
Giriraj.
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have information of the JNDI provider for the RMI registry binding?

I am using glassfish server and I am able to perform that lookup by passing the fully qualified class name of the interface deployed as a session bean (this is the default behavior in my environment). So I suggest that you refer and consult the documentation of the application server regarding the specific provider.

Regards.

 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure whether this has something to do with Websphere.
But I got around this with following settings.
In my web project I added an ejb reference in web.xml (through RAD 7) and then I used ejb reference defined here for the lookup purpose in the servlet.

In web.xml:



This also resulted in following entry being added in ibm-web-bnd.xmi

In ibm-web-bnd.xmi:



And then I used following string for lookup
"java:comp/env/ejb/MyBeanOne" where "ejb/MyBeanOne" was taken from web.xml entry.

And voila!

Regards,
Giriraj.


 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the intended behavior because you registered it in the deployment descriptor (web.xml)



Try altering this part and see it the lookup still succeeds.

Regards.
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Altering ejb reference and keeping the lookup as it is resulted in the same naming exception.

Is there any other way i can lookup the ejb without defining it in web.xml i.e. without mentioning it in the web module anywhere.
I suppose there should be some way to do this since both the ejb project and the web project are the part of same ear.

Regards,
Giriraj.
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will depend on the application and provider that binds session beans in the RMI registry.

Try the following -

1. Make a servlet
2. Declare the session bean as a instance variable of the servlet
3. Mark it with @EJB annotation
4. Package the WEB and EJB component into an EAR
5. Remove the <ejb-ref> element in web.xml

Let us see if dependency injection succeeds.

Regards.
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does DI work in EJB 2.x?
I am not sure if i can use @EJB in this.
Please advise.

Regards,
Giriraj.
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. Dependency injection works only in EJB 3.

Regards.
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this mean there is no other way to access ejb apart from defining it in web.xml in EJB 2.x.
Is DI the only way out?

Regads,
Giriraj.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Giriraj,

Each application server binds the EJB to an default JNDI name , if you don't specify your own. The first thing you could do is, check the WebSphere documentation to figure out, how to view the JNDI tree. Once you access the JNDI tree, you can then check for the JNDI name of the bean. Then use this JNDI name in your lookup code. If you are still having trouble with this, please post the entire exception stacktrace and also the JNDI tree view output.

Also, remember that the java:comp/env namespace is per component - which means that if you are binding the bean to java:comp/env of one web-application, then you can only lookup the bean in this namespace through servlets belonging to this web-application.
 
Giriraj Bhojak
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I did now is I tried to access the ejb by using fully qualified class name prefixed by string "ejb/" and the lookup succeeded.
Thanks Jaikiran for pointing out that java:comp/env namespace is per component. It really helped me a lot.


Regards,
Giriraj.

reply
    Bookmark Topic Watch Topic
  • New Topic