aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Doubt about getting a Queue reference using Resource annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Doubt about getting a Queue reference using Resource annotation" Watch "Doubt about getting a Queue reference using Resource annotation" New topic
Author

Doubt about getting a Queue reference using Resource annotation

Edwin Gonzales
Greenhorn

Joined: Jul 22, 2013
Posts: 12

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.

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
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
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
You may see some more discussion here:
http://www.coderanch.com/t/428944/java-EJB-SCBCD/certification/mappedName-attributes
Edwin Gonzales
Greenhorn

Joined: Jul 22, 2013
Posts: 12

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
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
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.

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10205
    
166

There's a previous discussion about a similar topic here http://www.coderanch.com/t/164146/java-EJB-SCBCD/certification/Resource. See if it helps.

[My Blog] [JavaRanch Journal]
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
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?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

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
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt about getting a Queue reference using Resource annotation