jsr343-experts@jms-spec.java.net

[jsr343-experts] (JMS_SPEC-121) Injection of JMSContext objects not possible in a WebSocket @OnMessage or @OnClose callback method

From: Nigel Deakin <nigel.deakin_at_oracle.com>
Date: Tue, 07 May 2013 14:27:22 +0100

I have logged this JIRA issue:

https://java.net/jira/browse/JMS_SPEC-121
Injection of JMSContext objects not possible in a WebSocket @OnMessage or @OnClose callback method

John and I have started to discuss this on the issue, so I'll start a thread here.


---------------------------------------------------------------------------------------------------

                  Key: JMS_SPEC-121
                  URL: https://java.net/jira/browse/JMS_SPEC-121
              Project: jms-spec
           Issue Type: Bug
     Affects Versions: 2.0
             Reporter: Nigel Deakin


If you inject a {{JMSContext}} into a websocket {{ServerEndpoint}} you can use it in the {{_at_OnOpen}} callback, but if
you attempt to use it from the {{_at_OnMessage}} or {_at_OnClose}} callbacks you get an exception from the CDI container
because there is no valid request scope or transaction scope. From a user perspective it would seem reasonable to expect
to be able to use an injected {{JMSContext}} in these places.

Here's an example of a websocket {{ServerEndpoint}} that injects a {{JMSContext}}:

@ServerEndpoint("/websocket")
public class SampleWebSocket {

     @Resource(lookup="java:comp/DefaultJMSConnectionFactory") ConnectionFactory cf;
     @Resource(lookup = "jms/myQueue") Queue myQueue;

     @Inject
     private JMSContext jmsContext;

     @OnOpen
     public void onOpen(final Session session) {
         // works OK as there is a valid request scope here
         context.createProducer().send(myQueue, message);
     }

     @OnMessage
     public void onMessage(final String message, final Session client) {
         // fails as there is no valid request scope here
         context.createProducer().send(myQueue, message);
     }

     @OnClose
     public void onClose(final Session session) {
         // fails as there is no valid request scope here
         context.createProducer().send(myQueue, message);
     }