jakartaee / messaging

Jakarta Messaging
https://eclipse.org/ee4j/messaging
Other
41 stars 33 forks source link

Injection of JMSContext objects not possible in a WebSocket @OnMessage or @OnClose callback method #121

Open glassfishrobot opened 11 years ago

glassfishrobot commented 11 years ago

If you inject a JMSContext into a websocket ServerEndpoint you can use it in the tt>@OnOpen</tt callback, but if you attempt to use it from the tt>@OnMessage</tt or tt>@OnClose</tt 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);
    }

Affected Versions

[2.0]

glassfishrobot commented 6 years ago
glassfishrobot commented 11 years ago

@glassfishrobot Commented Reported by @nigeldeakin

glassfishrobot commented 11 years ago

@glassfishrobot Commented @nigeldeakin said: 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 tt>@OnMessage</tt or tt>@OnClose</tt callback.

This seems to be an inconsistency in the CDI specification. Although there is no valid HTTP request during the tt>@OnMessage</tt and tt>@OnClose</tt callbacks, CDI already widens the definition of tt>@RequestScoped</tt 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 have 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

If no such change is made, the JMS expert group may need to consider whether any changes are needed to the definition of the scope of an injected JMSContext.

glassfishrobot commented 11 years ago

@glassfishrobot Commented genomeprjct said: A similar issue is currently floating around the CDI spec. This isn't an issue for CDI or JMS, but that WebSockets spec needs to indicate what contexts need to be activated (for example, regular CDI injections for any object that is request or transaction scoped are not injected either) for each of these annotated methods.

I haven't tested it yet, but I believe the work around will be to make your server endpoint a session bean (which makes it fall under the EJB spec) or to make the endpoint a standard CDI object.

Here's the link to the existing CDI issue: https://issues.jboss.org/browse/CDI-370

glassfishrobot commented 11 years ago

@glassfishrobot Commented miojo said: I tried that John and it does not work. A different instance is created to the CDI context, besides the one for the ws endpoint.

glassfishrobot commented 11 years ago

@glassfishrobot Commented @nigeldeakin said: Added link to WEBSOCKET_SPEC-196

glassfishrobot commented 11 years ago

@glassfishrobot Commented Issue-Links: is related to WEBSOCKET_SPEC-196

glassfishrobot commented 7 years ago

@glassfishrobot Commented This issue was imported from java.net JIRA JMS_SPEC-121