diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java index 9cd515ef..d98e143b 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import java.util.concurrent.BlockingQueue; import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; /** * Rejected policies. @@ -15,12 +16,12 @@ import java.util.concurrent.RejectedExecutionHandler; public class RejectedPolicies { /** - * 发生拒绝事件时, 添加新任务并运行最早的任务 - * - * @return + * 发生拒绝事件时, 添加新任务并运行最早的任务. */ - public static RejectedExecutionHandler runsOldestTaskPolicy() { - return (r, executor) -> { + public static class RunsOldestTaskPolicy implements RejectedExecutionHandler { + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } @@ -33,16 +34,17 @@ public class RejectedPolicies { if (!newTaskAdd) { executor.execute(r); } - }; + } + } /** - * 使用阻塞方法将拒绝任务添加队列, 可保证任务不丢失 - * - * @return + * 使用阻塞方法将拒绝任务添加队列, 可保证任务不丢失. */ - public static RejectedExecutionHandler syncPutQueuePolicy() { - return (r, executor) -> { + public static class SyncPutQueuePolicy implements RejectedExecutionHandler { + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } @@ -51,7 +53,8 @@ public class RejectedPolicies { } catch (InterruptedException e) { log.error("Adding Queue task to thread pool failed.", e); } - }; + } + } } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java index afd186cd..c4e9faa0 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java @@ -42,12 +42,12 @@ public enum RejectedTypeEnum { /** * 发生拒绝事件时, 添加新任务并运行最早的任务 */ - RUNS_OLDEST_TASK_POLICY(5, RejectedPolicies.runsOldestTaskPolicy()), + RUNS_OLDEST_TASK_POLICY(5, new RejectedPolicies.RunsOldestTaskPolicy()), /** * 使用阻塞方法将拒绝任务添加队列, 可保证任务不丢失 */ - SYNC_PUT_QUEUE_POLICY(6, RejectedPolicies.syncPutQueuePolicy()); + SYNC_PUT_QUEUE_POLICY(6, new RejectedPolicies.SyncPutQueuePolicy()); /** * 类型