By mistake, I have posted this message in SCWCD thread. I'm just creating a copy here. I need help on local interfaces.
------ 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?
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.
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.
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 ]
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?