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

@Resource question

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi

I have a question regarding the following code snippet, what does this code snippet ?



My answer is :
will inject in the billingQueue field the resource with the name "jms/OrderBillingQueue" and will assign the "OrderBillingQueue" name to this resource JNDI name- that means some other bean (or managed class) can
do a jndi lookup for this resource.

Is my answer correct ? and if is not what is wrong ?

Regards,
M


SCJP, SCJD, SCWCD, OCPJBCD
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
yes, your answer is correct...


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

will inject in the billingQueue field the resource with the name "jms/OrderBillingQueue" and will assign the "OrderBillingQueue" name to this resource JNDI name- that means some other bean (or managed class) can
do a jndi lookup for this resource.


I am skeptical about this but i think you might need to go through this document.

Regards
Sudhakar


Regards
Sudhakar
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166



To put it in simple words - An object (of type Destination) will be injected from jndi name "OrderBillingQueue" to the ENC java:comp/env/jms/OrderBillingQueue. This way any unmanaged object (in this module) can then do a lookup using the ENC name java:comp/env/jms/OrderBillingQueue


[My Blog] [JavaRanch Journal]
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

To put it in simple words - An object (of type Destination) will be injected from jndi name "OrderBillingQueue" to the ENC java:comp/env/jms/OrderBillingQueue. This way any unmanaged object (in this module) can then do a lookup using the ENC name java:comp/env/jms/OrderBillingQueue


Thanks Jaikiran for your explanations.

In what case the two parameters(name and mappedName) are mandatory?

Regards
Sudhakar
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

Originally posted by Karnati Sudhakar:
In what case the two parameters(name and mappedName) are mandatory?

Regards
Sudhakar


None of these attributes are mandatory. Both are optional. This Annotation reference has more details (look for javax.annotation.Resource in that document).
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Guys and thanks for your answers.

IF I understand well the code snippet inject the resource identified after its jndi name in the the given filed.
After this some unmanaged object can search for the injected resource using the ENC.
So this is like some sort of mapping between the resource and the ENC.
Is this right ?

If the answer is yes why ? Which is the practical purpose for such a mapping ? By example : who want to access the same resource that is injected in a certain bean ?


Regards,
M
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

Originally posted by Mihai Radulescu:

Which is the practical purpose for such a mapping ?


Consider this example - You have a servlet which calls some (unmanaged) XYZ class which further calls a (unmanaged) MessageSender class which sends an message to a JMS queue. Its not advisable to keep passing the JMS queue from the servlet to the MessageSender class and the MessageSender is not a managed class so you can't even inject the queue. Instead, you inject the queue in the ENC of the servlet (as explained in the example above). Then later during the flow through the servlet, the MessageSender can lookup the queue in the servlet's ENC. You might ask me why not directly lookup the queue using the JNDI (mappedName) name from the MessageSender. If interested, see this

This entire example is exactly what the resource-ref entry is meant to do through xml files.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Jaikiran

Thanks for your answer.
As far as I understand the "mappedName" are (vendor spacific) bad for portability. That why is recommendable not to use them.
I agree with this but I find a little bit odd.

I must do a mapping in to a class and to use this mapping in other class - with other words the two classes must have knowledge about each other. It is like the class which do the mapping (the managed one) has a method which expose the resource (in your example the JMS Query). In this case the unmanaged class needs the instance of the managed and method name to access the resource. If I use the managed class and the name mapping from the unmannaged class I only need to knoe the name for the JNDI lookup - preate good compromise.


Regards,
M
[ October 30, 2008: Message edited by: Mihai Radulescu ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: @Resource question