Java 组合Resteasy async无法将SessionScoped和RequestScoped bean注入CDI

Java 组合Resteasy async无法将SessionScoped和RequestScoped bean注入CDI,java,cdi,resteasy,interceptor,Java,Cdi,Resteasy,Interceptor,我正在处理一个与Reasteasy中的异步作业相关的问题http://docs.jboss.org/resteasy/docs/2.3.1.GA/userguide/html_single/index.htmlasync_job_service. 我发布了一个请求,将?asynch=true添加到url,然后异步运行作业,但当作业运行时,它可以与@ApplicationScoped或@Singleton注释bean一起正常工作,但它无法访问使用@RequestScoped注释声明的类的bean,

我正在处理一个与Reasteasy中的异步作业相关的问题http://docs.jboss.org/resteasy/docs/2.3.1.GA/userguide/html_single/index.htmlasync_job_service.

我发布了一个请求,将?asynch=true添加到url,然后异步运行作业,但当作业运行时,它可以与@ApplicationScoped或@Singleton注释bean一起正常工作,但它无法访问使用@RequestScoped注释声明的类的bean,我总是遇到以下错误:

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)
如果我发布相同的请求而不添加?asynch=true,则不会发生此错误

我进一步调查了这个问题,并在代码中写了以下几行

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);
}
如果我处于异步模式,则总是抛出ContextNotActivieExceptionis,并且日志具有相同的异常消息WELD-001303作用域类型javax.enterprise.context.RequestScoped的无活动上下文

所以我猜当任务以异步模式启动时,不会创建两个上下文会话和请求,因此在这些作用域中定义的bean是不可访问的


为此,我向Resteasy Jira提交了一张票据:

根据规范第6.7.2节,SessionScope不可用于web服务请求。您必须创建一个新的持久作用域并使用它。web服务调用JAXRS或JAXWS以及会话的问题在于,没有保证能够从一个请求跟踪到下一个请求。客户端不需要发送回cookie,也不需要使用请求参数。如果您的服务需要并强制执行此操作,那么您必须创建新的作用域和上下文支持,或者使用CDI实现的API手动启动并绑定到会话作用域

说得好,我尝试更改了@SessionScoped的@RequestScoped,但这里的问题相同:org.jboss.weld.context.ContextNotActivieException:weld-001303作用域类型javax.enterprise.context.RequestScopedI编辑了原始帖子以反映此更改。事实上,我的问题只出现在CDI环境显然不可用的异步请求中古怪的6.7.1表示它应处于激活状态。哪个服务器和版本?在tomcat 6.0.35下。但是,resteasy的异步特性也有一个问题,在正常模式下,同样的请求也能正常工作我对这个问题进行了大幅编辑,以符合我目前在这个问题上的先见之明。当我处理完一个bug并在Resteasy tracker中提出一个问题时,我现在将编写一个解决方法: