• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt about getting a Queue reference using Resource annotation

 
Greenhorn
Posts: 22
PHP Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have seen in some places that people using different attributes for getting a queue reference using Resource annotation. I have seen the following attributes:

@Resource (name = "....")
@Resource(mappedBy = "....")
@Resource(lookup = "....")

Which one is the recommended?? Because I could get wrong in the exam. And in the enthuware mock exam for EJB, They recommend that I should use lookup attribute for java portable jndi (java:global, java:module, and java:app) and not for getting a reference of a Queue.

Greets.

 
Bartender
Posts: 2442
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On Frit's notes, p.30,


@Resource (lookup="jms/websendqueue")
Queue queue;



I assume lookup is the way to refer to a queue.

On p.32,


@MessageDriven( ... mappedName="jms/websendqueue")
....



The message driven bean is bound to a resource, which is a queue. And this queue is named to a application server specific name "jms/websendqueue".
Correct me if I am wrong.
 
Himai Minh
Bartender
Posts: 2442
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may see some more discussion here:
https://coderanch.com/t/428944/java-EJB-SCBCD/certification/mappedName-attributes
 
Jhon Gonzales
Greenhorn
Posts: 22
PHP Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information @Himai Minh and the link.

I get it. The correct (because is the portable way) way for getting a Queue destination is using the lookup attribute.

mappedBy will works in some application server and in other it will not work.

The name for getting a Queue destination will works in some application server like Glassfish. In glassfish if you only supply the name attribute and that name is the same of a jndi direction of a Queue destination, glassfish will use it.

Regards.
 
Himai Minh
Bartender
Posts: 2442
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Edwin and everyone.
I am reading EJB in Action. On p.133,


@Resource annotation elements:
name - besides performing an injection, the @Resource annotation implicitly creates a binding referring to the injected resource in the java:comp namespace. This is primarily done for backward compatibility. This attribute allows you to specify the name that is used for the implicit binding. This is equivalent t the <res-ref-name> element in deployment descriptors used extensively in EJB2.


My comment : I guess @Resource(name = ...) is not commonly used with EJB 3.1 unless it is used for backward compatibility with EJB 2.0.


lookup - This is the actual JNDI lookup name of the resource to be injected. This is likely the attribute you will use the most.


My comment: I guess it is always the right choice to use lookup for looking up the JNDI name of a queue, DataSource, Session and etc.

Example on p.134-135:


1. @Resource (lookup="java:global/jdbc/ActionBazaarDB")
private DataSource dataSource;

2. @Resource (lookup="java:global/jms/ActionBazaarQueue")
private Queue queue;

3. @Resource (lookup="java:global/mail/ActionBazaar")
private javax.mail.Session mailSession;



Note:


@Resource
private EJBContext context;

Note that the injected session context isn't stored anywhere in JNDI. In fact, it would be incorrect to try to specify JNDI lookup parameters in this case at all, and servers will probably ignore the element if specified.




mappedName - A vendor-name for the bean.


Example on p.21 of EJB 3.1 Cookbook by Richard Reese


The connection factory was created by the server GlassFish in this case, with the name jms/SalutationQueueFactory which is of type QueueConnectionFactory.
@Resource (mappedName ="jms/SalutationQueueFactory")
private QueueConnectionFactory queueConnectionFactory;

The queue is of type javax.jms.Queue and used the name jms/SalutationQueue as defined within the server. @Resource annotation was used again to inject this resource.
@Resource(mappedName="jms/SalutationQueue")
private Queue queue;



My comment: The application server, GlassFish , creates the resource QueueConnectionFactory with the name jms/SalutionQueueFactory and Queue with the name jms/SalutationQueue first before the developer can inject them.
Also, these name jms/... are not portable JNDI name and vendor-specific. According to http://thegreyblog.blogspot.com/2010/09/introduction-to-ejb-30-injection-and.html, in EJB 3.1, mappedName is becoming deprecated.


mappedName
The EJB 3.0 Specification defines mappedName as a "product-specific name that the session bean should be mapped to." Often, application server use mappedName to map a session bean to a global JNDI name. The EJB 3.1 specification sort of deprecates the mappedName element and introduces the concept of "portable global JNDI name".




These are just my comments. Correct me if I am wrong.

 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a previous discussion about a similar topic here https://coderanch.com/t/164146/java-EJB-SCBCD/certification/Resource. See if it helps.
 
Himai Minh
Bartender
Posts: 2442
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Pai.
From the previous example:


Do you mean this story:
inject a Destination object from the application server and this Destination object's name is mapped to OrderBillingQueue (the one specified by mappedName attribute) in the server.
Once this object is injected and refered by the billingQueue reference, it will be called jms/OrderBillingQueue (the one specified by the name attribute) locally
?


I believe @Resource (lookup= "java:global/jms/OrderBilingQueue) is more commonly used.
So, is lookkup a replacement of mappedName?
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, is lookkup a replacement of mappedName?


Yes, this is the portable replacement.

In analogy of Jaikiran's example using a mappedName and name attribute you can have an @Resource with a lookup and a name attribute.

(Example from my notes ch. 7.5.2 name vs. lookup):
You can make a resource in the global JNDI (lookup=”jms/websendqueue”) available under another name (name=“q”) in the bean class.
 
Himai Minh
Bartender
Posts: 2442
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Frits.Thanks for your reply.
As I read your notes and other materials,
if we have :

I realized lookup = jms/OrderBillingQueue is the name of the resource in the JNDI directory, it is "java:global/jms/OrderBillingQueue" as it is a global JNDI name.
The name = OrderBillingQueue is name referring to this resource for developer to lookup



Or, if we have :

I realized mappedName = jms/OrderBillingQueue is the name of the resource in the specific application server's JNDI directory, it may be "java:global/jms/OrderBillingQueue" if it is a global JNDI name .
The name = OrderBillingQueue is name referring to this resource for developer to look for

Correct me if I am wrong.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic