diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java index fe10a09b..71cc5bed 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/DynamicThreadPoolExecutor.java @@ -24,7 +24,9 @@ import cn.hippo4j.core.plugin.impl.TaskTimeoutNotifyAlarmPlugin; import cn.hippo4j.core.plugin.impl.ThreadPoolExecutorShutdownPlugin; import cn.hippo4j.core.plugin.manager.DefaultThreadPoolPluginManager; import cn.hippo4j.core.plugin.manager.DefaultThreadPoolPluginRegistrar; +import lombok.Getter; import lombok.NonNull; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.DisposableBean; import org.springframework.core.task.TaskDecorator; @@ -42,6 +44,13 @@ import java.util.concurrent.atomic.AtomicLong; @Slf4j public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor implements DisposableBean { + /** + * wait for tasks to complete on shutdown + */ + @Getter + @Setter + public boolean waitForTasksToCompleteOnShutdown; + /** * Creates a new {@code DynamicThreadPoolExecutor} with the given initial parameters. * @@ -82,10 +91,10 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl threadPoolId, new DefaultThreadPoolPluginManager(), corePoolSize, maximumPoolSize, keepAliveTime, unit, blockingQueue, threadFactory, rejectedExecutionHandler); - log.info("Initializing ExecutorService" + threadPoolId); - + log.info("Initializing ExecutorService {}", threadPoolId); + this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown; // init default plugins - new DefaultThreadPoolPluginRegistrar(executeTimeOut, awaitTerminationMillis, waitForTasksToCompleteOnShutdown) + new DefaultThreadPoolPluginRegistrar(executeTimeOut, awaitTerminationMillis) .doRegister(this); } @@ -116,19 +125,6 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl .orElse(-1L); } - /** - * Is wait for tasks to complete on shutdown. - * - * @return true if instance wait for tasks to complete on shutdown, false other otherwise. - * @deprecated use {@link ThreadPoolExecutorShutdownPlugin} - */ - @Deprecated - public boolean isWaitForTasksToCompleteOnShutdown() { - return getPluginOfType(ThreadPoolExecutorShutdownPlugin.PLUGIN_NAME, ThreadPoolExecutorShutdownPlugin.class) - .map(ThreadPoolExecutorShutdownPlugin::isWaitForTasksToCompleteOnShutdown) - .orElse(false); - } - /** * Set support param. * @@ -138,10 +134,9 @@ public class DynamicThreadPoolExecutor extends ExtensibleThreadPoolExecutor impl */ @Deprecated public void setSupportParam(long awaitTerminationMillis, boolean waitForTasksToCompleteOnShutdown) { + setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown); getPluginOfType(ThreadPoolExecutorShutdownPlugin.PLUGIN_NAME, ThreadPoolExecutorShutdownPlugin.class) - .ifPresent(processor -> processor - .setAwaitTerminationMillis(awaitTerminationMillis) - .setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown)); + .ifPresent(processor -> processor.setAwaitTerminationMillis(awaitTerminationMillis)); } /** diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/impl/ThreadPoolExecutorShutdownPlugin.java b/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/impl/ThreadPoolExecutorShutdownPlugin.java index e61fdfe1..114f2971 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/impl/ThreadPoolExecutorShutdownPlugin.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/impl/ThreadPoolExecutorShutdownPlugin.java @@ -31,9 +31,8 @@ import java.util.List; import java.util.concurrent.*; /** - * After the thread pool calls {@link ThreadPoolExecutor#shutdown()} or {@link ThreadPoolExecutor#shutdownNow()}, - * if necessary, cancel the remaining tasks in the pool, - * and wait for the thread pool to terminate until + *

After the thread pool calls {@link ThreadPoolExecutor#shutdown()} or {@link ThreadPoolExecutor#shutdownNow()}.
+ * Cancel the remaining tasks in the pool, then wait for the thread pool to terminate until * the blocked main thread has timed out or the thread pool has completely terminated. */ @Accessors(chain = true) @@ -60,12 +59,6 @@ public class ThreadPoolExecutorShutdownPlugin implements ShutdownAwarePlugin { @Setter public long awaitTerminationMillis; - /** - * wait for tasks to complete on shutdown - */ - @Setter - public boolean waitForTasksToCompleteOnShutdown; - /** * Callback before pool shutdown. * @@ -77,14 +70,13 @@ public class ThreadPoolExecutorShutdownPlugin implements ShutdownAwarePlugin { ExtensibleThreadPoolExecutor dynamicThreadPoolExecutor = (ExtensibleThreadPoolExecutor) executor; String threadPoolId = dynamicThreadPoolExecutor.getThreadPoolId(); if (log.isInfoEnabled()) { - log.info("Before shutting down ExecutorService" + " '" + threadPoolId + "'"); + log.info("Before shutting down ExecutorService {}", threadPoolId); } } } /** - * Callback after pool shutdown. - * if {@link #waitForTasksToCompleteOnShutdown} return {@code true}, + * Callback after pool shutdown.
* cancel the remaining tasks, * then wait for pool to terminate according {@link #awaitTerminationMillis} if necessary. * @@ -95,7 +87,7 @@ public class ThreadPoolExecutorShutdownPlugin implements ShutdownAwarePlugin { public void afterShutdown(ThreadPoolExecutor executor, List remainingTasks) { if (executor instanceof ExtensibleThreadPoolExecutor) { ExtensibleThreadPoolExecutor pool = (ExtensibleThreadPoolExecutor) executor; - if (!waitForTasksToCompleteOnShutdown && CollectionUtil.isNotEmpty(remainingTasks)) { + if (CollectionUtil.isNotEmpty(remainingTasks)) { remainingTasks.forEach(this::cancelRemainingTask); } awaitTerminationIfNecessary(pool); @@ -110,8 +102,7 @@ public class ThreadPoolExecutorShutdownPlugin implements ShutdownAwarePlugin { @Override public PluginRuntime getPluginRuntime() { return new PluginRuntime(getId()) - .addInfo("awaitTerminationMillis", awaitTerminationMillis) - .addInfo("waitForTasksToCompleteOnShutdown", waitForTasksToCompleteOnShutdown); + .addInfo("awaitTerminationMillis", awaitTerminationMillis); } /** @@ -139,11 +130,13 @@ public class ThreadPoolExecutorShutdownPlugin implements ShutdownAwarePlugin { try { boolean isTerminated = executor.awaitTermination(this.awaitTerminationMillis, TimeUnit.MILLISECONDS); if (!isTerminated && log.isWarnEnabled()) { - log.warn("Timed out while waiting for executor" + " '" + threadPoolId + "'" + " to terminate."); + log.warn("Timed out while waiting for executor {} to terminate.", threadPoolId); + } else { + log.info("ExecutorService {} has been shutdowned.", threadPoolId); } } catch (InterruptedException ex) { if (log.isWarnEnabled()) { - log.warn("Interrupted while waiting for executor" + " '" + threadPoolId + "'" + " to terminate."); + log.warn("Interrupted while waiting for executor {} to terminate.", threadPoolId); } Thread.currentThread().interrupt(); } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrar.java b/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrar.java index 6177014c..211c33a9 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrar.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrar.java @@ -47,11 +47,6 @@ public class DefaultThreadPoolPluginRegistrar implements ThreadPoolPluginRegistr */ private long awaitTerminationMillis; - /** - * wait for tasks to complete on shutdown - */ - private boolean waitForTasksToCompleteOnShutdown; - /** * Get id. * @@ -76,7 +71,7 @@ public class DefaultThreadPoolPluginRegistrar implements ThreadPoolPluginRegistr support.register(new TaskRejectCountRecordPlugin()); support.register(new TaskRejectNotifyAlarmPlugin()); // callback when pool shutdown - support.register(new ThreadPoolExecutorShutdownPlugin(awaitTerminationMillis, waitForTasksToCompleteOnShutdown)); + support.register(new ThreadPoolExecutorShutdownPlugin(awaitTerminationMillis)); } }