aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes @EJB 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 "@EJB" Watch "@EJB" New topic
Author

@EJB

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263

///////////////////////// code 1/////////////////////////////////
@EJB
private DocumentSession documentSessionLocal;
//////////////////////////////

////////////////////////////////code 2/////////////////////////
@EJB(name="ejb/EmplRecord", beanInterface=EmployeeRecordHome.class)
@Stateless
public class EmployeeServiceBean implements EmployeeService {
public void changePhoneNumber(...) {
// Obtain the default initial JNDI context.
Context initCtx = new InitialContext();

// Look up the home interface of the EmployeeRecord
// enterprise bean in the environment.
Object result = initCtx.lookup("java:comp/env/ejb/EmplRecord");

// Convert the result to the proper type.
EmployeeRecordHome emplRecordHome = (EmployeeRecordHome)
javax.rmi.PortableRemoteObject.narrow(result, EmployeeRecordHome.class);
...
}
}
///////////////////////////////////////code 2 end//////////////////////////////////////

Q1 There are two different codes i am having here.. code 1 is used to injection ejb reference to a variable if that is the case then why we need to do look up as done in code 2. can't we just inject ejb reference in the variable and straight away used that.

Q2 Do we still have to use portableremoteobject.narrow ?

Ross Crockett
Ranch Hand

Joined: Aug 25, 2009
Posts: 30
Q1 There are two different codes i am having here.. code 1 is used to injection ejb reference to a variable if that is the case then why we need to do look up as done in code 2. can't we just inject ejb reference in the variable and straight away used that.


Code 2 is showing the alternative way to get reference to an EJB Bean instead of using Dependency Injection. This example shows a look-up done using JNDI. As to why you would want to that...apart from just for fun, it could be that the class that your trying to get the EJB reference in isn't container managed, hence can't be injected.

Q2 Do we still have to use portableremoteobject.narrow ?


Only when getting reference to beans developed using the EJB 2.1 specification. EJB 3.0 beans can retrieved similarly via JNDI doing something like:

Context context = new InitialContext();
MyBean bean = (MyBean) context.lookup(java:comp/env/ejb/MyBean);

If you want to avoid the nasty EJB 2.1 interoperability code, you can still use dependency injection to get 2.1 beans using the @EJB annotation, as long as the class your getting the reference in is container managed (e.g. a stateless bean, MDB, Servlet etc).
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
thanks for your reply but if you see code 2 there is a @ejb annotation over there. IF we are using jndi lookup then why we need @ejb in code2.
Ross Crockett
Ranch Hand

Joined: Aug 25, 2009
Posts: 30
Hey Nitin,
You can use this annotation in non-container managed classes to get a reference to the bean, then use JNDI to look it up. The @EJB annotation at class level gives you a reference to the EJB, similar to the <ejb-ref> element you find in the descriptor. This puts the bean in the JNDI tree from which you can then use look-up code to actually get the instance.
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
hi ross,
fog is still not clear ;)

I got your point but if you see code2 which is a stateless bean which means it is managed by container so why we need @ejb annotation here then. One more question if you are saying it will put in JNDI tree. aren't it supposed to put in jndi tree when we deploy application?
so you mean if i don't do @ejb in code 2 and try to lookup Object result = initCtx.lookup("java:comp/env/ejb/EmplRecord"); then it will not work?
Ross Crockett
Ranch Hand

Joined: Aug 25, 2009
Posts: 30
I might handball you to section "16.5 EJB References" in the ejb3-core spec. It should answer most of your questions in greater detail that what I can post in this forum
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: @EJB