Starting and Stopping JMSContext

Steffi S. :

The JMSContext has two handy methods:

@Inject
JMSContext jmsContext;

this.jmsContext.start();
this.jmsContext.stop();

However I'm not allowed to use these methods since start()'s JavaDoc states "This method must not be used if the JMSContext is container-managed (injected). Doing so will cause a IllegalStateRuntimeException to be thrown."

(And I tried it, both methods indead throw an exception.)

So how do I start and stop the JMSContext if it's container-managed?

thokuest :

The lifecycle (start(), stop()) of a container-managed JMSContext is managed by the container. That means that the injected JMSContext is already started when it is injected and will be stopped by the container depending on the context in which the JMSContext is used.

This is what the JMS Specification 2.0 (Download) has to say about this topic:

12.4.4. Scope of injected JMSContext objects

  • If an injected JMSContext is used in a JTA transaction (both bean-managed and container-managed), its scope will be that of the transaction. This means that:
    • The JMSContext object will be automatically created the first time it is used within the transaction.
    • The JMSContext object will be automatically closed when the transaction is committed.
    • If, within the same JTA transaction, different beans, or different methods within the same bean, use an injected JMSContext which is injected using identical annotations then they will all share the same JMSContext object.
  • If an injected JMSContext is used when there is no JTA transaction then its scope will be the existing CDI scope @RequestScoped. This means that:
    • The JMSContext object will be created the first time it is used within a request.
    • The JMSContext object will be closed when the request ends.
    • If, within the same request, different beans, or different methods within the same bean, use an injected JMSContext which is injected using identical annotations then they will all share the same JMSContext object.
  • If injected JMSContext is used both in a JTA transaction and outside a JTA transaction then separate JMSContext objects will be used, with a separate JMSContext object being used for each JTA transaction as described above.

It further describes the restriction on the JMSContext API:

12.4.5. Restrictions on use of injected JMSContext objects

However, to avoid the possibility of code in one bean having an unexpected effect on a different bean, the following methods which change the public state of a JMSContext will not be permitted if the JMSContext is injected.

  • setClientID
  • setExceptionListener
  • stop
  • acknowledge
  • commit
  • rollback
  • recover
  • setAutoStart
  • start
  • close

[...]

These restrictions do not apply when the JMSContext is managed by the application.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154442&siteId=1