diff --git a/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java b/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java index 7cfbd15d6..d1cbcbf9f 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java +++ b/common/src/main/java/org/dromara/dynamictp/common/constant/DynamicTpConst.java @@ -75,6 +75,8 @@ private DynamicTpConst() { } public static final String RUN_TIMEOUT = "runTimeout"; + public static final String TRY_INTERRUPT_WHEN_TIMEOUT = "tryInterrupt"; + public static final String QUEUE_TIMEOUT = "queueTimeout"; public static final String TASK_WRAPPERS = "taskWrappers"; diff --git a/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java b/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java index 2bd7389ed..1e21bbc45 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java +++ b/common/src/main/java/org/dromara/dynamictp/common/entity/TpExecutorProps.java @@ -86,6 +86,11 @@ public class TpExecutorProps { */ private long runTimeout = 0; + /** + * If try interrupt task when timeout. + */ + private boolean tryInterrupt = false; + /** * Task queue wait timeout, unit (ms), just for statistics. */ diff --git a/core/src/main/java/org/dromara/dynamictp/core/aware/TaskTimeoutAware.java b/core/src/main/java/org/dromara/dynamictp/core/aware/TaskTimeoutAware.java index 4bb19815d..bce9260aa 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/aware/TaskTimeoutAware.java +++ b/core/src/main/java/org/dromara/dynamictp/core/aware/TaskTimeoutAware.java @@ -53,6 +53,7 @@ protected void refresh(TpExecutorProps props, ThreadPoolStatProvider statProvide if (Objects.nonNull(props)) { statProvider.setRunTimeout(props.getRunTimeout()); statProvider.setQueueTimeout(props.getQueueTimeout()); + statProvider.setTryInterrupt(props.isTryInterrupt()); } } diff --git a/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java b/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java index e4f65e6d0..739138a82 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/executor/DtpExecutor.java @@ -116,6 +116,11 @@ public class DtpExecutor extends ThreadPoolExecutor */ private long runTimeout = 0; + /** + * for manual builder thread pools only + */ + private boolean tryInterrupt = false; + /** * for manual builder thread pools only */ @@ -355,22 +360,30 @@ public void setRejectHandlerType(String rejectHandlerType) { this.rejectHandlerType = rejectHandlerType; } + public long getRunTimeout() { + return runTimeout; + } + public void setRunTimeout(long runTimeout) { this.runTimeout = runTimeout; } - public void setQueueTimeout(long queueTimeout) { - this.queueTimeout = queueTimeout; + public boolean isTryInterrupt() { + return tryInterrupt; } - public long getRunTimeout() { - return runTimeout; + public void setTryInterrupt(boolean tryInterrupt) { + this.tryInterrupt = tryInterrupt; } public long getQueueTimeout() { return queueTimeout; } + public void setQueueTimeout(long queueTimeout) { + this.queueTimeout = queueTimeout; + } + public boolean isWaitForTasksToCompleteOnShutdown() { return waitForTasksToCompleteOnShutdown; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java index 5f56727de..30a257c6b 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java +++ b/core/src/main/java/org/dromara/dynamictp/core/spring/DtpBeanDefinitionRegistrar.java @@ -24,12 +24,12 @@ import org.dromara.dynamictp.common.entity.DtpExecutorProps; import org.dromara.dynamictp.common.properties.DtpProperties; import org.dromara.dynamictp.common.spring.SpringBeanHelper; -import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; +import org.dromara.dynamictp.core.executor.ExecutorType; import org.dromara.dynamictp.core.executor.NamedThreadFactory; +import org.dromara.dynamictp.core.executor.eager.EagerDtpExecutor; +import org.dromara.dynamictp.core.executor.eager.TaskQueue; import org.dromara.dynamictp.core.reject.RejectHandlerGetter; import org.dromara.dynamictp.core.support.BinderHelper; -import org.dromara.dynamictp.core.executor.ExecutorType; -import org.dromara.dynamictp.core.executor.eager.TaskQueue; import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.EnvironmentAware; @@ -55,6 +55,7 @@ import static org.dromara.dynamictp.common.constant.DynamicTpConst.TASK_WRAPPERS; import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_ALIAS_NAME; import static org.dromara.dynamictp.common.constant.DynamicTpConst.THREAD_POOL_NAME; +import static org.dromara.dynamictp.common.constant.DynamicTpConst.TRY_INTERRUPT_WHEN_TIMEOUT; import static org.dromara.dynamictp.common.constant.DynamicTpConst.WAIT_FOR_TASKS_TO_COMPLETE_ON_SHUTDOWN; import static org.dromara.dynamictp.common.em.QueueTypeEnum.buildLbq; import static org.dromara.dynamictp.common.entity.NotifyItem.mergeAllNotifyItems; @@ -104,6 +105,7 @@ private Map buildPropertyValues(DtpExecutorProps props) { propertyValues.put(REJECT_HANDLER_TYPE, props.getRejectedHandlerType()); propertyValues.put(REJECT_ENHANCED, props.isRejectEnhanced()); propertyValues.put(RUN_TIMEOUT, props.getRunTimeout()); + propertyValues.put(TRY_INTERRUPT_WHEN_TIMEOUT, props.isTryInterrupt()); propertyValues.put(QUEUE_TIMEOUT, props.getQueueTimeout()); val notifyItems = mergeAllNotifyItems(props.getNotifyItems()); propertyValues.put(NOTIFY_ITEMS, notifyItems); diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java b/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java index fe3a2a386..8e69a2cd9 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolBuilder.java @@ -169,6 +169,11 @@ public class ThreadPoolBuilder { */ private long runTimeout = 0; + /** + * If try interrupt task when timeout. + */ + private boolean tryInterrupt = false; + /** * Task queue wait timeout, unit (ms), just for statistics. */ @@ -355,6 +360,11 @@ public ThreadPoolBuilder runTimeout(long runTimeout) { return this; } + public ThreadPoolBuilder tryInterrupt(boolean tryInterrupt) { + this.tryInterrupt = tryInterrupt; + return this; + } + public ThreadPoolBuilder queueTimeout(long queueTimeout) { this.queueTimeout = queueTimeout; return this; @@ -480,6 +490,7 @@ private DtpExecutor buildDtpExecutor(ThreadPoolBuilder builder) { dtpExecutor.setPreStartAllCoreThreads(builder.preStartAllCoreThreads); dtpExecutor.setRejectEnhanced(builder.rejectEnhanced); dtpExecutor.setRunTimeout(builder.runTimeout); + dtpExecutor.setTryInterrupt(builder.tryInterrupt); dtpExecutor.setQueueTimeout(builder.queueTimeout); dtpExecutor.setTaskWrappers(builder.taskWrappers); dtpExecutor.setNotifyItems(builder.notifyItems); diff --git a/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolStatProvider.java b/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolStatProvider.java index d576996f7..05c782435 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolStatProvider.java +++ b/core/src/main/java/org/dromara/dynamictp/core/support/ThreadPoolStatProvider.java @@ -48,6 +48,11 @@ public class ThreadPoolStatProvider { */ private long runTimeout = 0; + /** + * Try interrupt task when timeout. + */ + private boolean tryInterrupt = false; + /** * Task queue wait timeout, unit (ms), just for statistics. */ @@ -98,6 +103,7 @@ public static ThreadPoolStatProvider of(ExecutorWrapper executorWrapper) { val dtpExecutor = (DtpExecutor) executorWrapper.getExecutor(); provider.setRunTimeout(dtpExecutor.getRunTimeout()); provider.setQueueTimeout(dtpExecutor.getQueueTimeout()); + provider.setTryInterrupt(dtpExecutor.isTryInterrupt()); } return provider; } @@ -114,6 +120,14 @@ public void setRunTimeout(long runTimeout) { this.runTimeout = runTimeout; } + public boolean isTryInterrupt() { + return tryInterrupt; + } + + public void setTryInterrupt(boolean tryInterrupt) { + this.tryInterrupt = tryInterrupt; + } + public long getQueueTimeout() { return queueTimeout; } diff --git a/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java b/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java index f17623224..70d759bce 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java +++ b/core/src/main/java/org/dromara/dynamictp/core/timer/RunTimeoutTimerTask.java @@ -58,6 +58,9 @@ protected void doRun() { statProvider.getExecutorWrapper().getExecutor().getQueueCapacity(), executor.getQueue().size(), executor.getQueue().remainingCapacity(), traceToString(thread.getStackTrace())); log.warn(logMsg); + if (statProvider.isTryInterrupt()) { + thread.interrupt(); + } } public String traceToString(StackTraceElement[] trace) {