添加线程池监控接口

This commit is contained in:
Guoqs
2025-12-22 18:26:08 +08:00
parent 5aae98b0c3
commit 1333278a53

View File

@@ -0,0 +1,130 @@
package com.jsowell.web.controller.monitor;
import com.jsowell.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 线程池监控
*
* @author jsowell
*/
@RestController
@RequestMapping("/monitor/threadpool")
public class ThreadPoolController {
@Autowired
@Qualifier("threadPoolTaskExecutor")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Autowired
@Qualifier("thirdpartyTaskExecutor")
private ThreadPoolTaskExecutor thirdpartyTaskExecutor;
@Autowired
@Qualifier("scheduledExecutorService")
private ScheduledThreadPoolExecutor scheduledExecutorService;
/**
* 获取所有线程池运行状态
*/
@PreAuthorize("@ss.hasPermi('monitor:threadpool:list')")
@GetMapping("/list")
public AjaxResult list() {
List<Map<String, Object>> poolInfoList = new ArrayList<>();
// 充电桩IO线程池
poolInfoList.add(buildTaskExecutorInfo("threadPoolTaskExecutor", "充电桩IO线程池", threadPoolTaskExecutor));
// 第三方推送线程池
poolInfoList.add(buildTaskExecutorInfo("thirdpartyTaskExecutor", "第三方推送线程池", thirdpartyTaskExecutor));
// 定时任务线程池
poolInfoList.add(buildScheduledExecutorInfo("scheduledExecutorService", "定时任务线程池", scheduledExecutorService));
return AjaxResult.success(poolInfoList);
}
/**
* 构建 ThreadPoolTaskExecutor 信息
*/
private Map<String, Object> buildTaskExecutorInfo(String name, String description, ThreadPoolTaskExecutor executor) {
Map<String, Object> info = new HashMap<>();
ThreadPoolExecutor pool = executor.getThreadPoolExecutor();
info.put("name", name);
info.put("description", description);
info.put("type", "ThreadPoolTaskExecutor");
// 配置信息
info.put("corePoolSize", pool.getCorePoolSize());
info.put("maxPoolSize", pool.getMaximumPoolSize());
info.put("queueCapacity", pool.getQueue().size() + pool.getQueue().remainingCapacity());
// 运行状态
info.put("activeCount", pool.getActiveCount());
info.put("poolSize", pool.getPoolSize());
info.put("largestPoolSize", pool.getLargestPoolSize());
info.put("taskCount", pool.getTaskCount());
info.put("completedTaskCount", pool.getCompletedTaskCount());
info.put("queueSize", pool.getQueue().size());
info.put("queueRemainingCapacity", pool.getQueue().remainingCapacity());
// 计算指标
int queueCapacity = pool.getQueue().size() + pool.getQueue().remainingCapacity();
double queueUsageRate = queueCapacity > 0 ? (double) pool.getQueue().size() / queueCapacity * 100 : 0;
double threadUsageRate = pool.getMaximumPoolSize() > 0 ? (double) pool.getActiveCount() / pool.getMaximumPoolSize() * 100 : 0;
info.put("queueUsageRate", String.format("%.2f%%", queueUsageRate));
info.put("threadUsageRate", String.format("%.2f%%", threadUsageRate));
// 拒绝策略
info.put("rejectedExecutionHandler", pool.getRejectedExecutionHandler().getClass().getSimpleName());
return info;
}
/**
* 构建 ScheduledThreadPoolExecutor 信息
*/
private Map<String, Object> buildScheduledExecutorInfo(String name, String description, ScheduledThreadPoolExecutor executor) {
Map<String, Object> info = new HashMap<>();
info.put("name", name);
info.put("description", description);
info.put("type", "ScheduledThreadPoolExecutor");
// 配置信息
info.put("corePoolSize", executor.getCorePoolSize());
info.put("maxPoolSize", executor.getMaximumPoolSize());
// 运行状态
info.put("activeCount", executor.getActiveCount());
info.put("poolSize", executor.getPoolSize());
info.put("largestPoolSize", executor.getLargestPoolSize());
info.put("taskCount", executor.getTaskCount());
info.put("completedTaskCount", executor.getCompletedTaskCount());
info.put("queueSize", executor.getQueue().size());
// 计算线程使用率
double threadUsageRate = executor.getMaximumPoolSize() > 0 ? (double) executor.getActiveCount() / executor.getMaximumPoolSize() * 100 : 0;
info.put("threadUsageRate", String.format("%.2f%%", threadUsageRate));
// 拒绝策略
info.put("rejectedExecutionHandler", executor.getRejectedExecutionHandler().getClass().getSimpleName());
return info;
}
}