gpt4 book ai didi

java - spring threadpooltaskexecutor导致tomcat内存泄漏

转载 作者:行者123 更新时间:2023-11-28 22:52:38 29 4
gpt4 key购买 nike

我知道这个问题被问过几次,但没有人提供正确答案所以重新发布

我有一个在 Tomcat 7 中运行的 Spring4-Jersey 网络服务。

我正在使用 Spring 的 ThreadPoolTask​​Executor 来处理队列外的一些消息。我有一个使用 @Scheduled 的 bean,它每 1000 毫秒向执行程序提交一次任务。

但是,我注意到当我关闭 Tomcat 时,它会警告我它无法关闭某些任务。

    SEVERE: The web application appears to have started a thread named [taskExecutor-9] but has failed to stop it. This is very likely to create a memory leak.
org.apache.catalina.loader.WebappClassLoader clearReferencesThreads

这是我在代码中用来初始化 taskExecutor 的内容

@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}

http://docs.spring.io/spring/docs/3.2.0.RC1_to_3.2.0.RC2/changes/docdiffs_org.springframework.scheduling.annotation.html

提到 spring 会处理我创建的线程;但不幸的是,情况似乎并非如此……

有人可以提供任何指示吗??

最佳答案

作为一个 web 应用程序,您可以尝试以下操作;

您的 SchedulingConfiguration 类

@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {

/* Beans and Other Stuff */

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(workers());
}

@Bean(name = "executorService")
ExecutorService workers() {
return Executors.newScheduledThreadPool(100);
}

}

关闭 ServletContextListenercontextDestroyed 方法中的 ExecutorService

@Configuration
public class CustomServletContextListener implements ServletContextListener {

@Autowired
private ExecutorService executorService;

@Override
public void contextInitialized(ServletContextEvent context) {
/* Do stuff If Required */
}

@Override
public void contextDestroyed(ServletContextEvent context) {
executorService.shutdown();
}

}

为我工作,我使用 Tomcat8

关于java - spring threadpooltaskexecutor导致tomcat内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35673595/

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