出错信息

22-Sep-2017 06:19:51.064 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [license] appears to have started a thread named [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)

问题分析:servlet容器关闭时发现Quartz定时器线程还在执行,对其无所适从,不懂怎么办只能强行关闭。

解决思路,在关闭容器时的contextDestroyed事件里检测ServletContext里Quartz相关属性,找到Bean然后调用它的方法结束掉。

解决方法:

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener; import org.apache.logging.log4j.web.Log4jWebSupport;
import org.quartz.impl.StdScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; @WebListener
public class AppContextListener implements ServletContextListener
{ public void contextDestroyed(ServletContextEvent event) {
logger.info("Destroying Context..."); try {
WebApplicationContext context = (WebApplicationContext) event.getServletContext().getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); Enumeration<String> attributes = event.getServletContext().getAttributeNames();
while(attributes.hasMoreElements())
{
String attr = attributes.nextElement();
Object prop = event.getServletContext().getAttribute(attr);
logger.info("attribute.name: {},class:{}, value:{}",attr,prop.getClass().getName(),prop);
} String[] beanNames = context.getBeanDefinitionNames(); for(String beanName:beanNames)
{
Object bean = context.getBean(beanName);
logger.info("found bean attribute in ServletContext,name:{},class:{},value:{}",
beanName,bean.getClass().getName(),bean);
if(beanName.contains("quartz")&&beanName.contains("Scheduler")){
StdScheduler scheduler = (StdScheduler)context.getBean("org.springframework.scheduling.quartz.SchedulerFactoryBean#0");
logger.info("发现quartz定时任务");
logger.info("beanName:{},className:{}",scheduler,scheduler.getClass().getName());
if(scheduler.isStarted())
{
logger.info("Quartz:waiting for job complete...");
scheduler.shutdown(true);
logger.info("Quartz:all threads are complete and exited...");
}
}
} } catch (Exception e) {
logger.error("Error Destroying Context", e);
}
} //https://stackoverflow.com/questions/23936162/register-shutdownhook-in-web-application
public void contextInitialized(ServletContextEvent event) {
//ServletContext context = event.getServletContext();
//System.setProperty("rootPath", context.getRealPath("/"));
//LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
//ctx.reconfigure();
/*logger.info("global setting,rootPath:{}",rootPath);
logger.info("deployed on architecture:{},operation System:{},version:{}",
System.getProperty("os.arch"), System.getProperty("os.name"),
System.getProperty("os.version"));
Debugger.dump();
logger.info("app startup completed....");*/
}
|

关闭tomcat日志如下:

其他解决方法:Spring配置文件

笔者经过实践,发现在spring配置文件里设置参数也可以达到以上效果

  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
<ref bean="cronTrigger" />
<ref bean="secondCronTrigger"/>
</list>
</property>
<property name="waitForJobsToCompleteOnShutdown" value="true"/>
</bean>
<property name="waitForJobsToCompleteOnShutdown" value="true"/>可以在web关闭的时候关闭线程

Spring整合quartz关闭,关闭Tomcat Servlet容器时内存泄漏的更多相关文章

  1. 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】

    初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...

  2. spring整合quartz并持久化

    spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overd ...

  3. Spring整合Quartz定时任务执行2次,Spring定时任务执行2次

    Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...

  4. spring整合quartz框架

    spring整合quartz: 网上也有很多教程,好多都是基于配置方式,我们使用当然怎么简单就怎么用,所以这里介绍基于注解方式整合quartz.前提:你需要有一个能运行的web项目. 1.引依赖: & ...

  5. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  6. spring整合quartz时间任务调度框架

    spring整合quartz框架 1.创建maven工程 2.导入jar包(pom.xml) <dependencies> <dependency> <groupId&g ...

  7. Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)

    Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境)   转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...

  8. Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

    Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >> ...

  9. 使用spring整合Quartz实现—定时器

    使用spring整合Quartz实现—定时器(Maven项目做演示) 不基于特定的基类的方法 一,开发环境以及依赖的jar包 Spring 4.2.6.RELEASE Maven 3.3.9 Jdk ...

随机推荐

  1. 注解 - Excel 校验工具

    注解类: @Retention(RetentionPolicy.RUNTIME) public @interface ExcelValidate { public boolean ignoreBlan ...

  2. Java中的transient关键字

    转载于:[lfsf802](http://blog.csdn.net/lfsf802/article/details/43239663) 关键字介绍 一个对象只要实现了Serilizable接口,这个 ...

  3. 在VS2015中用C++编写可被C#调用的DLL

    VS2015用C++创建动态库DLL步骤如下: (1)启动VS2015-->文件-->新建-->项目,按图二进行选择,选择Win32项目,弹出创建窗口,如第二张图.注意.net版本根 ...

  4. java异常处理——finally相关

    示例程序1 public class EmbededFinally { public static void main(String args[]) { int result; try { Syste ...

  5. 三、CSS样式——列表

    概念: CSS列表属性允许你放置.改变列表标志,或者将图像作为列表项标志 属性 描述 list-style 简写列表项 list-style-image 列表项图像 list-style-positi ...

  6. vue登录注册及token验证

    // router.jsimport Vue from 'vue'import VueRouter from 'vue-router' Vue.use(VueRouter) const routes ...

  7. sql语句case when 以及left()

    select count(CASE jyje WHEN '1300' THEN '2' ELSE '1' END) as count  from tpent_orders where cplx = 6 ...

  8. 关于python的多行注释,启动新浏览器,循环语句乘法口诀

    1,提问:如何将python写的多行代码改写成注释,进行写下一段代码?这样可以在多个脚本中写东西? 回答:百度了一下,还真有 选中所要注释的代码  CTRL + / 然后所选的代码前面都会出现#,编程 ...

  9. rownum用法

    对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...

  10. 搭建rancher节点

    1.centos 7.5 64 2.安装docker systemctl restart docker.service 注意:重启后才有 /etc/docker/文件夹 切换到这文件夹下再增加对应的d ...