@RequestScoped将CDI注入@MessageDriven bean [英] @RequestScoped CDI injection into @MessageDriven bean

查看:239
本文介绍了@RequestScoped将CDI注入@MessageDriven bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用JMS将请求范围的CDI bean注入 @MessageDriven EJB,如下所示,我可以假设任何给定的 Foo 实例一次只能由一个 onMessage 调用使用?

If I have a request scoped CDI bean injected into a @MessageDriven EJB using JMS, as below, can I assume that any given Foo instance will only be used by a single onMessage invocation at a time?

在其他在下面的示例中,我可以安全地使用 Foo 对象中的成员变量来跨子例程存储状态,类似于JSF @RequestScoped 托管bean?

In other words, in the below example, can I safely use member variables in the Foo object to store state across subroutines, analogously to a JSF @RequestScoped managed bean?

请注意,如果相同的 Foo 对象从一个顺序回收 onMessage 调用下一个,只要每个 MessageDrivenBean 实例都有自己的 Foo 实例,以便同时处理两个请求。

Note that it's ok if the same Foo object gets recycled sequentially from one onMessage call to the next, as long as each MessageDrivenBean instance has its own Foo instance such that two requests processing concurrently would be isolated.

 @MessageDriven
 public class MessageDrivenBean implements MessageListener {
    @Inject 
    private Foo foo;

    public void onMessage(Message m) {
      foo.doSomething();
    }
 }

 @Named
 @RequestScoped
 public class Foo {
   private String property;
     public void doSomething() {
       property = ...;
     }
 }


推荐答案

WRT请求范围/上下文,第6.7.1节中的CDI规范说它将对实现MessageListener的消息驱动bean有效。它在传递消息后也会被销毁,因此您将为每个传递的消息提供一个新实例。此外,第6.7.3节规定应用程序上下文也是活动的(正如人们所期望的那样)。会话和会话范围无效。

WRT the Request scope / context, the CDI spec in section 6.7.1 says that it will be active for a message driven bean implementing MessageListener. It is also destroyed after the delivery of the message, so you'll have a new instance for each message delivered. Further, section 6.7.3 states that the Application context is also active (as one would expect). Conversation and session scopes are not active.

这篇关于@RequestScoped将CDI注入@MessageDriven bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆