gpt4 book ai didi

java - 调用@Stateless bean 的@Asynchronous 方法时出现ContextNotActiveException

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:02 24 4
gpt4 key购买 nike

我在异步 Servlet 中注入(inject)一个 @Stateless bean 并从 Serrvlet 调用 @Asynchronous 方法。在 jboss 的服务器日志中,我看不到任何异常,但是在启动 Java Mission Control 和 Flight Recorder 时,只要 Servlet 调用 @Asyncrhonous,我就可以看到 ContextNotActiveExcetion 方法。

服务小程序::

@WebServlet(urlPatterns = { "/asyncservice" }, asyncSupported = true)
public class AsyncServiceServlet extends HttpServlet {

@Inject
private Service service;

protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
final AsyncContext asyncContext = request.startAsync(request, response);
asyncContext.start(new Runnable() {
@Override
public void run() {
try {
service.service(asyncContext);
} catch (ContextNotActiveException | IOException e) {
e.printStackTrace();
}
});
}

服务等级::

@Stateless
public class Service {

@Asynchronous
public void service(final AsyncContext asyncContext) throws IOException {
HttpServletResponse res = (HttpServletResponse) asyncContext.getResponse();
res.setStatus(200);
asyncContext.complete();
}
}

我可以在飞行记录器中看到的堆栈跟踪::

      java.lang.Throwable.<init>()  4
java.lang.Exception.<init>() 4
java.lang.RuntimeException.<init>() 4
javax.enterprise.context.ContextException.<init>() 4
javax.enterprise.context.ContextNotActiveException.<init>() 4
org.jboss.weld.context.ContextNotActiveException.<init>(Enum,Object[]) 4
org.jboss.weld.manager.BeanManagerImpl.getContext(Class) 4
org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(InterceptorContext) 4
org.jboss.invocation.InterceptorContext.proceed() 4
org.jboss.invocation.InitialInterceptor.processInvocation(InterceptorContext) 4
org.jboss.invocation.InterceptorContext.proceed() 4
org.jboss.invocation.ChainedInterceptor.processInvocation(InterceptorContext) 4
org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(InterceptorContext) 4
org.jboss.invocation.InterceptorContext.proceed() 4
org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(InterceptorContext) 4
org.jboss.invocation.InterceptorContext.proceed() 4
org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(InterceptorContext,TransactionManager,EJBComponent) 4
org.jboss.as.ejb3.tx.CMTTxInterceptor.required(InterceptorContext,EJBComponent,int) 4
org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(InterceptorContext)

我已经看了很多帖子,但问题仍然存在,请帮助我。

最佳答案

AsyncContext.start 的 javadoc:

Registers the given AsyncListener with the most recent asynchronous cycle that was started by a call to one of the ServletRequest.startAsync() methods. The given AsyncListener will receive an AsyncEvent when the asynchronous cycle completes successfully, times out, or results in an error.

暗示到这个时候调用

service.service(asyncContext);

已创建,httpservletrequest“上下文”可能不可用,甚至可能已提交请求,导致 CDI 无法确定您的服务使用的任何“@RequestScoped”bean。

请注意,AsyncContext.start 会注册一个 onEvent,以便在异步调用完成或出错时调用,而不是在异步调用开始时调用。

您可能希望在调用 AsyncContext.start 之前添加要调用的监听器

关于java - 调用@Stateless bean 的@Asynchronous 方法时出现ContextNotActiveException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29721160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com