Spring整合ElasticJob 关闭Tomcat容器时内存泄漏
2017-11-14 18:35
513 查看
在完成定时任务的过程中,基本要求完成但是在项目停止的时候,Job 不能正常的去关闭,报出警告:
org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [***_QuartzSchedulerThread] but has failed to stop it. This is very likely to create a memory ...
原因:servlet容器关闭时发现Job定时器线程还在执行,对其无所适从,不懂怎么办只能强行关闭。
解决思路:在关闭容器时的contextDestroyed事件里检测ServletContext里Job 相关属性,找到Bean然后调用它的方法结束掉
代码:
1、web.xml 增加一个listener
org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [***_QuartzSchedulerThread] but has failed to stop it. This is very likely to create a memory ...
原因:servlet容器关闭时发现Job定时器线程还在执行,对其无所适从,不懂怎么办只能强行关闭。
解决思路:在关闭容器时的contextDestroyed事件里检测ServletContext里Job 相关属性,找到Bean然后调用它的方法结束掉
代码:
1、web.xml 增加一个listener
<listener> <listener-class>com.***.elasticjob.AppContextListener</listener-class> </listener>
2、AppContextListener 代码
public class AppContextListener extends ContextLoader implements ServletContextListener{ private static Logger logger = LoggerFactory.getLogger(AppContextListener.class); @Override public void contextInitialized(ServletContextEvent servletContextEvent) { } @Override public void contextDestroyed(ServletContextEvent event) { logger.info("Destroying Context..."); try { WebApplicationContext context = (WebApplicationContext) event.getServletContext().getAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); String[] beanNames = context.getBeanDefinitionNames(); for(String beanName:beanNames) { if(beanName.contains("dangdang")&&beanName.contains("SpringJobScheduler")){ logger.info("发现dangdang定时任务beanName: "+beanName); SpringJobScheduler scheduler = (SpringJobScheduler)context.getBean(beanName); scheduler.getSchedulerFacade().shutdownInstance(); } } } catch (Exception e) { logger.error("Error Destroying Context", e); } } }
参考文献:
https://www.cnblogs.com/passedbylove/p/7580477.html[/code]http://blog.csdn.net/liujun_for_java/article/details/78101478http://blog.csdn.net/dslztx/article/details/47276953
相关文章推荐
- Spring整合quartz关闭,关闭Tomcat Servlet容器时内存泄漏
- 解决spring整合quartz,tomcat关闭后,quartz还在运行
- 把spring-boot项目部署到tomcat容器中
- spring整合quartz定时器的项目中,如何关闭不断输出的batch acquisition of 0 triggers ?
- Flex BlazeDS整合Spring在Tomcat下的安全验证
- CXF整合Spring通过tomcat发布webservice
- IntelliJ IDEA 配置Gradle运行SpringBoot Web项目(tomcat容器)
- Spring容器整合WebSocket
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- 把spring-boot项目部署到tomcat容器中
- springboot 内置默认启动tomcat容器遇到The valid characters are defined in RFC 7230 and RFC 3986”
- 把spring-boot项目部署到tomcat容器中
- 把spring-boot项目部署到tomcat容器中
- spring mvc + velocity 搭建实例程序maven版本并且使用的是tomcat容器而不是jetty(step by step)
- spring整合JNDI和Tomcat
- Spring整合ActiveMQ:spring+JMS+ActiveMQ+Tomcat
- Struts1和Spring整合三种启动Spring容器的方式
- Struts框架下Nginx与Tomcat容器的深度整合实践
- maven打包排除spring-boot内嵌tomcat容器依赖jar
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象