• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

@EJB

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

///////////////////////// 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 ?

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

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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic