From febd255f8bcda668032853f920167890e82351f1 Mon Sep 17 00:00:00 2001 From: weihu Date: Tue, 22 Mar 2022 17:00:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notify/request/AlarmNotifyRequest.java | 40 +++++++++++++++++++ .../request/base/BaseNotifyRequest.java | 1 + .../ThreadPoolNotifyAlarmHandler.java | 38 +----------------- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/AlarmNotifyRequest.java b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/AlarmNotifyRequest.java index b28faca6..c525442e 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/AlarmNotifyRequest.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/AlarmNotifyRequest.java @@ -5,6 +5,9 @@ import cn.hippo4j.common.notify.request.base.BaseNotifyRequest; import lombok.Data; import lombok.experimental.Accessors; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; + /** * Alarm notify request. * @@ -115,4 +118,41 @@ public class AlarmNotifyRequest extends BaseNotifyRequest { */ private String executeTimeoutTrace; + public void initThreadPoolExecutorProperty(ThreadPoolExecutor threadPoolExecutor) { + // 核心线程数 + int corePoolSize = threadPoolExecutor.getCorePoolSize(); + // 最大线程数 + int maximumPoolSize = threadPoolExecutor.getMaximumPoolSize(); + // 线程池当前线程数 (有锁) + int poolSize = threadPoolExecutor.getPoolSize(); + // 活跃线程数 (有锁) + int activeCount = threadPoolExecutor.getActiveCount(); + // 同时进入池中的最大线程数 (有锁) + int largestPoolSize = threadPoolExecutor.getLargestPoolSize(); + // 线程池中执行任务总数量 (有锁) + long completedTaskCount = threadPoolExecutor.getCompletedTaskCount(); + + this.corePoolSize = corePoolSize; + this.maximumPoolSize = maximumPoolSize; + this.poolSize = poolSize; + this.activeCount = activeCount; + this.largestPoolSize = largestPoolSize; + this.completedTaskCount = completedTaskCount; + } + + public void initQueueProperty(BlockingQueue queue) { + // 队列元素个数 + int queueSize = queue.size(); + // 队列类型 + String queueType = queue.getClass().getSimpleName(); + // 队列剩余容量 + int remainingCapacity = queue.remainingCapacity(); + // 队列容量 + int queueCapacity = queueSize + remainingCapacity; + + this.queueName = queueType; + this.capacity = queueCapacity; + this.queueSize = queueSize; + this.remainingCapacity = remainingCapacity; + } } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/base/BaseNotifyRequest.java b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/base/BaseNotifyRequest.java index 455e07d3..b15a7845 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/base/BaseNotifyRequest.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/notify/request/base/BaseNotifyRequest.java @@ -41,4 +41,5 @@ public class BaseNotifyRequest implements NotifyRequest { */ private String receives; + } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/ThreadPoolNotifyAlarmHandler.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/ThreadPoolNotifyAlarmHandler.java index 9be4062f..2cf3fbf3 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/ThreadPoolNotifyAlarmHandler.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/ThreadPoolNotifyAlarmHandler.java @@ -216,42 +216,8 @@ public class ThreadPoolNotifyAlarmHandler implements Runnable, CommandLineRunner String appName = StrUtil.isBlank(itemId) ? applicationName : itemId; request.setAppName(appName); - - // 核心线程数 - int corePoolSize = threadPoolExecutor.getCorePoolSize(); - // 最大线程数 - int maximumPoolSize = threadPoolExecutor.getMaximumPoolSize(); - // 线程池当前线程数 (有锁) - int poolSize = threadPoolExecutor.getPoolSize(); - // 活跃线程数 (有锁) - int activeCount = threadPoolExecutor.getActiveCount(); - // 同时进入池中的最大线程数 (有锁) - int largestPoolSize = threadPoolExecutor.getLargestPoolSize(); - // 线程池中执行任务总数量 (有锁) - long completedTaskCount = threadPoolExecutor.getCompletedTaskCount(); - - request.setActive(active.toUpperCase()); - request.setIdentify(IdentifyUtil.getIdentify()); - request.setCorePoolSize(corePoolSize); - request.setMaximumPoolSize(maximumPoolSize); - request.setPoolSize(poolSize); - request.setActiveCount(activeCount); - request.setLargestPoolSize(largestPoolSize); - request.setCompletedTaskCount(completedTaskCount); - - BlockingQueue queue = threadPoolExecutor.getQueue(); - // 队列元素个数 - int queueSize = queue.size(); - // 队列类型 - String queueType = queue.getClass().getSimpleName(); - // 队列剩余容量 - int remainingCapacity = queue.remainingCapacity(); - // 队列容量 - int queueCapacity = queueSize + remainingCapacity; - request.setQueueName(queueType); - request.setCapacity(queueCapacity); - request.setQueueSize(queueSize); - request.setRemainingCapacity(remainingCapacity); + request.initThreadPoolExecutorProperty(threadPoolExecutor); + request.initQueueProperty(threadPoolExecutor.getQueue()); RejectedExecutionHandler rejectedExecutionHandler = threadPoolExecutor instanceof DynamicThreadPoolExecutor ? ((DynamicThreadPoolExecutor) threadPoolExecutor).getRedundancyHandler()