显示使用线程池Executors,必须执行 pool.shutdown() 否则会存在线程池泄露;

http://stackoverflow.com/questions/22650569/spring-webapp-shutting-down-threads-on-application-stop

I am instantiating a ScheduledExecutorService using Spring's ApplicationListener interface as follows:

@Component
public class ExecutorsStart implements ApplicationListener<ContextRefreshedEvent> { private ScheduledExecutorService executor; @Autowired
Scheduler scheduler; @Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
executor = Executors.newSingleThreadScheduledExecutor();
scheduler.init();
int delay = 10;
int period = 60;// repeat every 1 minutes.
executor.scheduleAtFixedRate(scheduler, delay, period, TimeUnit.SECONDS);
}

At the moment, Tomcat won't shut down cleanly when I run, ./shutdown.sh, with message:

The web application [/foo] appears to have started a thread named [pool-1-thread-1] but has failed to stop it

and this seems to be because I have not yet written code to stop the ScheduledExecutorService.

My question is: how should this be done properly in this environment?

I noticed that there exists a ContextStoppedEvent, so, I implemented a listener for it:

@Component
public class ExecutorsStop implements ApplicationListener<ContextStoppedEvent> { @Autowired
ExecutorsStart executorsStart; @Override
public void onApplicationEvent(final ContextStoppedEvent event) {
executorsStart.executor.shutdownNow();
}

But it seems that this event handler doesn't get called when Tomcat is shutdown.

Have I implemented this incorrectly, or am I going about this completely the wong way?

asked Mar 26 '14 at 2:47
Francis

1,42712241
 

You're looking for ContextClosedEvent.

@Component
public class ExecutorsStop implements ApplicationListener<ContextClosedEvent> { @Autowired
ExecutorsStart executorsStart; @Override
public void onApplicationEvent(final ContextClosedEvent event) {
System.out.println("Stopped: " + event);
}
}

When the Servlet container shuts down, it calls contextDestroyed(..) on its various ServletContextListener and destroy() on its Servlet instances. The ContextLoaderListener and DispatcherServlet each call close() on their ApplicationContext.

answered Mar 26 '14 at 2:55
Sotirios Delimanolis

159k25267382
 
1  
Is the PreDestroy annotation you mention in stackoverflow.com/a/22544982/55070 not enough for doing so?– leo Aug 27 '14 at 12:13
    
@lep Sure, preDestroy could work here too. – Sotirios Delimanolis Aug 27 '14 at 14:24

Spring webapp - shutting down threads on Application stop的更多相关文章

  1. [Spring Boot] Set Context path for application in application.properties

    If you were using Microservice with Spring Boot to build different REST API endpoints, context path ...

  2. spring boot中的底层配置文件application.yam(application.property)的装配原理初探

    *在spring boot中有一个基础的配置文件application.yam(application.property)用于对spring boot的默认设置做一些改动. *在spring boot ...

  3. Spring JTA应用JOTM & Atomikos I Application

    关于Spring JTA的介绍非常多了,这里就不再一再阐述其优越性怎么怎么了,直接开始正题.一个大致的需求如下,用户在进行增删改操作时,会同时更新2至3个数据库的数据表,操作需要事务来包裹,以便在操作 ...

  4. mvn+spring+webapp模板

    idea新建项目,选择maven-archetype-webapp 在main目录下创建java  resource 文件夹,赋予特殊文件夹 pom.xml 添加 <!--Spring框架核心库 ...

  5. Spring系列之——springboot解析resources.application.properties文件

    摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...

  6. spring webapp的配置文件放置在项目外的方法

    在web.xml中,填写     <context-param>         <param-name>CFG_HOME</param-name>         ...

  7. spring boot application.properties 属性详解

    2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...

  8. spring boot application.properties详解

    附上最新文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-propertie ...

  9. Spring boot 全局配置文件application.properties

    #更改Tomcat端口号 server.port=8090 #修改进入DispatcherServlet的规则为:*.htmlserver.servlet-path=*.html#这里要注意高版本的s ...

随机推荐

  1. Cryptopp iOS 使用 RSA加密解密和签名验证签名

    Cryptopp 是一个c++写的功能完善的密码学工具,类似于openssl 官网:https://www.cryptopp.com 以下主要演示Cryptopp 在iOS上的RSA加密解密签名与验证 ...

  2. AS3绘制扇形算法解析

    网上有很多使用AS3画一个扇形的方法,但是却一个都没有解释这个函数是如何运作来画出扇形的,下面浅谈下我对这个函数的理解. 首先上代码,代码来自http://blog.csdn.net/weiming8 ...

  3. scanf与scanf_s的区别

    scanf()函数是标准C中提供的标准输入函数,用以用户输入数据 scanf_s()函数是Microsoft公司VS开发工具提供的一个功能相同的安全标准输入函数,从vc++2005开始,VS系统提供了 ...

  4. EaseType缓动函数

    http://sol.gfxile.net/interpolation/   一篇很详细的图文

  5. gz

    不准备的话,是真的会滚粗的. leetcode  还是重新做起来叭. 那么就开始咯 8.22 leetcode 144 Binary Tree Preorder Traversal 二叉树的前序遍历 ...

  6. iOS开发UI篇—使用picker View控件完成一个简单的选餐应用

    iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162 ...

  7. Maven+SSM搭建总结(非教程)

    记录我用Maven搭建Spring+SpringMVC+Mybatis项目的过程. 网上关于这个的详细教程有很多,但是优质而适合自己看的需要自己筛选以下我看过的几篇认为讲的比较详细的资源(照着做吧,做 ...

  8. 国际性公司的中国化BPM业务流程管理怎么落地?

    康奈可集团于1938年在日本成立,总部位于东京,日本康奈可自2002年开始投资中国,现已在江苏无锡.湖北襄樊和广州地区投资设立8家公司,总投资超过1亿美元.公司主要生产汽车模块及散热器.车用空调.消音 ...

  9. 调用回调函数出现或者大循环出现has triggered a breakpoint

    triggered a breakpoint 的意思是触发一个断点.这个问题一般发生在程序运行过程中.下面是错误发生显示的信息:Windows has triggered a breakpoint i ...

  10. SoapUI中如何传递cookie

    import com.eviware.soapui.support.types.StringToStringMap //Get all the cookies in the response def ...