File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Get EJB name from within the EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Get EJB name from within the EJB" Watch "Get EJB name from within the EJB" New topic
Author

Get EJB name from within the EJB

Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Why would you want that, you may ask...
I have two MDBs that originate from the same source file. Either I keep and maintain twice the same code, or for the only difference that is required between them I do something to determine which one is executed currently.

So, how do I retrieve the MDB name from within the MDB with java code?

Thanks
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Greg Triant wrote: I have two MDBs that originate from the same source file

This is not clear to me. Do you mean to say that you have 2 classes in a single java file and each class is a MDB?

Even if that is the case, you don't need to write the same code twice and neither would you need the name of the bean.
Just put the common code in a separate class and call a method inside that class from both the places. The method should take as parameters what you think would vary between the 2 calls.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Simple: Two deployment descriptors, looking at the same Java source code => two different MDBs implementing the same functionality.
Why this is better practice than your suggestion has to do with the application. But it is for several reasons.

So the question remains....
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

You need to provide us more details. What is the version of EJB you are using? Also, what do you mean by ejb name? do you mean the JNDI name?
Also, provide code sample of what you have along with deployment descriptors.
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Here are (parts of) the deployment descriptors of the two MDBs (EJB 2.0)

1)
<enterprise-beans>
<message-driven>
<ejb-name>MyQueueEjb</ejb-name>
<ejb-class>mdb.MyQueueBean</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
</message-driven>
</enterprise-beans>

2)
<enterprise-beans>
<message-driven>
<ejb-name>MyRedeliveryQueueEjb</ejb-name>
<ejb-class>mdb.MyQueueBean</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
</message-driven>
</enterprise-beans>


These are two different MDBs "looking" to the same java class.
So I am actually asking if from within this java class: MyQueueBean I can access the information of the <ejb-name> on runtime.

Thank you
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

ah I see. Now, its clear.
There is a <env-entry> tag available which can be added to each mdb entry. So, you can follow a kind of trick where you provide the same name in both entries and different values.
You can then lookup this env entry in your code and use it.

However, I still feel you should consider the common code approach I suggested earlier.

Or even better: Have an abstract class that has all the common stuff coded. Then you can create 2 MDB classes which extend this class and re-use code with only the different parts implemented. You could consider implementing this with the Template Method pattern.
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Hi,

Regarding your suggestions, you are completely right but they are not really an option right now for several reasons.

Would you please specify how the solution with <env-entry> tag would go with code, assuming that I make the change in my DD?

Thanks again
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

You can get the value via lookup:

where envVarName is the name that you have provided for the env-entry-name tag.
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Wont the two values in the JNDI tree override each other?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

That is a good question. But, the <env-entry> is supposed to be unique only within the session/entity/mdb nodes as per the spec.
Actually, there was earlier a method named getEnvironment() in the EJBContext object - but, that was deprecated later and the recommendation was to use lookup. Note that you are using the lookup from within the bean method, so I think that should work. I have never tried it though.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
160

Each bean will have its own unique java:comp/env namespace. So each of those env-entry values will be different when different beans are invoked (even if the classname of the bean is same). So java:comp/env/foo lookup is the right way to do it.

[My Blog] [JavaRanch Journal]
Greg Triant
Greenhorn

Joined: Nov 25, 2007
Posts: 28
Worked like a charm.
Thank you for your help.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Cool.

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get EJB name from within the EJB
 
Similar Threads
java:comp/env/ejb/jndi-name.
Bean's environment question
JNDI Question...
ReadLock/WriteLock from within EJB
The place where we define Annotations inside the EJB Bean class