jsr343-experts@jms-spec.java.net

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

From: John D. Ament <john.d.ament_at_gmail.com>
Date: Tue, 7 May 2013 09:40:11 -0400

(glad my name displays better now - yay) Right, so in general my feedback
on this is that it's not a JMS spec issue. The problem I see from reading
through the WebSocket spec is that they don't define any CDI contexts that
should be activated. They do correctly identify that the beans should be
CDI/Managed Bean enabled - that's why people see OnOpen (a post construct
callback) work but not OnMessage. The issue isn't unique to JMS either -
anything that is created as @RequestScoped isn't active in this method
invocation. Dependent probably works, haven't tried it yet. You should
get something working if the endpoint is a session bean also.

If WebSocket wants contexts to be enabled for these method invocations, it
needs to be stated in their spec the same way JMS has clearly defined this.

John


On Tue, May 7, 2013 at 9:27 AM, Nigel Deakin <nigel.deakin_at_oracle.com>wrote:

> I have logged this JIRA issue:
>
> https://java.net/jira/browse/**JMS_SPEC-121<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<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);
> }
>
>
>