简体   繁体   English

spring threadpooltaskexecutor导致tomcat中的内存泄漏

[英]spring threadpooltaskexecutor causes memory leak in tomcat

I know this question was asked couple of times but none have provided a correct answer so reposting 我知道这个问题曾被问过几次,但没有人提供正确的答案,所以重新发布

I have a Spring4-Jersey webservice that runs inside Tomcat 7. 我有一个运行在Tomcat 7中的Spring4-Jersey Web服务。

I am using Spring's ThreadPoolTaskExecutor to process some messages off a queue. 我正在使用Spring的ThreadPoolTask​​Executor处理队列中的某些消息。 I have a bean that uses @Scheduled which submits tasks to the executor every 1000 millis. 我有一个使用@Scheduled的bean,它每1000毫秒将任务提交给执行程序。

However, I have noticed when I shutdown Tomcat, it warns me that it can't shutdown some tasks. 但是,我注意到当我关闭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

this what I have in code to initialize taskExecutor 这是我在代码中初始化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 http://docs.spring.io/spring/docs/3.2.0.RC1_to_3.2.0.RC2/changes/docdiffs_org.springframework.scheduling.annotation.html

mentions that spring would take care of the threads that i created; 提到spring会照顾我创建的线程; but unfortunately it doesn't seem to be case... 但不幸的是,情况似乎并非如此……

Could someone provide any pointers ?? 有人可以提供任何指针吗?

As its a web-application, you can try something like below; 作为其Web应用程序,您可以尝试以下操作;

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

}

ShutDown The ExecutorService in ServletContextListener 's contextDestroyed method. ServletContextListenercontextDestroyed方法中contextDestroyed 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();
    }

}

Worked for me and I use Tomcat8 . 为我工作,我使用Tomcat8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM