【发布时间】:2015-04-18 18:25:32
【问题描述】:
我在异步 Servlet 中注入 @Stateless bean 并从 Servlet 调用 @Asynchronous 方法。在 jboss 的服务器日志中,我看不到任何异常,但是在启动 Java Mission Control 时,每当 Servlet 调用 @Asyncrhonous 方法时,飞行记录器我都可以看到 ContextNotActiveExcetion。
Servlet ::
@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)
我已经浏览了很多帖子,但问题仍然存在,请帮助我。
【问题讨论】:
-
这是整个堆栈跟踪吗?另外,您如何构建项目(Maven、Ant)?您确定没有缺少某些依赖项吗? :)
-
哪个 JBoss AS 版本?无法在 WildFly 8.2 中重现。工作得很好,所以看起来很像 JBoss AS 中使用的 Weld 版本中的错误。可能值得升级到 WildFly 或至少是 WildFly 8.2 中使用的 Weld 版本。
-
我使用的是 jboss eap 6.1,这不会在任何服务器日志或控制台输出中产生任何错误/异常,但如果您将启动 Java 任务控制并启动 java 飞行记录器,您会看到 ContextNotActiveException在代码>>异常。
-
您在 Flight Recorder 的异常视图中看到上面的堆栈跟踪了吗?可能是 Flight Recorder 异常事件是在创建异常时创建的,而不是在抛出异常时创建的,这可以解释 JFR 和日志之间的区别。您有实际问题,还是 JFR 中的例外?
-
我有只在 JFR 中显示的异常,在执行流程时我没有看到任何异常。
标签: java jakarta-ee asynchronous ejb stateless-session-bean