启动和停止 JMSContext
Starting and Stopping JMSContext
JMSContext
有两个方便的方法:
@Inject
JMSContext jmsContext;
this.jmsContext.start();
this.jmsContext.stop();
但是我不允许使用这些方法,因为 start()
的 JavaDoc 指出 "This method must not be used if the JMSContext is container-managed (injected). Doing so will cause a IllegalStateRuntimeException to be thrown."
(而且我试过了,两种方法都确实抛出异常。)
那么,如果 JMSContext
是容器管理的,我该如何启动和停止它呢?
容器管理的JMSContext
的生命周期(start()
、stop()
)由容器管理。这意味着注入的 JMSContext
在注入时已经启动,并且将根据使用 JMSContext
的上下文被容器停止。
这是 JMS Specification 2.0 (Download) 对这个话题的看法:
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.
它进一步描述了对 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.
JMSContext
有两个方便的方法:
@Inject
JMSContext jmsContext;
this.jmsContext.start();
this.jmsContext.stop();
但是我不允许使用这些方法,因为 start()
的 JavaDoc 指出 "This method must not be used if the JMSContext is container-managed (injected). Doing so will cause a IllegalStateRuntimeException to be thrown."
(而且我试过了,两种方法都确实抛出异常。)
那么,如果 JMSContext
是容器管理的,我该如何启动和停止它呢?
容器管理的JMSContext
的生命周期(start()
、stop()
)由容器管理。这意味着注入的 JMSContext
在注入时已经启动,并且将根据使用 JMSContext
的上下文被容器停止。
这是 JMS Specification 2.0 (Download) 对这个话题的看法:
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.
它进一步描述了对 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.