从线程调用 bean 时,范围类型 javax.enterprise.context.RequestScoped 没有活动上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26631093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:58:57  来源:igfitidea点击:

No active contexts for scope type javax.enterprise.context.RequestScoped when invoking a bean from a thread

javamultithreadingcdiweld-se

提问by Nader

While using Weld-SE 2.1.2.Final to obtain a bean and to invoke it from a thread, I encounter the following exception:

在使用 Weld-SE 2.1.2.Final 获取 bean 并从线程调用它时,我遇到以下异常:

Exception in thread "main" org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped

线程“main” org.jboss.weld.context.ContextNotActiveException 中的异常:WELD-001303:范围类型 javax.enterprise.context.RequestScoped 没有活动上下文

My bean is annotated with @RequestScooped. If I annotate @ApplicationScoped then it works fine, but I need to keep @RequestScooped.

我的 bean 用 @RequestScooped 注释。如果我注释@ApplicationScoped,那么它工作正常,但我需要保留@RequestScooped。

Here is a reproducer :

这是一个复制器:

public static void main(String[] args) throws Exception {
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    final MyPojo pojo = container.instance().select(MyPojo.class).get();

    Thread t = new Thread() {
        public void run() {
            System.out.println(pojo.ping());   // This call fails
        }
    };
    t.start();
    t.join();
    System.out.println(pojo.ping()); // This call succeed
    weld.shutdown();

}

@RequestScoped
public class MyPojo {
 public String ping() {
    return "pong";
 }
}

Did you encounter this behavior? Any idea to make this work please?

你遇到过这种行为吗?有什么想法可以使这项工作吗?

采纳答案by kaos

In this case Weld is using unbound RequestContext that is associated with a thread (RequestContext). You need to manually initialize new RequestContext in a thread that You're creating, this works for me:

在这种情况下,Weld 使用与线程 ( RequestContext)关联的未绑定 RequestContext 。您需要在您正在创建的线程中手动初始化新的 RequestContext,这对我有用:

public static void main(String[] args) throws Exception {
    Weld weld = new Weld();
    final WeldContainer container = weld.initialize();
    RequestContext requestContext= container.instance().select(RequestContext.class, UnboundLiteral.INSTANCE).get();
    requestContext.activate();

    final MyPojo pojo = container.instance().select(MyPojo.class).get();

    Thread t = new Thread() {
        public void run() {
            RequestContext requestContext= container.instance().select(RequestContext.class, UnboundLiteral.INSTANCE).get();
            requestContext.activate();
            System.out.println("1" + pojo.ping()); 
        }
    };
    t.start();
    t.join();
    System.out.println("2" + pojo.ping());
    weld.shutdown();

}