Environment : WAS 8.0.0.10 CDI : 1.0 (Implementation OpenWebBeans)

Use Case: Server is executing the Java class asynchronously via TimerManager. I am trying to inject the cdi bean with Request scope into the class but when any method is called on the injection, below is the stack trace i am getting. If i use the Applicationscope instead of RequestScope in the injection, Code works fine.

Upon investigating the issue, i found that Request and Session context will not be active for the threads initiallized asynchronously by the container. Is there some way i can initialize the request and session context?

Error :

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)
1

There are 1 answers

9
McCoy On

I'm assuming you already have this, or something alike somewhere:

CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();    
cdiContainer.boot();
ContextControl contextControl = cdiContainer.getContextControl();

Then, somehow you have access to the ContextControl instance. Then you can start the context wherever you need it, just remember to stop it when it's no longer needed

try{
 //start
 contextControl.startContext(RequestScoped.class);

 // do stuff

}catch(Exception e){}
finally{
 //stop
 contextControl.stopContext(RequestScoped.class);   
}

This is working for me in some asynced classes.

Hope it helps. regards!