显示使用线程池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. zabbix3.0.4 部署之八 (zabbix3.0.4 微信报警)

    [root@sv-zabbix ~]# cat /usr/local/zabbix/share/zabbix/alertscripts/weixin.py #!/usr/bin/env python# ...

  2. cxf的soap风格+spirng4+maven 服务端

    简介 SOAP 比较复杂,基于XML,有对应规范:REST利用HTTP请请求方式GET,POST,PUT,delete约定具体操作.简单的说,SOAP通过传输XML,XML定义了请求和响应的具体数据, ...

  3. 解决本机安装多版本jdk导致The type java.lang.Object cannot be resolved It is indirectly referenced ...

    本机开始安装了jdk1.6,然后安装了jdk1.8 当在调自动化的时候,发现传入函数传参String类型,报错The type java.lang.Object cannot be resolved ...

  4. 用c语言编写直接插入法

    #include<stdio.h> //直接插入法 void D_insert(int s[],int n); int main() { int i; ]; printf("pl ...

  5. iOS 视图控制器转场详解

    iOS 视图控制器转场详解 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标有了大幅度的增长,多谢唐巧前辈的推荐.有些人问我相关的问题 ...

  6. 鼠标滚轮控制侧边div上下翻动效果

    css部分: <style> * { margin: 0; padding: 0;} .wrap { width: 1000px; margin: 0 auto; overflow: hi ...

  7. mac xcworkspace xcodebuild

    xcodebuild 在介绍xcodebuild之前,需要先弄清楚一些在XCode环境下的一些概念[4]: Workspace:简单来说,Workspace就是一个容器,在该容器中可以存放多个你创建的 ...

  8. 测试nfs和cifs

    losetup /dev/loop1 file1.img losetup /dev/loop2 file2.img losetup /dev/loop3 file3.img mkfs -t ext4 ...

  9. JavaWeb chapeter 5 Web应用程序状态管理

    1.  HTTP协议使用的是无状态连接,对容器而言,每一个请求都来自于一个新的客户. 2. html表单隐藏字段:对用户在网站上的访问进行会话跟踪.为服务器端程序提供预定义的输入.存储动态产生的页面上 ...

  10. JavaEE 7技术一览

    参见此文: https://my.oschina.net/Barudisshu/blog/334903 perfect