From 9704b3300242268a30938b9e9ad32a1e7543a496 Mon Sep 17 00:00:00 2001 From: kamtohung Date: Sun, 15 Dec 2024 21:01:35 +0800 Subject: [PATCH 1/3] [ISSUE #515] add scheduledFuture cancel check --- .../dromara/dynamictp/common/util/ExecutorUtil.java | 2 +- .../test/core/thread/ScheduledDtpExecutorTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/dromara/dynamictp/common/util/ExecutorUtil.java b/common/src/main/java/org/dromara/dynamictp/common/util/ExecutorUtil.java index 5005638b8..951de5668 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/util/ExecutorUtil.java +++ b/common/src/main/java/org/dromara/dynamictp/common/util/ExecutorUtil.java @@ -51,7 +51,7 @@ private static void tryPrintError(Runnable r, Throwable t) { if (r instanceof FutureTask) { try { FutureTask future = (FutureTask) r; - if (future.isDone()) { + if (future.isDone() && !future.isCancelled()) { future.get(); } } catch (InterruptedException e) { diff --git a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java index cb60abdba..fd9d2f946 100644 --- a/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java +++ b/test/test-core/src/test/java/org/dromara/dynamictp/test/core/thread/ScheduledDtpExecutorTest.java @@ -29,6 +29,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import java.time.LocalDateTime; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @PropertySource(value = "classpath:/dynamic-tp-nacos-demo-dtp-dev.yml", factory = YamlPropertySourceFactory.class) @@ -67,4 +68,14 @@ void testSubNotify() { dtpExecutor14.shutdownNow(); } + @Test + void testScheduleCancel() { + ScheduledDtpExecutor dtpExecutor12 = (ScheduledDtpExecutor) DtpRegistry.getExecutor("dtpExecutor12"); + ScheduledFuture scheduledFuture = dtpExecutor12.scheduleWithFixedDelay(() -> { + System.out.println(Thread.currentThread().getName() + "进来了," + + "当前时间是 "); + }, 0, 1000, TimeUnit.MILLISECONDS); + scheduledFuture.cancel(false); + } + } From 34f36f66ca695045f97accfd37438736a4e43d8b Mon Sep 17 00:00:00 2001 From: yanhom Date: Thu, 26 Dec 2024 21:29:49 +0800 Subject: [PATCH 2/3] setTaskWrappers for ScheduledDtpExecutor's delegate --- .../java/org/dromara/dynamictp/common/util/ReflectionUtil.java | 2 +- .../dromara/dynamictp/core/executor/ScheduledDtpExecutor.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/dromara/dynamictp/common/util/ReflectionUtil.java b/common/src/main/java/org/dromara/dynamictp/common/util/ReflectionUtil.java index d0bb4d184..562562bff 100644 --- a/common/src/main/java/org/dromara/dynamictp/common/util/ReflectionUtil.java +++ b/common/src/main/java/org/dromara/dynamictp/common/util/ReflectionUtil.java @@ -99,7 +99,7 @@ public static Field getField(Class targetClass, String fieldName) { } public static Method findMethod(Class targetClass, String methodName, Class... parameterTypes) { - Method method = MethodUtils.getMatchingAccessibleMethod(targetClass, methodName, parameterTypes); + Method method = MethodUtils.getMatchingMethod(targetClass, methodName, parameterTypes); if (Objects.isNull(method)) { log.warn("Method '{}' with parameters '{}' not found in class '{}'", methodName, parameterTypes, targetClass.getName()); } diff --git a/core/src/main/java/org/dromara/dynamictp/core/executor/ScheduledDtpExecutor.java b/core/src/main/java/org/dromara/dynamictp/core/executor/ScheduledDtpExecutor.java index 23421196c..835053114 100644 --- a/core/src/main/java/org/dromara/dynamictp/core/executor/ScheduledDtpExecutor.java +++ b/core/src/main/java/org/dromara/dynamictp/core/executor/ScheduledDtpExecutor.java @@ -59,6 +59,7 @@ public ScheduledDtpExecutor(int corePoolSize, corePoolSize = corePoolSize == 0 ? 1 : corePoolSize; } delegate = new ScheduledThreadPoolExecutorProxy(new ScheduledThreadPoolExecutor(corePoolSize, threadFactory, handler)); + delegate.setTaskWrappers(getTaskWrappers()); } @Override From 855570d54c70d0c699770d3d81e184b47816e132 Mon Sep 17 00:00:00 2001 From: yanhom Date: Thu, 26 Dec 2024 21:34:09 +0800 Subject: [PATCH 3/3] update readme, add gitcode star img --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5cea1833e..1d8fda150 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ + fork - 备注加群