From 2a8cbfdbf96bb25203a34e2bef38bb9191fa6c89 Mon Sep 17 00:00:00 2001 From: Nikhil Khatwani Date: Fri, 22 Aug 2014 12:03:27 +0530 Subject: [PATCH] MIFOSX-1501 #comment Fixed Issues related to AbandonedCleanupThread and Quartz scheduler worker Threads not stopping on mifos server shut-down. --- ...onedConnectionCleanupShutdownListener.java | 54 +++++++++++++++++++ .../jobs/service/JobRegisterService.java | 2 + .../jobs/service/JobRegisterServiceImpl.java | 20 ++++++- .../data/LoanChargePaidDetail.java | 5 ++ ...epaymentScheduleInstallmentRepository.java | 5 ++ .../domain/RecalculatedSchedule.java | 5 ++ .../domain/RecalculationDetail.java | 5 ++ 7 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/AbandonedConnectionCleanupShutdownListener.java diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/AbandonedConnectionCleanupShutdownListener.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/AbandonedConnectionCleanupShutdownListener.java new file mode 100644 index 000000000..d5d36c0f6 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/AbandonedConnectionCleanupShutdownListener.java @@ -0,0 +1,54 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.infrastructure.core.service; + +import java.sql.Driver; +import java.sql.DriverManager; +import java.util.Enumeration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextStoppedEvent; +import org.springframework.stereotype.Service; + +import com.mysql.jdbc.AbandonedConnectionCleanupThread; + +@Service +public class AbandonedConnectionCleanupShutdownListener implements ApplicationListener { + + private final static Logger logger = LoggerFactory.getLogger(AbandonedConnectionCleanupShutdownListener.class); + + @Override + public void onApplicationEvent(ContextStoppedEvent event) { + shutDowncleanUpThreadAndDeregisterJDBCDriver(); + } + + private void shutDowncleanUpThreadAndDeregisterJDBCDriver() { + try { + AbandonedConnectionCleanupThread.shutdown(); + logger.info("Shut-down of AbandonedConnectionCleanupThread successful"); + } catch (Throwable t) { + logger.error("Exception occurred while shut-down of AbandonedConnectionCleanupThread", t); + } + + // This manually deregisters JDBC driver, which prevents Tomcat 7 from + // complaining about memory leaks + Enumeration drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { + Driver driver = drivers.nextElement(); + try { + java.sql.DriverManager.deregisterDriver(driver); + logger.info("JDBC driver de-registered successfully"); + } catch (Throwable t) { + logger.error("Exception occured while deristering jdbc driver", t); + } + } + try { + Thread.sleep(2000L); + } catch (Exception e) {} + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterService.java index 641c2232f..82e14d916 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterService.java @@ -19,4 +19,6 @@ public interface JobRegisterService { public void stopScheduler(String name); + public void stopAllSchedulers(); + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java index 35a639a72..c401cf7a5 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/jobs/service/JobRegisterServiceImpl.java @@ -35,6 +35,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextStoppedEvent; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; @@ -46,7 +48,7 @@ import org.springframework.stereotype.Service; * {@link CronTriggerFactoryBean} */ @Service -public class JobRegisterServiceImpl implements JobRegisterService { +public class JobRegisterServiceImpl implements JobRegisterService, ApplicationListener { private final static Logger logger = LoggerFactory.getLogger(JobRegisterServiceImpl.class); @@ -228,6 +230,11 @@ public class JobRegisterServiceImpl implements JobRegisterService { return !this.schedularWritePlatformService.retriveSchedulerDetail().isSuspended(); } + @Override + public void onApplicationEvent(ContextStoppedEvent event) { + this.stopAllSchedulers(); + } + private void scheduleJob(final ScheduledJobDetail scheduledJobDetails) { if (!scheduledJobDetails.isActiveSchedular()) { scheduledJobDetails.updateNextRunTime(null); @@ -251,6 +258,17 @@ public class JobRegisterServiceImpl implements JobRegisterService { scheduledJobDetails.updateCurrentlyRunningStatus(false); } + @Override + public void stopAllSchedulers() { + for (Scheduler scheduler : this.schedulers.values()) { + try { + scheduler.shutdown(); + } catch (final SchedulerException e) { + logger.error(e.getMessage(), e); + } + } + } + private Scheduler getScheduler(final ScheduledJobDetail scheduledJobDetail) throws Exception { final String schedulername = getSchedulerName(scheduledJobDetail); Scheduler scheduler = this.schedulers.get(schedulername); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/data/LoanChargePaidDetail.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/data/LoanChargePaidDetail.java index 47f9b78aa..179a964c6 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/data/LoanChargePaidDetail.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/data/LoanChargePaidDetail.java @@ -1,3 +1,8 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ package org.mifosplatform.portfolio.loanaccount.data; import org.mifosplatform.organisation.monetary.domain.Money; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepaymentScheduleInstallmentRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepaymentScheduleInstallmentRepository.java index d8975cfcd..f646fe83f 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepaymentScheduleInstallmentRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepaymentScheduleInstallmentRepository.java @@ -1,3 +1,8 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ package org.mifosplatform.portfolio.loanaccount.domain; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculatedSchedule.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculatedSchedule.java index f458cfab0..d9847269c 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculatedSchedule.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculatedSchedule.java @@ -1,3 +1,8 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ package org.mifosplatform.portfolio.loanaccount.loanschedule.domain; public class RecalculatedSchedule { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculationDetail.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculationDetail.java index a1e91799d..916757a43 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculationDetail.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/loanschedule/domain/RecalculationDetail.java @@ -1,3 +1,8 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ package org.mifosplatform.portfolio.loanaccount.loanschedule.domain; import org.joda.time.LocalDate;