3

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)
MWiesner
  • 8,868
  • 11
  • 36
  • 70
user3437502
  • 31
  • 1
  • 4

1 Answers1

0

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!

McCoy
  • 780
  • 1
  • 10
  • 21
  • My code is executed in the server environment, i need not to start the CDI container and boot it manually, server takes care of this. – user3437502 Jun 11 '15 at 18:55
  • Now, as i mentioned above, WAS has the TimerManager Interface (as part of commonJ spec, not J2ee spec) for Asynchronous tasks. My requirement is to have the RequestScope active during threads initiated asynchronously from TimerManager. – user3437502 Jun 11 '15 at 19:03
  • ContextControl from deltaspike ctrl module looks interesting. I tried using it but seems like i don't have the proper Jars, I am getting noclassdef found exception for WebBeansContext. Seems like WebBeansContext is a part of openwebbeans-impl-xxx, adding that to classpath also doesn't solve my problem, it just changes the error message. WAS 8 has its internal openwebbeans as a cdi implementation. So, adding external jar neither make sense nor it solves the problem. – user3437502 Jun 11 '15 at 19:03
  • oh, I thought ContextControl was part of OpenWebBeans, but it seems like it belongs to Deltaspike implementation only. Well, in my case I have Quartz working to run async jobs, and my jobs are annotated with ApplicationScoped. I got "ContextNotActiveException" also when trying to use the injected EntityManager, so I start the RequestScoped like I mentioned, using contextControl.startContext() – McCoy Jun 11 '15 at 19:31
  • WAS 8.0.0.7 also did not had the ApplicationScope active for Async threads, but WAS fixed this issue as part of WAS 8.0.0.10 fix pack. So in my case, Application scope is available for async threads but not the RequestScope and Session scope. What are the jars you are using to make contextControl.startContext() work? – user3437502 Jun 12 '15 at 05:32
  • And more importantly, you are doing contextControl.startContext() in server env. or outside it? – user3437502 Jun 12 '15 at 07:20
  • So let's assume you have Application Scope active in async threads. I use contextControl.startContext(RequestScope) inside the async thread, in my case, inside the job, when needed. – McCoy Jun 13 '15 at 13:43
  • Not sure what you mean by "inside server env or outside", I don't have a server like Tomcat or Jboss, it's just a Main class that initiates the cdiContainer along with the Application Scope plus 2 services: the job I'm talking about and a WatchService to look for incoming files. I use DeltaSpike to achieve this, so the libraries belong to Deltaspike. Righ now I don't have the libraries handy because I'm not at work, but if you want the exact name I can give it to you on Monday – McCoy Jun 13 '15 at 13:43
  • In another project setup, I had to `@Inject javax.enterprise.context.control.RequestContextController` and call `activate()` / `deactivate()` – Daniel Alder May 21 '21 at 17:10