This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Resource question

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, your answer is correct...
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic