Skip to main content

Resource injection in servlet (JMS)

2 replies [Last post]
oliechti
Offline
Joined: 2003-06-09

Hello,

I would like some advice regarding the use of resource injection in servlets, and in particular for JMS resources.

What I would like to do, is to develop a Web Application (war) and have one servlet use a connection factory and a queue defined in Glassfish. I thought that I should do the following:

- in Glassfish, create a physical queue, the ConnectionFactory and the Queue
- in my servlet, inject dependency on the resources with the following code:

@Resource(name = "jms/ProcessingQueue")
private Queue processingQueue;
@Resource(name = "jms/ProcessingQueueFactory")
private ConnectionFactory cf;

- in the deploymen descriptors, (web.xml and sun-web.xml), do nothing.

I tried this, but got the following exception:

com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Message-Destination-Ref jms/ProcessingQueue@java.lang.String@null into class ch.unifr.ase.TestServlet

I have actually found two ways to fix the problem, but I don't completely understand what is happening (not sure if it is the expected behavior or a bug) and I am looking for advice on the question.

The first way to fix the problem is to use "mappedName" instead of "name" in the annotation.
The second way is to add an entry in sun-web.xml, to declare the mapping between the content of the "name" field in the annotation and the JNDI name of the Queue in Glassfish. (in my case, a mappting between "jms/ProcessingQueue" and "jms/ProcessingQueue")

I have to admit that I don't completely grasp the use of "mappedName"... I know that it is used to specify "application-server specific" ways to name components (and thus should be avoided to ensure portability). The Java EE tutorial uses this field in the JMS examples.

Any comment/suggestion would be greatly appreciated :-)

Cheers,

Olivier

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
fvu
Offline
Joined: 2007-07-19

> The first way to fix the problem is to use
> "mappedName" instead of "name" in the annotation.

Correct, but not portable

> The second way is to add an entry in sun-web.xml, to
> declare the mapping between the content of the "name"
> field in the annotation and the JNDI name of the
> Queue in Glassfish. (in my case, a mappting between
> "jms/ProcessingQueue" and "jms/ProcessingQueue")

Correct, and portable.

> The Java EE tutorial uses this field in the JMS
> examples.

Indeed, IMO an unfortunate decision :-(

The best explanation of this topic I know can be found here:
https://glassfish.dev.java.net/javaee5/ejb/compdependencies_xmlforum_nov...

fvu

Sivakumar Thyagarajan

> The first way to fix the problem is to use "mappedName" instead of "name" in
> the annotation.

Yes, this is right. Please see Ken's response at
http://forums.java.net/jive/thread.jspa?messageID=213415 for a detailed
explanation on using mappedName. mappedName is the vendor-specific
representation of your global JNDI resource and using it is not equivalent to
using a non-portable "vendor-specific" API for binding to a global JNDI resource.

> The second way is to add an entry in sun-web.xml, to declare the mapping
> between the content of the "name" field in the annotation and the JNDI name
> of the Queue in Glassfish. (in my case, a mappting between
> "jms/ProcessingQueue" and "jms/ProcessingQueue")

If you do not want the resource mapping to be done in the bean-code itself via
mappedName, sun-web.xml is the alternative to tie the local JNDI reference to
the global JNDI resource, but this would result in one-additional
vendor-specific deployment descriptor [sun-web.xml] to be added to your
deployment archive.

HTH,
Thanks
--Siva.

glassfish@javadesktop.org wrote:
> Hello,
>
> I would like some advice regarding the use of resource injection in servlets, and in particular for JMS resources.
>
> What I would like to do, is to develop a Web Application (war) and have one servlet use a connection factory and a queue defined in Glassfish. I thought that I should do the following:
>
> - in Glassfish, create a physical queue, the ConnectionFactory and the Queue
> - in my servlet, inject dependency on the resources with the following code:
>
> @Resource(name = "jms/ProcessingQueue")
> private Queue processingQueue;
> @Resource(name = "jms/ProcessingQueueFactory")
> private ConnectionFactory cf;
>
> - in the deploymen descriptors, (web.xml and sun-web.xml), do nothing.
>
> I tried this, but got the following exception:
>
> com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Message-Destination-Ref jms/ProcessingQueue@java.lang.String@null into class ch.unifr.ase.TestServlet
>
> I have actually found two ways to fix the problem, but I don't completely understand what is happening (not sure if it is the expected behavior or a bug) and I am looking for advice on the question.
>
> The first way to fix the problem is to use "mappedName" instead of "name" in the annotation.
> The second way is to add an entry in sun-web.xml, to declare the mapping between the content of the "name" field in the annotation and the JNDI name of the Queue in Glassfish. (in my case, a mappting between "jms/ProcessingQueue" and "jms/ProcessingQueue")
>
> I have to admit that I don't completely grasp the use of "mappedName"... I know that it is used to specify "application-server specific" ways to name components (and thus should be avoided to ensure portability). The Java EE tutorial uses this field in the JMS examples.
>
> Any comment/suggestion would be greatly appreciated :-)
>
> Cheers,
>
> Olivier
> [Message sent by forum member 'oliechti' (oliechti)]
>
> http://forums.java.net/jive/thread.jspa?messageID=238818
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net