From 2e84909a63478ee518dcabe333b13ff621537b15 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Wed, 20 May 2015 18:38:39 +0900 Subject: [PATCH] Allow setting the ScheduledThreadPoolExecutor. --- .../java/com/zaxxer/hikari/HikariConfig.java | 22 +++++++++++++++++++ .../com/zaxxer/hikari/pool/HikariPool.java | 13 +++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index f753f12f..a866f806 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -26,6 +26,7 @@ import java.nio.file.Paths; import java.util.Properties; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -87,6 +88,7 @@ public class HikariConfig implements HikariConfigMBean private DataSource dataSource; private Properties dataSourceProperties; private ThreadFactory threadFactory; + private ScheduledThreadPoolExecutor scheduledExecutor; private Object metricRegistry; private Object healthCheckRegistry; private Properties healthCheckProperties; @@ -643,6 +645,26 @@ public class HikariConfig implements HikariConfigMBean this.poolName = poolName; } + /** + * Get the ScheduledExecutorService used for housekeeping. + * + * @return the executor + */ + public ScheduledThreadPoolExecutor getScheduledExecutorService() + { + return scheduledExecutor; + } + + /** + * Set the ScheduledExecutorService used for housekeeping. + * + * @param executor the ScheduledExecutorService + */ + public void setScheduledExecutorService(ScheduledThreadPoolExecutor executor) + { + this.scheduledExecutor = executor; + } + public String getTransactionIsolation() { return transactionIsolationName; diff --git a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java index b18ccaf3..956c9a93 100644 --- a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java +++ b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java @@ -148,10 +148,15 @@ public class HikariPool implements HikariPoolMBean, IBagStateListener this.closeConnectionExecutor = createThreadPoolExecutor(4, "HikariCP connection closer (pool " + config.getPoolName() + ")", config.getThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); ThreadFactory threadFactory = config.getThreadFactory() != null ? config.getThreadFactory() : new DefaultThreadFactory("Hikari Housekeeping Timer (pool " + config.getPoolName() + ")", true); - this.houseKeepingExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy()); - this.houseKeepingExecutorService.scheduleAtFixedRate(new HouseKeeper(), HOUSEKEEPING_PERIOD_MS, HOUSEKEEPING_PERIOD_MS, TimeUnit.MILLISECONDS); - this.houseKeepingExecutorService.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); - this.houseKeepingExecutorService.setRemoveOnCancelPolicy(true); + if (config.getScheduledExecutorService() != null) { + this.houseKeepingExecutorService = config.getScheduledExecutorService(); + } + else { + this.houseKeepingExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy()); + this.houseKeepingExecutorService.scheduleAtFixedRate(new HouseKeeper(), HOUSEKEEPING_PERIOD_MS, HOUSEKEEPING_PERIOD_MS, TimeUnit.MILLISECONDS); + this.houseKeepingExecutorService.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + this.houseKeepingExecutorService.setRemoveOnCancelPolicy(true); + } this.leakTask = (config.getLeakDetectionThreshold() == 0) ? LeakTask.NO_LEAK : new LeakTask(config.getLeakDetectionThreshold(), houseKeepingExecutorService); poolUtils.setLoginTimeout(dataSource, connectionTimeout);