spring threadpooltaskexecutor导致tomcat中的内存泄漏

时间:2016-02-27 18:21:18

标签: java spring tomcat jersey-2.0

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

我有一个在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

提到春天会照顾我创造的线索;但不幸的是,它似乎并非如此......

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

1 个答案:

答案 0 :(得分:0)

作为一个网络应用程序,您可以尝试类似下面的内容;​​

您的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);
    }

}

关闭ExecutorService ServletContextListener方法中的contextDestroyed

@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