From b8a50a7c4388f3e02dec24abda9e106b6f5b4b4d Mon Sep 17 00:00:00 2001 From: qiushengjie Date: Sat, 2 Sep 2023 11:33:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ms/api/common/TaskBaseService.java | 22 ++++++++++++++++--- .../java/com/ms/api/task/TestTaskService.java | 16 ++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ms-biz/src/main/java/com/ms/api/common/TaskBaseService.java b/ms-biz/src/main/java/com/ms/api/common/TaskBaseService.java index f6a82fbb..ea7e0547 100644 --- a/ms-biz/src/main/java/com/ms/api/common/TaskBaseService.java +++ b/ms-biz/src/main/java/com/ms/api/common/TaskBaseService.java @@ -11,7 +11,23 @@ public abstract class TaskBaseService implements TaskHandler { protected int corePoolSiz = 4; protected int maxPoolSize = 10; protected int queueCapacity = 500; - protected String TaskExecutorName = "TaskPool"; + protected String taskExecutorName = "TaskPool"; + + public int getCorePoolSiz() { + return corePoolSiz; + } + + public int getMaxPoolSize() { + return maxPoolSize; + } + + public int getQueueCapacity() { + return queueCapacity; + } + + public String getTaskExecutorName() { + return taskExecutorName; + } public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); @@ -19,7 +35,7 @@ public abstract class TaskBaseService implements TaskHandler { executor.setCorePoolSize(corePoolSiz + 1); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); - executor.setThreadNamePrefix(TaskExecutorName); + executor.setThreadNamePrefix(taskExecutorName); executor.initialize(); return executor; } @@ -66,7 +82,7 @@ public abstract class TaskBaseService implements TaskHandler { int poolSize = mTaskPool.getPoolSize(); int maxPoolSize = mTaskPool.getMaxPoolSize(); int queueSize = mTaskPool.getThreadPoolExecutor().getQueue().size(); - return String.format("异步方法从线程:%s中被调用!TaskExecutor状态 - 活跃线程数:%d, 线程池大小:%d, 最大线程池大小:%d, 队列大小:%d", + return String.format("异步方法从线程:%s中被调用!状态 - 活跃线程数:%d, 线程池大小:%d, 最大线程池大小:%d, 队列大小:%d", currentThreadName, activeThreads, poolSize, maxPoolSize, queueSize); } diff --git a/ms-biz/src/main/java/com/ms/api/task/TestTaskService.java b/ms-biz/src/main/java/com/ms/api/task/TestTaskService.java index f93384aa..19778fd7 100644 --- a/ms-biz/src/main/java/com/ms/api/task/TestTaskService.java +++ b/ms-biz/src/main/java/com/ms/api/task/TestTaskService.java @@ -2,6 +2,7 @@ package com.ms.api.task; import com.ms.api.common.StrObjMap; import com.ms.api.common.TaskBaseService; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -18,8 +19,19 @@ import java.util.concurrent.Executor; @Slf4j public class TestTaskService extends TaskBaseService { - protected int corePoolSiz = 3; // 同时开启任务处理数量 - protected String TaskExecutorName = "testTaskPool"; // 任务管理器名称 + /** + * 同时开启任务处理数量 + */ + public int getCorePoolSiz() { + return 3; + } + + /** + * 任务管理器名称 + */ + public String getTaskExecutorName() { + return "testTaskPool"; + } @Resource(name = "testTaskPool") protected Executor taskPool;