mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
close CronThreadFactory to avoid thread leak
This commit is contained in:
@@ -17,15 +17,17 @@ public class CronScheduler implements Scheduler {
|
|||||||
|
|
||||||
private final ScheduledExecutorService executorService;
|
private final ScheduledExecutorService executorService;
|
||||||
private final CronTaskFactory taskFactory;
|
private final CronTaskFactory taskFactory;
|
||||||
|
private final CronThreadFactory threadFactory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public CronScheduler(CronTaskFactory taskFactory) {
|
public CronScheduler(CronTaskFactory taskFactory) {
|
||||||
this.taskFactory = taskFactory;
|
this.taskFactory = taskFactory;
|
||||||
|
this.threadFactory = new CronThreadFactory();
|
||||||
this.executorService = createExecutor();
|
this.executorService = createExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ScheduledExecutorService createExecutor() {
|
private ScheduledExecutorService createExecutor() {
|
||||||
return Executors.newScheduledThreadPool(2, new CronThreadFactory());
|
return Executors.newScheduledThreadPool(2, threadFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -52,6 +54,7 @@ public class CronScheduler implements Scheduler {
|
|||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
LOG.debug("shutdown underlying executor service");
|
LOG.debug("shutdown underlying executor service");
|
||||||
|
threadFactory.close();
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package sonia.scm.schedule;
|
package sonia.scm.schedule;
|
||||||
|
|
||||||
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
import org.apache.shiro.util.ThreadContext;
|
import org.apache.shiro.util.ThreadContext;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@@ -19,7 +20,10 @@ class CronThreadFactory implements ThreadFactory, AutoCloseable {
|
|||||||
|
|
||||||
private static final AtomicLong FACTORY_COUNTER = new AtomicLong();
|
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 long factoryId = FACTORY_COUNTER.incrementAndGet();
|
||||||
private final AtomicLong threadCounter = new AtomicLong();
|
private final AtomicLong threadCounter = new AtomicLong();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user