Resource injection in servlet (JMS)
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 :-)