*
The moose likes EJB and other Java EE Technologies and the fly likes JNDI Lookup/ejb-jar.xml to find an another ejb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "JNDI Lookup/ejb-jar.xml to find an another ejb" Watch "JNDI Lookup/ejb-jar.xml to find an another ejb" New topic
Author

JNDI Lookup/ejb-jar.xml to find an another ejb

SU Lingam
Greenhorn

Joined: Feb 09, 2005
Posts: 6
Hi,

When a EJB wants to talk to an another bean,we always do a JNDI lookup to get the Bean's Home Interface,then the home's create to get the componenet interface.
In the deployment descriptor do we need to specify the <ejb-ref> tag inorder for a EJB to talk to another EJB.Is this required only when the two ejb's have seperate DD's.If the two EJB's are defined in the same ejb-jar.xml then we wouldn't need the <ejb-ref> tag?Am I right?

Why do we need this ejb-ref tag at all,when we always locate a Bean using the JNDI lookup?

Any insights is really appreciated.

Thanks
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi SU,


In the deployment descriptor do we need to specify the <ejb-ref> tag inorder for a EJB to talk to another EJB.Is this required only when the two ejb's have seperate DD's.If the two EJB's are defined in the same ejb-jar.xml then we wouldn't need the <ejb-ref> tag?Am I right?

Actually you�re not. They are not required but they are good practice though. Without ejb references, EJB components will be highly coupled enforcing the code in your EJBs to know the JNDI name for all referenced EJBs, in order to do the lookup. If later on the referenced beans' JNDI name will change (as an example consider versioning where the bean could have names like /ejb/1.0/AccountHome, which might change from version to version), then you have to change your code accordingly, recompile and redeploy the beans, etc. Sure you might have the JNDI names configuration driven, but ejb reference on the other hand will always be the best approach.
Regards.


I think, therefore I exist -- Rene Descartes
SU Lingam
Greenhorn

Joined: Feb 09, 2005
Posts: 6
Hi,

Thanks for the reply.

Well the value of the <ejb-ref> tag corresponds to the ejb-name of a Bean.Isn't?Well it's tightly coupled even then!!If the ejb-name of a bean
changes then you still have to change the ejb-ref??
The purpose of the ejb-ref tag shud have been I guess to be able to directly access the Home without going thru a lookup process.With the JNDI
Name we can go ahead directly accessing the Bean's Home,without having to
specify <ejb-ref> blocks.If the bean references 6-7 beans,the DD then becomes fairly complex and you still need to do the JNDI lookup for all the beans.That gives me the thinking why go for ejb-ref tags.

Thanks
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi SU,

Well the value of the <ejb-ref> tag corresponds to the ejb-name of a Bean.Isn't?

That�s correct.

Well it's tightly coupled even then!!If the ejb-name of a bean
changes then you still have to change the ejb-ref??

Not quite. The ejb-name of the bean is just a symbolic name and doesn�t bear the same naming restrictions like the jndi name. Moreover it shouldn�t change at all during the entire application lifetime. I personally cannot see any good reason why you should change the ejb-name. Can you think of any?

The purpose of the ejb-ref tag shud have been I guess to be able to directly access the Home without going thru a lookup process.

I�m happy you raised this issue and I�ll go even further saying that EJBs should not be looked-up to jndi at all. However in order to understand the importance of ejb-refs I�ll advice you to search the net for the Environment Naming Context (ENC).

With the JNDI
Name we can go ahead directly accessing the Bean's Home,without having to
specify <ejb-ref> blocks.

Please understand the difference. jndi names are not easy to manage and they might change over time much more often than the ejb-names (which shouldn�t change at all). If you�d like we can go through the versioning example in more detail and try to understand the issue from a maintenance point of view, when ejbs are accessed through references vs jndi names.

If the bean references 6-7 beans,the DD then becomes fairly complex and you still need to do the JNDI lookup for all the beans.That gives me the thinking why go for ejb-ref tags.

You�re very right here and this is a common drawback of using ejb references. This also might give you the reason why ejb-names should not change at all. However it is a fait trade if you�d consider the headache to maintain a system where the jndi names of your beans changes over time. On the other hand using tools like EJBGen or XDoclet might help you managing the references within your descriptors.
Regards.
SU Lingam
Greenhorn

Joined: Feb 09, 2005
Posts: 6
Hi VT,

>>Please understand the difference. jndi names are not easy to manage and they might change over time much more often than the ejb-names (which shouldn�t change at all). If you�d like we can go through the versioning example in more detail and try to understand the issue from a maintenance point of view, when ejbs are accessed through references vs jndi names.

Yes..I would definitely would like to gain some perspective on this.Let's go thru an example.

Thanks for your insights.
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi SU,

For simplicity let�s suppose we have deployed the application as an ear that contains at least an entity bean. The Account ejb will be accessed by other j2ee components, which could be either other ejbs, jsps, or servlets. Let�s take a look at the configuration:

In this approach the client (jsp, servlet, other ejb, etc) will lookup the Account bean like this:

The same could be achieved doing a global lookup:

So far so good and both solutions seems to be pretty identical. Morover the global lookup doesn�t require any extra configuration stuff. Now imagine that after three months a new release of the application is required. This time the Account bean will be upgraded and a new version 1.1 of this ejbs needs to be deployed with the new ear. Also let�s suppose that the requirements enforce deploying both versions to the same cluster (as you know beans cannot be bound to the jndi tree with the same names and this is a common technique for versioning ejbs). Hence the Account bean will be reconfigured as followed:

If your clients looked up the bean using the references then your code is safe and no changes are required. As you might see the first lookup will always work. Using global jndi names on the other hand will enforce changing the client code as well.
I hope this will help.
Regards.
SU Lingam
Greenhorn

Joined: Feb 09, 2005
Posts: 6
I really appreciate, you taking your time explaining abt <ejb-ref> tag over JNDI tag.Sure did gain some perspective...

Thanks
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
You're most welcome SU
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JNDI Lookup/ejb-jar.xml to find an another ejb
 
Similar Threads
weblogic-ejb-jar.xml prblems ....
relation between ejb--jar.xml and web.xml?
How ejb-ref works, when we have to ejb's deployed on two diffrent machines???
DI with @Resource adnotation understanding
Need help on EJB2.0 Local interfaces