mirror of https://github.com/longtai-cn/hippo4j
parent
7d23411556
commit
9a5fadb1a6
@ -0,0 +1,92 @@
|
|||||||
|
package io.dynamic.threadpool.common.model;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pool Run State Info.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/7/7 18:57
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class PoolRunStateInfo implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前负载
|
||||||
|
*/
|
||||||
|
private String currentLoad;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 峰值负载
|
||||||
|
*/
|
||||||
|
private String peakLoad;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程池 ID
|
||||||
|
*/
|
||||||
|
private String tpId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 核心线程数
|
||||||
|
*/
|
||||||
|
private Integer coreSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最大线程数
|
||||||
|
*/
|
||||||
|
private Integer maximumSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程池当前线程数
|
||||||
|
*/
|
||||||
|
private Integer poolSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 活跃线程数
|
||||||
|
*/
|
||||||
|
private Integer activeSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程池中同时进入的最大线程数
|
||||||
|
*/
|
||||||
|
private Integer largestPoolSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 队列类型
|
||||||
|
*/
|
||||||
|
private String queueType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 队列容量
|
||||||
|
*/
|
||||||
|
private Integer queueCapacity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 队列元素个数
|
||||||
|
*/
|
||||||
|
private Integer queueSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 队列剩余容量
|
||||||
|
*/
|
||||||
|
private Integer queueRemainingCapacity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程池中执行任务总数量
|
||||||
|
*/
|
||||||
|
private Long completedTaskCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拒绝策略发生次数
|
||||||
|
*/
|
||||||
|
private Integer regectCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Host
|
||||||
|
*/
|
||||||
|
private String host;
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package io.dynamic.threadpool.starter.controller;
|
||||||
|
|
||||||
|
import io.dynamic.threadpool.common.model.PoolRunStateInfo;
|
||||||
|
import io.dynamic.threadpool.common.web.base.Result;
|
||||||
|
import io.dynamic.threadpool.common.web.base.Results;
|
||||||
|
import io.dynamic.threadpool.starter.handler.ThreadPoolRunStateHandler;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pool Run State Controller.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/7/7 21:34
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class PoolRunStateController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ThreadPoolRunStateHandler threadPoolRunStateHandler;
|
||||||
|
|
||||||
|
@GetMapping("/run/state/{tpId}")
|
||||||
|
public Result<PoolRunStateInfo> getPoolRunState(@PathVariable("tpId") String tpId) {
|
||||||
|
PoolRunStateInfo poolRunState = threadPoolRunStateHandler.getPoolRunState(tpId);
|
||||||
|
return Results.success(poolRunState);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
package io.dynamic.threadpool.starter.handler;
|
||||||
|
|
||||||
|
import io.dynamic.threadpool.common.model.PoolRunStateInfo;
|
||||||
|
import io.dynamic.threadpool.starter.core.GlobalThreadPoolManage;
|
||||||
|
import io.dynamic.threadpool.starter.wrap.CustomThreadPoolExecutor;
|
||||||
|
import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程池运行状态组件.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/7/7 19:37
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ThreadPoolRunStateHandler {
|
||||||
|
|
||||||
|
private static InetAddress addr;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
addr = InetAddress.getLocalHost();
|
||||||
|
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PoolRunStateInfo getPoolRunState(String tpId) {
|
||||||
|
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(tpId);
|
||||||
|
ThreadPoolExecutor pool = executorService.getPool();
|
||||||
|
|
||||||
|
// 核心线程数
|
||||||
|
int corePoolSize = pool.getCorePoolSize();
|
||||||
|
// 最大线程数
|
||||||
|
int maximumPoolSize = pool.getMaximumPoolSize();
|
||||||
|
// 线程池当前线程数
|
||||||
|
int poolSize = pool.getPoolSize();
|
||||||
|
// 活跃线程数
|
||||||
|
int activeCount = pool.getActiveCount();
|
||||||
|
// 同时进入池中的最大线程数
|
||||||
|
int largestPoolSize = pool.getLargestPoolSize();
|
||||||
|
// 线程池中执行任务总数量
|
||||||
|
long completedTaskCount = pool.getCompletedTaskCount();
|
||||||
|
// 当前负载
|
||||||
|
String currentLoad = divide(activeCount, maximumPoolSize);
|
||||||
|
// 峰值负载
|
||||||
|
String peakLoad = divide(largestPoolSize, maximumPoolSize);
|
||||||
|
|
||||||
|
BlockingQueue<Runnable> queue = pool.getQueue();
|
||||||
|
// 队列类型
|
||||||
|
String queueType = queue.getClass().getSimpleName();
|
||||||
|
// 队列元素个数
|
||||||
|
int queueSize = queue.size();
|
||||||
|
// 队列剩余容量
|
||||||
|
int remainingCapacity = queue.remainingCapacity();
|
||||||
|
// 队列容量
|
||||||
|
int queueCapacity = queueSize + remainingCapacity;
|
||||||
|
|
||||||
|
PoolRunStateInfo stateInfo = new PoolRunStateInfo();
|
||||||
|
stateInfo.setCoreSize(corePoolSize);
|
||||||
|
stateInfo.setMaximumSize(maximumPoolSize);
|
||||||
|
stateInfo.setPoolSize(poolSize);
|
||||||
|
stateInfo.setActiveSize(activeCount);
|
||||||
|
stateInfo.setCurrentLoad(currentLoad);
|
||||||
|
stateInfo.setPeakLoad(peakLoad);
|
||||||
|
stateInfo.setQueueType(queueType);
|
||||||
|
stateInfo.setQueueSize(queueSize);
|
||||||
|
stateInfo.setQueueRemainingCapacity(remainingCapacity);
|
||||||
|
stateInfo.setQueueCapacity(queueCapacity);
|
||||||
|
stateInfo.setLargestPoolSize(largestPoolSize);
|
||||||
|
stateInfo.setCompletedTaskCount(completedTaskCount);
|
||||||
|
stateInfo.setHost(addr.getHostAddress());
|
||||||
|
stateInfo.setTpId(tpId);
|
||||||
|
|
||||||
|
int regectCount = pool instanceof CustomThreadPoolExecutor
|
||||||
|
? ((CustomThreadPoolExecutor) pool).getRegectCount()
|
||||||
|
: -1;
|
||||||
|
stateInfo.setRegectCount(regectCount);
|
||||||
|
|
||||||
|
return stateInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String divide(int num1, int num2) {
|
||||||
|
return ((int) (Double.parseDouble(num1 + "") / Double.parseDouble(num2 + "") * 100)) + "%";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue