On 07/05/2013 14:27, Nigel Deakin wrote:
> https://java.net/jira/browse/JMS_SPEC-121
> Injection of JMSContext objects not possible in a WebSocket @OnMessage or @OnClose callback method
>
> 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.
JMS 2.0 defines an injected JMSContext to have transaction scope if there is a current transaction, and request scope
otherwise. However neither scope is available in a websocket @OnMessage or @OnClose callback.
This seems to be an inconsistency in the CDI specification. Although there is no valid HTTP request during the
@OnMessage and @OnClose callbacks, CDI already widens the definition of @RequestScoped to cover cases which have no
relationship with HTTP. For example CDI 1.0 states that a MDB's onMessage method is executed within a request scope,
even though there is no HTTP request involved. Now that WebSockets are part of Java EE 7 (JSR 356), it seems a bit
arbitrary for a MDB callback to have a valid request scope, but for a WebSocket callback to not be.
I asked the CDI expert group to consider whether a future version of CDI should extend the definition of @RequestScoped
to include a WebSocket @OnMessage callback. This is
https://issues.jboss.org/browse/CDI-370
John A has commented that this should be defined by the WebSocket spec, not the CDI spec.
I can see the logic in that (though if that's the case then the current requirement for a request scope to be valid in
MDB callbacks should probably be moved from the CDI spec to the EJB spec).
In any case I'll follow this up with the CDI and WebSocket spec leads to make sure that it gets handled by one spec or
the other.
Nigel