显示使用线程池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. 怎么写jq插件?

    1.概述 先看看html代码 <ul id="catagory"> <li><a href="#">jQuery</a ...

  2. python--安装PIL

    PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux ...

  3. LR参数化后取值规则小记

    对参数化的取值,只有一个用户的情况能分清,但是多用户多迭代就搞不懂,特意使用Parameter List中自带的参数化模拟器Simulate Parameter进行简单的实验,3条数据 + 4个用户 ...

  4. 深入浅出Mybatis系列(一)---Mybatis入门

    最近两年 springmvc + mybatis 的在这种搭配还是蛮火的,楼主我呢,也从来没真正去接触过mybatis, 趁近日得闲, 就去学习一下mybatis吧. 本次拟根据自己的学习进度,做一次 ...

  5. Codeforces Round #371 (Div. 2) C. Sonya and Queries

    题目链接 分析:01trie树,很容易就看出来了,也没什么好说的.WA了一发是因为没有看见如果数字位数大于01序列的时候01序列也要补全0.我没有晚上爬起来打,白天发现过的人极多. /******** ...

  6. 那些年我们错过的超级好用的CSS属性

    在看前辈写的CSS样式的时候发现好多之前都没用过的Css属性,现在看来有必要整理一下啦. 一.CSS选择器(http://www.w3school.com.cn/cssref/css_selector ...

  7. BZOJ4046 [Cerc2014] Pork barre

    我们把边按权值从大到小依次加入图中 如果加到边权$V$,则当前的最小生成森林中边权$v\in[V, V']$(其中$V'$是任意值)形成的森林的边权和就是对于询问$[V, V']$的答案 由于点数不多 ...

  8. [转][C/C++] 怎样不用中间变量temp 实现两个数交换

    第一类方法也是常用的方法,通过多次的数值计算来完成交换,到现在知道的有下面三种: (1)加减法. a = a + b; b = a - b; a = a - b; 该方法可以交换整型和浮点型数值的变量 ...

  9. NetworkComms V3 之同时监听多端口

    NetworkComms网络通信框架序言 NetworkComms通信框架,是一款来自英国的c#语言编写的通信框架,历时6年研发,成熟稳定,性能可靠.   框架支持同时监听服务器上的多个端口,写法如下 ...

  10. iOS开发拓展篇—CoreLocation定位服务

    iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...