【发布时间】:2018-11-02 13:29:59
【问题描述】:
我有一个使用 WELD 3.0.5 和 RestEasy 3.6.1 在 Tomcat 9 上运行的 REST 应用程序。
对于异步请求,Tomcat 在与触发初始化事件的线程不同的线程中触发请求销毁事件。
在这种情况下,使用ThreadLocals 的 WELD 不会停用请求上下文,因此不会调用 bean 处理方法。
见:What do WELD-000225, WELD-000335 and WELD-000715 warnings mean?
我的应用程序依赖于容器生命周期事件来关闭资源并进行清理,因此我需要一种方法来使异步请求的一切工作也正常。我想出的解决方案是在执行链末尾添加一个WebFilter,使当前请求上下文无效。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws Exception {
BeanManager beanManager = CDI.current().getBeanManager();
AlterableContext context = (AlterableContext) beanManager.getContext(RequestScoped.class);
try {
chain.doFilter(request, response);
} finally {
if (request.isAsyncStarted()) {
AbstractBoundContext<?> ctxt = (AbstractBoundContext<?>) delegate;
ctxt.invalidate();
ctxt.deactivate();
ctxt.cleanup();
}
}
}
这在丢弃 bean 和删除一些线程局部变量方面做得很好。不幸的是,一些变量仍然与池线程相关联 Tomcat 抱怨它:
SEVERE: The web application [cdi-weld] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@3d97cd8b]) and a value of type [org.jboss.weld.module.web.servlet.HttpContextLifecycle.Counter] (value [org.jboss.weld.module.web.servlet.HttpContextLifecycle$Counter@43a15b16]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Nov 02, 2018 10:43:55 AM org.apache.catalina.loader.WebappClassLoaderBase checkThreadLocalMapForLeaks
SEVERE: The web application [cdi-weld] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@21d33dd8]) and a value of type [org.jboss.weld.contexts.AbstractManagedContext.ManagedState] (value [org.jboss.weld.contexts.AbstractManagedContext$ManagedState@2a336421]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Nov 02, 2018 10:43:55 AM org.apache.catalina.loader.WebappClassLoaderBase checkThreadLocalMapForLeaks
SEVERE: The web application [cdi-weld] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@294b0f79]) and a value of type [org.jboss.weld.module.web.servlet.ConversationContextActivator$$Lambda$555/1676983761] (value [org.jboss.weld.module.web.servlet.ConversationContextActivator$$Lambda$555/1676983761@60fb88ad]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
我的理解是每个请求都会覆盖 ThreadLocals,所以这并不完全是内存泄漏,但我仍然对这个解决方案不是 100% 满意。
有人知道解决此问题的更好方法吗?
【问题讨论】: