@@ -1,82 +0,0 @@ | |||||
package com.hz.pm.api.common.handler; | |||||
import cn.hutool.core.date.StopWatch; | |||||
import cn.hutool.core.util.RandomUtil; | |||||
import cn.hutool.system.oshi.OshiUtil; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | |||||
import org.springframework.context.annotation.Configuration; | |||||
import org.springframework.lang.NonNull; | |||||
import org.springframework.scheduling.annotation.Scheduled; | |||||
import java.util.concurrent.ExecutorService; | |||||
import java.util.concurrent.Executors; | |||||
import java.util.concurrent.ThreadFactory; | |||||
import java.util.concurrent.TimeUnit; | |||||
import java.util.concurrent.atomic.AtomicInteger; | |||||
import java.util.stream.Stream; | |||||
/** | |||||
* <p> | |||||
* CpuLoadTask | |||||
* </p> | |||||
* | |||||
* @author WendyYang | |||||
* @since 20:30 2024/3/5 | |||||
*/ | |||||
@Slf4j | |||||
@Configuration | |||||
@ConditionalOnExpression("${cpu-load-task.open:false}") | |||||
public class CpuLoadTask { | |||||
public CpuLoadTask() { | |||||
CpuLoadThreadFactory threadFactory = new CpuLoadThreadFactory(Thread.MIN_PRIORITY); | |||||
fixedThreadPool = Executors.newFixedThreadPool(1, threadFactory); | |||||
} | |||||
private final ExecutorService fixedThreadPool; | |||||
private static final Integer CPU_LIMIT = 20; | |||||
private static final int LIMIT_COUNT = 1000000000; | |||||
private final AtomicInteger taskNo = new AtomicInteger(0); | |||||
private static class CpuLoadThreadFactory implements ThreadFactory { | |||||
private final int priority; | |||||
public CpuLoadThreadFactory(int priority) { | |||||
this.priority = priority; | |||||
} | |||||
@Override | |||||
public Thread newThread(@NonNull Runnable r) { | |||||
Thread thread = new Thread(r); | |||||
thread.setPriority(priority); | |||||
return thread; | |||||
} | |||||
} | |||||
@Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES) | |||||
public void run() { | |||||
double cpuUsage = OshiUtil.getCpuInfo().getUsed(); | |||||
log.info("当前CPU使用率:{}", cpuUsage); | |||||
if (cpuUsage < CPU_LIMIT) { | |||||
log.info("提交计算任务:{}", taskNo.incrementAndGet()); | |||||
fixedThreadPool.execute(() -> { | |||||
StopWatch watch = new StopWatch(); | |||||
watch.start(); | |||||
long sum = Stream.generate(() -> RandomUtil.randomLong(1, LIMIT_COUNT)) | |||||
.map(w -> w + LIMIT_COUNT / w) | |||||
.limit(LIMIT_COUNT) | |||||
.mapToLong(Long::longValue) | |||||
.sum(); | |||||
watch.stop(); | |||||
log.info("总计:" + sum + " 耗时:" + watch.getTotal(TimeUnit.SECONDS)); | |||||
}); | |||||
} | |||||
} | |||||
} |
@@ -1,5 +1,6 @@ | |||||
package com.hz.pm.api.common.util; | package com.hz.pm.api.common.util; | ||||
import cn.hutool.core.date.StopWatch; | |||||
import cn.hutool.extra.spring.SpringUtil; | import cn.hutool.extra.spring.SpringUtil; | ||||
import com.hz.pm.api.common.config.ThreadPoolUtilProperties; | import com.hz.pm.api.common.config.ThreadPoolUtilProperties; | ||||
import com.hz.pm.api.common.config.ThreadPoolUtilProperties.ThreadPoolProperties; | import com.hz.pm.api.common.config.ThreadPoolUtilProperties.ThreadPoolProperties; | ||||
@@ -29,6 +30,8 @@ public class ThreadPoolUtil { | |||||
public static final ThreadPoolTaskScheduler SCHEDULER; | public static final ThreadPoolTaskScheduler SCHEDULER; | ||||
static { | static { | ||||
StopWatch watch = new StopWatch(); | |||||
watch.start(); | |||||
ThreadPoolUtilProperties properties = SpringUtil.getBean(ThreadPoolUtilProperties.class); | ThreadPoolUtilProperties properties = SpringUtil.getBean(ThreadPoolUtilProperties.class); | ||||
ThreadPoolProperties requestProps = properties.getRequest(); | ThreadPoolProperties requestProps = properties.getRequest(); | ||||
ThreadPoolTaskExecutor requestExecutor = new MDCThreadPoolTaskExecutor(); | ThreadPoolTaskExecutor requestExecutor = new MDCThreadPoolTaskExecutor(); | ||||
@@ -51,6 +54,8 @@ public class ThreadPoolUtil { | |||||
SCHEDULER.setWaitForTasksToCompleteOnShutdown(schedulerProps.getWaitForTasksToCompleteOnShutdown()); | SCHEDULER.setWaitForTasksToCompleteOnShutdown(schedulerProps.getWaitForTasksToCompleteOnShutdown()); | ||||
SCHEDULER.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | SCHEDULER.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||||
SCHEDULER.initialize(); | SCHEDULER.initialize(); | ||||
watch.stop(); | |||||
log.info("线程池初始化耗时: {} ms", watch.getTotalTimeMillis()); | |||||
} | } | ||||
} | } |
@@ -55,7 +55,6 @@ public class ProjectIdCodeCacheUtil { | |||||
} | } | ||||
watch.stop(); | watch.stop(); | ||||
log.info("初始化项目ID编码缓存耗时:{} ms", watch.getTotalTimeMillis()); | log.info("初始化项目ID编码缓存耗时:{} ms", watch.getTotalTimeMillis()); | ||||
log.info("初始化项目ID编码缓存:{} 个", CACHE.size()); | |||||
} | } | ||||
//================================================================================================================== | //================================================================================================================== | ||||
@@ -64,7 +64,7 @@ spring: | |||||
# 配置从池返回的连接的默认自动提交行为。默认值为true。 | # 配置从池返回的连接的默认自动提交行为。默认值为true。 | ||||
auto-commit: true | auto-commit: true | ||||
# 开启连接监测泄露 | # 开启连接监测泄露 | ||||
leak-detection-threshold: 5000 | |||||
leak-detection-threshold: 30000 | |||||
# 测试连接数据库 | # 测试连接数据库 | ||||
connection-test-query: SELECT 1 | connection-test-query: SELECT 1 | ||||
#设置上传 单个文件的大小 | #设置上传 单个文件的大小 | ||||