Thursday, 9 May 2013

How can I call one EJB from inside of another EJB? | EJB Interview Questions

In case of Remote :
EJBs can be clients of other EJBs. It just works. Really. Use JNDI to locate the Home Interface of the other bean, then acquire an instance reference.
For Example : 
Context ctx = new InitialContext();
//get Home interface of bean
//narrow -retype
EmpHome lhome = (EmpHome ) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("java:comp/env/LocalEmp"), EmpHome .class);
//get remote interface
Emplbean = lhome.create();
//now you can call bussiness method on remote interface like
lbean.doSomething()

Incase of Local : 
but we no longer have to worry about the PortableRemoteObject, as the bean is no longer remote
Context ctx = new InitialContext();
Object o = ctx.lookup("java:comp/env/LocalEmp");
EmpHome empHome = (EmpHome)o;

No comments: