close CronThreadFactory to avoid thread leak

This commit is contained in:
Sebastian Sdorra
2019-11-21 16:17:30 +01:00
parent ff7b8ca842
commit 755b99f524
2 changed files with 9 additions and 2 deletions

View File

@@ -17,15 +17,17 @@ public class CronScheduler implements Scheduler {
private final ScheduledExecutorService executorService;
private final CronTaskFactory taskFactory;
private final CronThreadFactory threadFactory;
@Inject
public CronScheduler(CronTaskFactory taskFactory) {
this.taskFactory = taskFactory;
this.threadFactory = new CronThreadFactory();
this.executorService = createExecutor();
}
private ScheduledExecutorService createExecutor() {
return Executors.newScheduledThreadPool(2, new CronThreadFactory());
return Executors.newScheduledThreadPool(2, threadFactory);
}
@Override
@@ -52,6 +54,7 @@ public class CronScheduler implements Scheduler {
@Override
public void close() {
LOG.debug("shutdown underlying executor service");
threadFactory.close();
executorService.shutdown();
}
}

View File

@@ -1,5 +1,6 @@
package sonia.scm.schedule;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.shiro.util.ThreadContext;
import java.util.concurrent.ExecutionException;
@@ -19,7 +20,10 @@ class CronThreadFactory implements ThreadFactory, AutoCloseable {
private static final AtomicLong FACTORY_COUNTER = new AtomicLong();
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
private final ExecutorService executorService = Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder().setNameFormat("CronThreadFactory-%d").build()
);
private final long factoryId = FACTORY_COUNTER.incrementAndGet();
private final AtomicLong threadCounter = new AtomicLong();