mirror of https://github.com/longtai-cn/hippo4j
parent
de946e78a6
commit
1bd367dcb7
@ -1,4 +1,4 @@
|
|||||||
package cn.hippo4j.starter.toolkit;
|
package cn.hippo4j.common.toolkit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字节转换工具类.
|
* 字节转换工具类.
|
@ -1,13 +1,13 @@
|
|||||||
package cn.hippo4j.starter.handler;
|
package cn.hippo4j.core.executor.state;
|
||||||
|
|
||||||
import cn.hippo4j.common.model.ManyPoolRunStateInfo;
|
import cn.hippo4j.common.model.ManyPoolRunStateInfo;
|
||||||
import cn.hippo4j.common.model.PoolRunStateInfo;
|
import cn.hippo4j.common.model.PoolRunStateInfo;
|
||||||
|
import cn.hippo4j.common.toolkit.ByteConvertUtil;
|
||||||
import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
|
import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
|
||||||
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
|
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
|
||||||
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
||||||
import cn.hippo4j.core.executor.support.AbstractDynamicExecutorSupport;
|
import cn.hippo4j.core.executor.support.AbstractDynamicExecutorSupport;
|
||||||
import cn.hippo4j.core.toolkit.inet.InetUtils;
|
import cn.hippo4j.core.toolkit.inet.InetUtils;
|
||||||
import cn.hippo4j.starter.toolkit.ByteConvertUtil;
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.system.RuntimeInfo;
|
import cn.hutool.system.RuntimeInfo;
|
@ -1,4 +1,4 @@
|
|||||||
package cn.hippo4j.starter.handler;
|
package cn.hippo4j.core.executor.state;
|
||||||
|
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
@ -1,4 +1,4 @@
|
|||||||
package cn.hippo4j.common.web.executor;
|
package cn.hippo4j.core.executor.web;
|
||||||
|
|
||||||
import cn.hippo4j.common.config.ApplicationContextHolder;
|
import cn.hippo4j.common.config.ApplicationContextHolder;
|
||||||
import cn.hippo4j.common.web.exception.ServiceException;
|
import cn.hippo4j.common.web.exception.ServiceException;
|
@ -1,10 +1,10 @@
|
|||||||
package cn.hippo4j.starter.handler.web;
|
package cn.hippo4j.core.executor.web;
|
||||||
|
|
||||||
import cn.hippo4j.common.model.PoolBaseInfo;
|
import cn.hippo4j.common.model.PoolBaseInfo;
|
||||||
import cn.hippo4j.common.model.PoolRunStateInfo;
|
import cn.hippo4j.common.model.PoolRunStateInfo;
|
||||||
|
import cn.hippo4j.common.toolkit.ByteConvertUtil;
|
||||||
import cn.hippo4j.common.toolkit.ReflectUtil;
|
import cn.hippo4j.common.toolkit.ReflectUtil;
|
||||||
import cn.hippo4j.starter.handler.AbstractThreadPoolRuntime;
|
import cn.hippo4j.core.executor.state.AbstractThreadPoolRuntime;
|
||||||
import cn.hippo4j.starter.toolkit.ByteConvertUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.system.RuntimeInfo;
|
import cn.hutool.system.RuntimeInfo;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
@ -0,0 +1,37 @@
|
|||||||
|
package cn.hippo4j.core.starter.monitor;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.PoolRunStateInfo;
|
||||||
|
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
||||||
|
import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract dynamic thread-pool monitor.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2022/3/25 12:07
|
||||||
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public abstract class AbstractDynamicThreadPoolMonitor implements DynamicThreadPoolMonitor {
|
||||||
|
|
||||||
|
private final ThreadPoolRunStateHandler threadPoolRunStateHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute.
|
||||||
|
*
|
||||||
|
* @param poolRunStateInfo
|
||||||
|
*/
|
||||||
|
protected abstract void execute(PoolRunStateInfo poolRunStateInfo);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void collect() {
|
||||||
|
List<String> listDynamicThreadPoolId = GlobalThreadPoolManage.listThreadPoolId();
|
||||||
|
for (String each : listDynamicThreadPoolId) {
|
||||||
|
PoolRunStateInfo poolRunState = threadPoolRunStateHandler.getPoolRunState(each);
|
||||||
|
execute(poolRunState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package cn.hippo4j.core.starter.monitor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool monitor.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2022/3/25 19:03
|
||||||
|
*/
|
||||||
|
public interface DynamicThreadPoolMonitor extends ThreadPoolMonitor {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,83 @@
|
|||||||
|
package cn.hippo4j.core.starter.monitor;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.config.ApplicationContextHolder;
|
||||||
|
import cn.hippo4j.common.toolkit.StringUtil;
|
||||||
|
import cn.hippo4j.core.executor.support.ThreadFactoryBuilder;
|
||||||
|
import cn.hippo4j.core.spi.DynamicThreadPoolServiceLoader;
|
||||||
|
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic thread-pool monitor executor.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2022/3/25 19:29
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DynamicThreadPoolMonitorExecutor implements ApplicationRunner {
|
||||||
|
|
||||||
|
private final BootstrapCoreProperties properties;
|
||||||
|
|
||||||
|
private ScheduledThreadPoolExecutor collectExecutor;
|
||||||
|
|
||||||
|
private List<ThreadPoolMonitor> threadPoolMonitors;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
String collectType = properties.getCollectType();
|
||||||
|
if (!properties.getCollect() || StringUtil.isBlank(collectType)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Start monitoring the running status of dynamic thread pool.");
|
||||||
|
threadPoolMonitors = Lists.newArrayList();
|
||||||
|
|
||||||
|
String collectTaskName = "client.scheduled.collect.data";
|
||||||
|
collectExecutor = new ScheduledThreadPoolExecutor(
|
||||||
|
new Integer(1),
|
||||||
|
ThreadFactoryBuilder.builder().daemon(true).prefix(collectTaskName).build()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get dynamic thread pool monitoring component.
|
||||||
|
List<String> collectTypes = Arrays.asList(collectType.split(","));
|
||||||
|
ApplicationContextHolder.getBeansOfType(ThreadPoolMonitor.class)
|
||||||
|
.forEach((key, val) -> {
|
||||||
|
if (collectTypes.contains(val.getType())) {
|
||||||
|
threadPoolMonitors.add(val);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Collection<DynamicThreadPoolMonitor> dynamicThreadPoolMonitors =
|
||||||
|
DynamicThreadPoolServiceLoader.getSingletonServiceInstances(DynamicThreadPoolMonitor.class);
|
||||||
|
dynamicThreadPoolMonitors.stream().filter(each -> collectTypes.contains(each.getType())).forEach(each -> threadPoolMonitors.add(each));
|
||||||
|
|
||||||
|
// Execute dynamic thread pool monitoring component.
|
||||||
|
collectExecutor.scheduleWithFixedDelay(
|
||||||
|
() -> scheduleRunnable(),
|
||||||
|
properties.getInitialDelay(),
|
||||||
|
properties.getCollectInterval(),
|
||||||
|
TimeUnit.MILLISECONDS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduleRunnable() {
|
||||||
|
for (ThreadPoolMonitor each : threadPoolMonitors) {
|
||||||
|
try {
|
||||||
|
each.collect();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("Error monitoring the running status of dynamic thread pool. Type :: {}", each.getType(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package cn.hippo4j.core.starter.monitor;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.PoolRunStateInfo;
|
||||||
|
import cn.hippo4j.common.toolkit.JSONUtil;
|
||||||
|
import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log monitor handler.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2022/3/25 19:22
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class LogMonitorHandler extends AbstractDynamicThreadPoolMonitor {
|
||||||
|
|
||||||
|
public LogMonitorHandler(ThreadPoolRunStateHandler threadPoolRunStateHandler) {
|
||||||
|
super(threadPoolRunStateHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void execute(PoolRunStateInfo poolRunStateInfo) {
|
||||||
|
log.info("{}", JSONUtil.toJSONString(poolRunStateInfo));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return "log";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package cn.hippo4j.core.starter.monitor;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.model.PoolRunStateInfo;
|
||||||
|
import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Metric monitor handler.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2022/3/25 20:37
|
||||||
|
*/
|
||||||
|
public class MetricMonitorHandler extends AbstractDynamicThreadPoolMonitor {
|
||||||
|
|
||||||
|
public MetricMonitorHandler(ThreadPoolRunStateHandler threadPoolRunStateHandler) {
|
||||||
|
super(threadPoolRunStateHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void execute(PoolRunStateInfo poolRunStateInfo) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return "metric";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package cn.hippo4j.core.starter.monitor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread-pool monitor.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2022/3/25 19:03
|
||||||
|
*/
|
||||||
|
public interface ThreadPoolMonitor {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get type.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect data.
|
||||||
|
*/
|
||||||
|
void collect();
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package cn.hippo4j.starter.handler;
|
package cn.hippo4j.starter.core;
|
||||||
|
|
||||||
import cn.hippo4j.common.api.ThreadDetailState;
|
import cn.hippo4j.common.api.ThreadDetailState;
|
||||||
import cn.hippo4j.common.model.ThreadDetailStateInfo;
|
import cn.hippo4j.common.model.ThreadDetailStateInfo;
|
Loading…
Reference in new issue