0

I am working on a issue related to async job in Reasteasy (http://docs.jboss.org/resteasy/docs/2.3.1.GA/userguide/html_single/index.html#async_job_service).

I post a request adding ?asynch=true to the url, and the job is then run asynchronously, but when it runs, it works fine with @ApplicationScoped or @Singleton annotated bean, but it cannot access bean of class declared with @RequestScoped annotation and I always run into this error :

org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped
    at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:664)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:77)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:87)
    at com.examplecompany.exampleproject.multitenancy.org$jboss$weld$bean-flat-ManagedBean-class_com$examplecompany$exampleproject$multitenancy$PersistenceContext_$$WeldClientProxy.setDb(org$jboss$weld$bean-flat-ManagedBean-class_com$examplecompany$exampleproject$multitenancy$PersistenceContext$$_WeldClientProxy.java)
    at com.examplecompany.exampleproject.auth.oauth.secure.OAuthDelegate.filterHttp(OAuthDelegate.java:115)
    at com.examplecompany.exampleproject.auth.oauth.secure.AuthorizationInterceptor.preProcess(AuthorizationInterceptor.java:59)
    at com.examplecompany.exampleproject.auth.oauth.secure.org$jboss$weld$bean-flat-ManagedBean-com$examplecompany$exampleproject$auth$oauth$secure$AuthorizationInterceptor$@javax$enterprise$context$ApplicationScoped()@javax$ws$rs$ext$Provider()@org$jboss$resteasy$annotations$interception$SecurityPrecedence()@org$jboss$resteasy$annotations$interception$ServerInterceptor()${com$examplecompany$exampleproject$auth$oauth$secure$AuthorizationInterceptor$oauthDelegate$@javax$inject$Inject()$$}_$$_WeldClientProxy.preProcess(org$jboss$weld$bean-flat-ManagedBean-com$examplecompany$exampleproject$auth$oauth$secure$AuthorizationInterceptor$@javax$enterprise$context$ApplicationScoped()@javax$ws$rs$ext$Provider()@org$jboss$resteasy$annotations$interception$SecurityPrecedence()@org$jboss$resteasy$annotations$interception$ServerInterceptor()${com$examplecompany$exampleproject$auth$oauth$secure$AuthorizationInterceptor$oauthDelegate$@javax$inject$Inject()$$}_$$_WeldClientProxy.java)
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:247)
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502)
    at org.jboss.resteasy.core.AsynchronousDispatcher.invokeSuper(AsynchronousDispatcher.java:227)
    at org.jboss.resteasy.core.AsynchronousDispatcher$1.call(AsynchronousDispatcher.java:267)
    at org.jboss.resteasy.core.AsynchronousDispatcher$1.call(AsynchronousDispatcher.java:259)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

This error do not occur if I post the same request not adding ?asynch=true.

I further investigated the issue and wrote the following lines in my code

try {
    Context context = beanManager.getContext(RequestScoped.class);
} catch (ContextNotActiveException e) {
    logger.info("Oops the context does not exists, we are in bad sh*t",e);
}

If I'm in async mode, the ContextNotActiveExceptionis always thrown, and the log have the same exception message WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped.

So I guess that when the task is launched async mode, the two context Session and Request are not created, and thus my bean defined in these scope are inaccessible.

I raised a ticket into Resteasy Jira for this: https://issues.jboss.org/browse/RESTEASY-682

Nicocube
  • 2,962
  • 2
  • 20
  • 28

1 Answers1

0

According the to spec, section 6.7.2, the SessionScope is not available for web service requests. You'll have to create a new persistent scope and use that. The problem with web service calls (JAXRS or JAXWS) and the session is that there is no guaranteed way to track the session from one request to the next. Clients aren't required to send back cookies, or use a request param. If your service requires and enforces this, then you'll have to either create a new scope and context backing, or use the CDI implementation's API to manually start and bind to the session scope.

LightGuard
  • 5,298
  • 19
  • 19
  • Good point, I gave a try changing my `@SessionScoped` for a `@RequestScoped` but same issue here: `org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped` – Nicocube Mar 14 '12 at 15:18
  • I edited the original post to reflect this change. In fact my issue arise only and only into asynchronous requests where apparently the CDI environment is not available. :( – Nicocube Mar 14 '12 at 15:26
  • Odd. 6.7.1 indicates it should be active. Which server and version? – LightGuard Mar 15 '12 at 15:13
  • It's under tomcat 6.0.35. But again it's an issue with asynchronous feature of resteasy, the same request in normal mode works well. :( – Nicocube Mar 15 '12 at 17:13
  • I edited drastically the question to match my current avancement on this issue. As I finished with a bug and raised an issue in the Resteasy tracker, I'll now write a workaround. :( – Nicocube Mar 16 '12 at 13:52