[英]No active contexts for scope type javax.enterprise.context.RequestScoped when invoking a bean from a thread
[英]CDI - ContextNotActiveException - WebBeans context with scope type annotation @RequestScoped does not exist within current thread
環境:WAS 8.0.0.10 CDI:1.0(實施OpenWebBeans)
用例:服務器通過TimerManager異步執行Java類。 我試圖將帶有Request范圍的cdi bean注入到類中,但是當在注入上調用任何方法時,下面是我得到的堆棧跟蹤。 如果我在注入中使用Applicationscope而不是RequestScope,則代碼可以正常工作。
在調查了這個問題之后,我發現對於容器異步初始化的線程,請求和會話上下文不會處於活動狀態。 有什么方法可以初始化請求和會話上下文?
錯誤:
javax.enterprise.context.ContextNotActiveException: WebBeans context with scope type annotation @RequestScoped does not exist within current thread**
at org.apache.webbeans.container.BeanManagerImpl.getContext(BeanManagerImpl.java:358)
at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.getContextualInstance(NormalScopedBeanInterceptorHandler.java:124)
at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.invoke(NormalScopedBeanInterceptorHandler.java:95)
at com.ford.it.processcontrol.TestJob3_$$_javassist_22.executeJobCB(TestJob3_$$_javassist_22.java)
我假設您已經有了這個,或者某處類似的東西:
CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
cdiContainer.boot();
ContextControl contextControl = cdiContainer.getContextControl();
然后,以某種方式您可以訪問ContextControl實例。 然后,您可以在任何需要的地方啟動上下文,只記得在不再需要它時停止它
try{
//start
contextControl.startContext(RequestScoped.class);
// do stuff
}catch(Exception e){}
finally{
//stop
contextControl.stopContext(RequestScoped.class);
}
這在某些異步類中為我工作。
希望能幫助到你。 問候!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.