Spring關(guān)閉Tomcat Servlet容器時(shí)內(nèi)存泄漏問(wèn)題解決方案
這篇文章主要介紹了Spring關(guān)閉Tomcat Servlet容器時(shí)內(nèi)存泄漏問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
出錯(cuò)信息
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)
問(wèn)題分析
servlet容器關(guān)閉時(shí)發(fā)現(xiàn)Quartz定時(shí)器線(xiàn)程還在執(zhí)行,對(duì)其無(wú)所適從,不懂怎么辦只能強(qiáng)行關(guān)閉。
解決思路
在關(guān)閉容器時(shí)的contextDestroyed事件里檢測(cè)ServletContext里Quartz相關(guān)屬性,找到Bean然后調(diào)用它的方法結(jié)束掉。
解決方法
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("發(fā)現(xiàn)quartz定時(shí)任務(wù)");
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....");*/
}
|
關(guān)閉tomcat日志如下:

其他解決方法
Spring配置文件
筆者經(jīng)過(guò)實(shí)踐,發(fā)現(xiàn)在spring配置文件里設(shè)置參數(shù)也可以達(dá)到以上效果
<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關(guān)閉的時(shí)候關(guān)閉線(xiàn)程
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC利用dropzone組件實(shí)現(xiàn)圖片上傳
- SpringMVC的執(zhí)行流程及組件詳解
- bootstrap fileinput組件整合Springmvc上傳圖片到本地磁盤(pán)
- SpringBoot注冊(cè)Servlet的三種方法詳解
- 詳解Spring框架下向異步線(xiàn)程傳遞HttpServletRequest參數(shù)的坑
- Servlet+MyBatis項(xiàng)目轉(zhuǎn)Spring Cloud微服務(wù),多數(shù)據(jù)源配置修改建議
- Spring MVC學(xué)習(xí)之DispatcherServlet請(qǐng)求處理詳析
- SpringMVC DispatcherServlet組件實(shí)現(xiàn)解析
相關(guān)文章
SpringBoot整合Apache Ignite的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合Apache Ignite的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Springboot項(xiàng)目因?yàn)閗ackson版本問(wèn)題啟動(dòng)報(bào)錯(cuò)解決方案
這篇文章主要介紹了Springboot項(xiàng)目因?yàn)閗ackson版本問(wèn)題啟動(dòng)報(bào)錯(cuò)解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
SVN導(dǎo)入maven項(xiàng)目報(bào)錯(cuò)解決方案
這篇文章主要介紹了SVN導(dǎo)入maven項(xiàng)目報(bào)錯(cuò)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Java 數(shù)組轉(zhuǎn)List的四種方式小結(jié)
最近看了下數(shù)組轉(zhuǎn)List的實(shí)現(xiàn)方法,總共有4種,本文就詳細(xì)的介紹一下,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
值得收藏!教你如何在IDEA中快速查看Java字節(jié)碼
開(kāi)發(fā)中如果我們想看JVM虛擬機(jī)怎么編譯我們的Java文件,生成字節(jié)碼的,用IDEA工具就可以查看,本篇文章就給大家詳細(xì)介紹,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Java+Swing實(shí)現(xiàn)醫(yī)院管理系統(tǒng)的完整代碼
這篇文章主要介紹了Java+Swing實(shí)現(xiàn)醫(yī)院管理系統(tǒng)的完整代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
Spring實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離詳解
這篇文章主要介紹了Spring?實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離,大多數(shù)系統(tǒng)都是讀多寫(xiě)少,為了降低數(shù)據(jù)庫(kù)的壓力,可以對(duì)主庫(kù)創(chuàng)建多個(gè)從庫(kù),從庫(kù)自動(dòng)從主庫(kù)同步數(shù)據(jù),程序中將寫(xiě)的操作發(fā)送到主庫(kù),將讀的操作發(fā)送到從庫(kù)去執(zhí)行,需要的朋友可以參考下2024-01-01

