Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Need help on EJB2.0 Local interfaces

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

By mistake, I have posted this message in SCWCD thread. I'm just creating a copy here. I need help on local interfaces.

Thank you.

------
Hello there,

I am new to EJB and I am trying out Local home and component interfaces. According to the book that I have read, for one EJB, you can expose both local and remote home/component interfaces. Does this mean we would only have one JNDI for this EJB? Whether the local or remote interfaces will be used, will depend if you narrow or downcast? (Please see below code)



But when I do the second one (ordinary type cast) from another EJB to get my bean's local home, I encounter a ClassCastException. What could have gone wrong?

Thank you for your help.

J.M.
SCJP2
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really confusing for most of us as the book does not cover it in good detail.

Does this mean we would only have one JNDI for this EJB?


Yes. Even if your bean has both local and remote interfaces, there will be only one JNDI name.

Whether the local or remote interfaces will be used, will depend if you narrow or downcast?



Not exactly. To get the local interface of an EJB, you need to have the ejb-local-ref element defined in the deployment descriptor. This element will map the JNDI name you use to lookup to the actual bean name.

For instance,
If Bean B needs Bean A's local interface, Bean B should define ejb-local-ref in its deployment descriptor.

To get the remote interface of an ejb, you need to have ejb-ref element defined in your deployment descriptor.

Example:


To lookup remote interface, use jndi.lookup("java:comp/env/BeanARemoteRef") and then narrow the stub to get the remote home object.

To lookup local interface, use jndi.lookup("java:comp/env/BeanALocalRef") and then type cast the object returned to get the local home object.
 
Joshua Masuki
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keerthi,

Thanks so much for the help. I'll it out and hopefully this works for me this time.

- JM
 
Joshua Masuki
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It seems not to be working on my end. I still encounter ClassCastException. Can somebody please help?

In this example of mine, the client uses PermutationBean (my 1st bean), which uses FactorialBean (my 2nd bean). MyPermutation and MyFactorial are JNDI names for PermutationBean and FactorialBean, respectively.

Here is my DD:



Here is a code snippet of my first EJB, PermutationBean:



And here's the code snippet of my client:



Thank you very much.

- JM
[ February 28, 2005: Message edited by: Joshua Masuki ]
 
Keerthi P
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change Object obj = ctx.lookup("MyFactorial"); to Object obj = ctx.lookup("java:comp/env/MyFactorial");

It should work.
 
Keerthi P
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ctx.lookup("MyFactorial") tries to look up the remote home that is not type compatible with your client code. Your client code gets the stub of remote which is why you get ClassCastException.
 
Joshua Masuki
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Apparently, using Object obj = ctx.lookup("java:comp/env/MyFactorial") does not work, I got NamingException. My JNDI name for FactorialBean is "MyFactorial", so I don't think "java:comp/env/MyFactorial" would work.

What do you think is other cause of problem? How can I make this work?

Thank you so much!

- JM
 
Keerthi P
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. My mistake. I did not read the ref-name properly from you DD.

<ejb-local-ref>
<ejb-ref-name>FactorialBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>practice.stateless.FactorialHomeLocal</local-home>
<local>practice.stateless.FactorialLocal</local>
<ejb-link>FactorialBean</ejb-link>
</ejb-local-ref>

You should use the ejb-ref-name in your lookup code. Use "java:comp/env/FactorialBean".
 
Joshua Masuki
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It's working now. Thank you so much!

- JM
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

for accessing other bean from bean,
"java:comp/env/ejb/BeanName" should be JNDI context.
"java:comp/env/" is context for enviroment entities.

whatever you have in java:come/env is not a home interface for your bean and heance your getting ClassCastException.
Amol.
 
reply
    Bookmark Topic Watch Topic
  • New Topic