diff --git a/pom.xml b/pom.xml index 7ac77dc..3d5db6d 100644 --- a/pom.xml +++ b/pom.xml @@ -146,6 +146,7 @@ org.slf4j jcl-over-slf4j + test diff --git a/src/main/java/com/guicedee/guicedinjection/JobService.java b/src/main/java/com/guicedee/guicedinjection/JobService.java index 89f8681..8c09c52 100644 --- a/src/main/java/com/guicedee/guicedinjection/JobService.java +++ b/src/main/java/com/guicedee/guicedinjection/JobService.java @@ -35,8 +35,13 @@ public class JobService private static TimeUnit defaultWaitUnit = TimeUnit.SECONDS; private static final JobService INSTANCE = new JobService(); + private static ExecutorService jobCleanup = null; public static JobService getInstance(){ + if (jobCleanup == null) + { + jobCleanup = INSTANCE.jobCleanup(); + } return INSTANCE; } @@ -223,6 +228,29 @@ public void waitForJob(String jobName, long timeout, TimeUnit unit) service.close(); } + private ExecutorService jobCleanup() + { + ScheduledExecutorService jobsShutdownNotClosed = addPollingJob("JobsShutdownNotClosed", () -> { + for (String jobPool : getInstance().getJobPools()) + { + ExecutorService executorService = serviceMap.get(jobPool); + if(executorService.isShutdown() && !executorService.isTerminated()) + { + log.fine("Closing unfinished job - " + jobPool); + removeJob(jobPool); + } + if(executorService.isShutdown() && executorService.isTerminated()) + { + log.fine("Cleaning terminated job - " + jobPool); + executorService.close(); + serviceMap.remove(jobPool); + } + } + }, 2, TimeUnit.MINUTES); + + return jobsShutdownNotClosed; + } + /** * Adds a static run once job to the monitored collections *