有一个会话范围的 bean 'Identity',我将它注入到实现 Runnable 的 @Stateless bean 中:
@Stateless
@LocalBean
public class Test implements Runnable {
@Inject
Identity identity;
@Inject
Logger log;
@Override
public void run() {
log.warn("Test: " + this + " " + identity.getAccount().getId());
}
}
还有一个 bean 可以异步调用上述 Runnable:
@Stateless
@LocalBean
public class BeanContextExecutor implements Executor {
@Asynchronous
@Override
public void execute(Runnable command) {
command.run();
}
}
最后,调用看起来像这样:
@Stateless
public class OtherBean {
@Inject
BeanContextExecutor executor;
...
executor.execute(command);
...
}
运行此程序时,我收到以下错误:
...
Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.SessionScoped
...
有没有办法将 SessionContext 传播到后台线程?
我还尝试将此 Runnable 提交给 ManagedExecutorService ,甚至使用 ContextService 为其创建代理并提交代理,但仍然遇到相同的错误。
感谢您对此的任何帮助!