diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/common/handler/CpuLoadTask.java b/hz-pm-api/src/main/java/com/hz/pm/api/common/handler/CpuLoadTask.java deleted file mode 100644 index ee69397..0000000 --- a/hz-pm-api/src/main/java/com/hz/pm/api/common/handler/CpuLoadTask.java +++ /dev/null @@ -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; - -/** - *

- * CpuLoadTask - *

- * - * @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)); - }); - } - } - -} diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/common/util/ThreadPoolUtil.java b/hz-pm-api/src/main/java/com/hz/pm/api/common/util/ThreadPoolUtil.java index 7505b9b..5456283 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/common/util/ThreadPoolUtil.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/common/util/ThreadPoolUtil.java @@ -1,5 +1,6 @@ package com.hz.pm.api.common.util; +import cn.hutool.core.date.StopWatch; import cn.hutool.extra.spring.SpringUtil; import com.hz.pm.api.common.config.ThreadPoolUtilProperties; import com.hz.pm.api.common.config.ThreadPoolUtilProperties.ThreadPoolProperties; @@ -29,6 +30,8 @@ public class ThreadPoolUtil { public static final ThreadPoolTaskScheduler SCHEDULER; static { + StopWatch watch = new StopWatch(); + watch.start(); ThreadPoolUtilProperties properties = SpringUtil.getBean(ThreadPoolUtilProperties.class); ThreadPoolProperties requestProps = properties.getRequest(); ThreadPoolTaskExecutor requestExecutor = new MDCThreadPoolTaskExecutor(); @@ -51,6 +54,8 @@ public class ThreadPoolUtil { SCHEDULER.setWaitForTasksToCompleteOnShutdown(schedulerProps.getWaitForTasksToCompleteOnShutdown()); SCHEDULER.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); SCHEDULER.initialize(); + watch.stop(); + log.info("线程池初始化耗时: {} ms", watch.getTotalTimeMillis()); } } diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java index ed43252..dd6dbf3 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java @@ -55,7 +55,6 @@ public class ProjectIdCodeCacheUtil { } watch.stop(); log.info("初始化项目ID编码缓存耗时:{} ms", watch.getTotalTimeMillis()); - log.info("初始化项目ID编码缓存:{} 个", CACHE.size()); } //================================================================================================================== diff --git a/hz-pm-api/src/main/resources/application-prod.yml b/hz-pm-api/src/main/resources/application-prod.yml index 3b7d6e2..aa98713 100644 --- a/hz-pm-api/src/main/resources/application-prod.yml +++ b/hz-pm-api/src/main/resources/application-prod.yml @@ -64,7 +64,7 @@ spring: # 配置从池返回的连接的默认自动提交行为。默认值为true。 auto-commit: true # 开启连接监测泄露 - leak-detection-threshold: 5000 + leak-detection-threshold: 30000 # 测试连接数据库 connection-test-query: SELECT 1 #设置上传 单个文件的大小