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

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

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

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

这是一个复制者:

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"; } } 

你遇到过这种行为吗? 有什么想让这个工作好吗?

在这种情况下,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(); }