显示使用线程池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. link和@import的区别

    link属于XHTML标签,而@import完全是css提供的一种方式:标签和css样式的区别就不说了,他们起到的作用区别不大,一般建议用link因为简单,@import会对页面载入有影响,影响性能l ...

  2. logistic回归模型

    一.模型简介 线性回归默认因变量为连续变量,而实际分析中,有时候会遇到因变量为分类变量的情况,例如阴性阳性.性别.血型等.此时如果还使用前面介绍的线性回归模型进行拟合的话,会出现问题,以二分类变量为例 ...

  3. 20145224&20145238《信息安全系统设计基础》实验五

    20145224陈颢文20145238荆玉茗 <信息安全系统设计基础>第五次实验报告 课程:信息安全系统设计基础 班级: 1452 姓名:荆玉茗 陈颢文 学号:20145238 20145 ...

  4. python窗体——pyqt初体验

    连续两周留作业要写ftp的作业,从第一周就想实现一个窗体版本的,但是时间实在太短,qt零基础选手表示压力很大,幸好又延长了一周时间,所以也就有了今天这篇文章...只是为了介绍一些速成的方法,还有初学者 ...

  5. 解决Selenium与firefox浏览器版本不兼容问题

    因为在用java打开firefox浏览器的时候报错 org.openqa.selenium.firefox.NotConnectedException: Unable to connect to ho ...

  6. 第十二章 非对称加密算法-RSA

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 12.1.RSA(最经典的非对称加密算法) 特点: 使用一套密钥即可完成加解密(与D ...

  7. java文件上传下载

    文件上传首先要引入两个核心包 commons-fileupload-1.2.1.jar commons-io-1.4.jar 下面是对文件上传和下载的一些代码做的一个简单封装,可以方便以后直接使用[使 ...

  8. VaildForm 自定义提示消息

    ValidForm插件提供了7种提示效果,其中有四种自定义效果,具体访问地址:http://validform.rjboy.cn/demo.html 个人偏爱其中两种,即 l 提示效果四:[自定义提示 ...

  9. css布局之一列布局

    在我们浏览网页中经常看见一列布局其实一列布局就是 一般的一列布局的都是固定宽度的 body{margin:0;padding:0} .main{width:800px;height:300px;bac ...

  10. 从0到1---“保多多”APP的开发(一)

    2015年8月份,我正式接手了公司保多多APP的开发(和另一个同事一起). 我之前并没有过从0开始创建一款APP,这次能有这样的机会,实在让我感到兴奋.因为我相信,作为这款APP的主要开发人员,在这一 ...