wood burning stoves*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Need help on EJB2.0 Local interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Need help on EJB2.0 Local interfaces" Watch "Need help on EJB2.0 Local interfaces" New topic
Author

Need help on EJB2.0 Local interfaces

Joshua Masuki
Greenhorn

Joined: Feb 14, 2005
Posts: 8
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
Keerthi P
Ranch Hand

Joined: Aug 19, 2003
Posts: 203
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.


Cheers.<br />Keerthi<br />(SCJP, SCWCD, SCBCD)
Joshua Masuki
Greenhorn

Joined: Feb 14, 2005
Posts: 8
Hi Keerthi,

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

- JM
Joshua Masuki
Greenhorn

Joined: Feb 14, 2005
Posts: 8
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

Joined: Aug 19, 2003
Posts: 203
Change Object obj = ctx.lookup("MyFactorial"); to Object obj = ctx.lookup("java:comp/env/MyFactorial");

It should work.
Keerthi P
Ranch Hand

Joined: Aug 19, 2003
Posts: 203
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

Joined: Feb 14, 2005
Posts: 8
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

Joined: Aug 19, 2003
Posts: 203
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

Joined: Feb 14, 2005
Posts: 8
Hi,

It's working now. Thank you so much!

- JM
amol deshpande
Ranch Hand

Joined: Sep 05, 2003
Posts: 162
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.
 
GeeCON Prague 2014
 
subject: Need help on EJB2.0 Local interfaces