Commit 5f8b3d5a by liuyu

2023年05/16 线程池配置

parent 69b7b452
...@@ -15,15 +15,15 @@ public class TaskPoolConfig { ...@@ -15,15 +15,15 @@ public class TaskPoolConfig {
public ThreadPoolTaskExecutor autMarkExecutor() { public ThreadPoolTaskExecutor autMarkExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 配置核心线程数 // 配置核心线程数
executor.setCorePoolSize(15); executor.setCorePoolSize(32);
// 配置最大线程数 // 配置最大线程数
executor.setMaxPoolSize(25); executor.setMaxPoolSize(32);
// 配置线程池中的线程的名称前缀 // 配置线程池中的线程的名称前缀
executor.setThreadNamePrefix("autoMark-executor-"); executor.setThreadNamePrefix("autoMark-executor-");
executor.setQueueCapacity(50); executor.setQueueCapacity(2000);
// rejection-policy:当pool已经达到max size的时候,如何处理新任务 // rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行 // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
// 执行初始化 // 执行初始化
executor.initialize(); executor.initialize();
return executor; return executor;
...@@ -31,7 +31,7 @@ public class TaskPoolConfig { ...@@ -31,7 +31,7 @@ public class TaskPoolConfig {
@Bean("templateExecutor") @Bean("templateExecutor")
public ThreadPoolExecutorTimeout templateExecutor() { public ThreadPoolExecutorTimeout templateExecutor() {
return new ThreadPoolExecutorTimeout(1000L * 60 * 30, 10, 20,10000L, return new ThreadPoolExecutorTimeout(1000L * 60 * 30, 32, 32,10000L,
TimeUnit.MILLISECONDS, 100, "模板任务线程池"); TimeUnit.MILLISECONDS, 200, "模板任务线程池");
} }
} }
...@@ -33,10 +33,12 @@ public class TaskServiceHandler { ...@@ -33,10 +33,12 @@ public class TaskServiceHandler {
for (TaskService taskService : SERVICE_LIST) { for (TaskService taskService : SERVICE_LIST) {
TaskType taskType = taskService.supports(autoTask.getType()); TaskType taskType = taskService.supports(autoTask.getType());
if (Objects.nonNull(taskType)) { if (Objects.nonNull(taskType)) {
if (taskService.thresholdWarn()) { try {
log.error("任务类型:{},当前运行任务已到达最大核心数", autoTask.getType());
}
taskService.runTask(autoTask); taskService.runTask(autoTask);
taskService.executorStatus();
} catch (Exception e) {
log.error("任务类型:{},执行异常:", taskType.getType(), e);
}
return taskType.getCacheId(); return taskType.getCacheId();
} }
} }
......
...@@ -6,6 +6,8 @@ import com.zhiwei.middleware.automatic.server.pojo.enums.TaskType; ...@@ -6,6 +6,8 @@ import com.zhiwei.middleware.automatic.server.pojo.enums.TaskType;
public interface TaskService { public interface TaskService {
String executorLog = "name:{}任务线程池信息:当前活跃线程{},队列长度:{}";
/** /**
* 任务类型匹配 * 任务类型匹配
* @return 名字 * @return 名字
...@@ -22,5 +24,5 @@ public interface TaskService { ...@@ -22,5 +24,5 @@ public interface TaskService {
* 任务运行阈值预警 * 任务运行阈值预警
* @return 是否进行预警 * @return 是否进行预警
*/ */
boolean thresholdWarn(); void executorStatus();
} }
...@@ -81,8 +81,9 @@ public class TaskServiceCommon extends BaseTaskTypePair<TaskServiceCommon.TaskCo ...@@ -81,8 +81,9 @@ public class TaskServiceCommon extends BaseTaskTypePair<TaskServiceCommon.TaskCo
} }
@Override @Override
public boolean thresholdWarn() { public void executorStatus() {
return autoMarkExecutor.getActiveCount() == autoMarkExecutor.getCorePoolSize(); log.info(executorLog, "自动标注",autoMarkExecutor.getThreadPoolExecutor().getActiveCount(),
autoMarkExecutor.getThreadPoolExecutor().getQueue().size());
} }
/** /**
......
...@@ -90,9 +90,8 @@ public class TaskServiceTemplate extends BaseTaskTypePair<TaskServiceTemplate.Ta ...@@ -90,9 +90,8 @@ public class TaskServiceTemplate extends BaseTaskTypePair<TaskServiceTemplate.Ta
} }
@Override @Override
public boolean thresholdWarn() { public void executorStatus() {
// return executor.getActiveCount() == executor.getCorePoolSize(); log.info(executorLog, "模板聚合",executor.getActiveCount(), executor.getQueue().size());
return false;
} }
private void templateRecordClean(AutoTask autoTask) { private void templateRecordClean(AutoTask autoTask) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment