显示使用线程池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. DIV下的DIV居中

    .ParentDIV{ display: -webkit-flex; display: flex; -webkit-align-items: center; align-items: center; ...

  2. UNIX 逐字符输入

    //终端驱动处于普通胡一次一字符模式 system("stty raw"); //终端驱动处于普通胡一次一行模式 system("stty cooked");

  3. 读javascript高级程序设计10-DOM

    一.节点关系 元素的childNodes属性来表示其所有子节点,它是一个NodeList对象,会随着DOM结构的变化动态变化. hasChildNodes():是否有子节点. var headline ...

  4. Windows Store App 全球化:在XAML元素中引用字符串资源

    在应用程序中可以通过XAML元素和后台代码两种方式引用资源文件中的字符串资源.本小节先讲述如何在XAML元素中引用字符串资源的相关知识点. 在XAML元素中可以通过使用x:Uid属性来引用资源文件中的 ...

  5. linux 基本命令操作

    1.ls 命令 ls -a  列出所有文件,包括隐藏文件 ls -l  列出文件详细信息 ls -r 列出所有文件包括文件夹 查询具体文件可以在命令后面加  |grep 要匹配的字符串,方便我们查找, ...

  6. IOS开发UI篇—导航控制器属性和基本使用

    IOS开发UI篇—导航控制器属性和基本使用 一.导航控制器的一些属性和基本使用 1.把子控制器添加到导航控制器中的四种方法 (1) 1.创建一个导航控制器 UINavigationController ...

  7. Rhel6-cacti+nagios+ganglia(apache)配置文档

    (lamp平台) 系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 1 ...

  8. Android 命令管理项目

    今天介绍一下Android怎么用命令管理项目,用命令管理Android项目需要用到Android命令,首先介绍一下用Android命令创建新项目,打开命令提示窗口,导航到Android SDK 的to ...

  9. Sql获取第一天、最后一天

    昨天面试一家公司,上机题目中要求获取每月最后一笔订单.用到了日期的选择性查询,回来在ITeye上找到了这篇文章. 原文: http://new-fighter.iteye.com/blog/17587 ...

  10. checkbox选中与取消选择

    先上代码 <form> 你爱好的运动是?<br/> <input type="checkbox" name="items" val ...