简体   繁体   English

启动和停止 JMSContext

[英]Starting and Stopping JMSContext

The JMSContext has two handy methods: JMSContext有两个方便的方法:

@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."但是,我不允许使用这些方法,因为start()的 JavaDoc 声明“如果 JMSContext 是容器管理(注入),则不得使用此方法。这样做将导致抛出 IllegalStateRuntimeException。”

(And I tried it, both methods indead throw an exception.) (我试过了,这两种方法都抛出异常。)

So how do I start and stop the JMSContext if it's container-managed?那么,如果JMSContext是容器管理的,我该如何启动和停止它?

The lifecycle ( start() , stop() ) of a container-managed JMSContext is managed by the container.容器管理的JMSContext的生命周期( start()stop() )由容器管理。 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.这意味着注入的JMSContext在注入时已经启动,并将根据使用JMSContext的上下文由容器停止。

This is what the JMS Specification 2.0 ( Download ) has to say about this topic:这是JMS 规范 2.0下载)关于这个主题的内容:

12.4.4. 12.4.4. Scope of injected JMSContext objects注入的 JMSContext 对象的范围

  • If an injected JMSContext is used in a JTA transaction (both bean-managed and container-managed), its scope will be that of the transaction.如果在 JTA 事务(bean 管理的和容器管理的)中使用注入的 JMSContext,则其作用域将是事务的作用域。 This means that:这意味着:
    • The JMSContext object will be automatically created the first time it is used within the transaction. JMSContext 对象将在事务中第一次使用时自动创建。
    • The JMSContext object will be automatically closed when the transaction is committed. JMSContext 对象将在事务提交时自动关闭。
    • 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.如果在同一个 JTA 事务中,不同的 bean 或同一个 bean 中的不同方法使用注入的 JMSContext,该 JMSContext 是使用相同的注解注入的,那么它们将共享相同的 JMSContext 对象。
  • If an injected JMSContext is used when there is no JTA transaction then its scope will be the existing CDI scope @RequestScoped.如果在没有 JTA 事务时使用注入的 JMSContext,则其范围将是现有的 CDI 范围 @RequestScoped。 This means that:这意味着:
    • The JMSContext object will be created the first time it is used within a request. JMSContext 对象将在第一次在请求中使用时创建。
    • The JMSContext object will be closed when the request ends. JMSContext 对象将在请求结束时关闭。
    • 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.如果在同一个请求中,不同的 bean 或同一个 bean 中的不同方法使用注入的 JMSContext,该 JMSContext 是使用相同的注解注入的,那么它们将共享相同的 JMSContext 对象。
  • 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 在 JTA 事务中和 JTA 事务外部都使用,那么将使用单独的 JMSContext 对象,如上所述,每个 JTA 事务使用单独的 JMSContext 对象。

It further describes the restriction on the JMSContext API:它进一步描述了对JMSContext API 的限制:

12.4.5. 12.4.5. Restrictions on use of injected JMSContext objects使用注入的 JMSContext 对象的限制

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.但是,为了避免一个 bean 中的代码对另一个 bean 产生意外影响的可能性,如果注入了 JMSContext,将不允许使用以下更改 JMSContext 公共状态的方法。

  • setClientID设置客户端ID
  • setExceptionListener设置异常监听器
  • stop停止
  • acknowledge承认
  • commit犯罪
  • rollback回滚
  • recover恢复
  • setAutoStart设置自动启动
  • start开始
  • close关闭

[...] [...]

These restrictions do not apply when the JMSContext is managed by the application.当 JMSContext 由应用程序管理时,这些限制不适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM