瀏覽代碼

feat:

1. 数据库连接泄露检测时间修改;
tags/24112201
WendyYang 1 月之前
父節點
當前提交
1ee5c6c01d
共有 4 個文件被更改,包括 6 次插入84 次删除
  1. +0
    -82
      hz-pm-api/src/main/java/com/hz/pm/api/common/handler/CpuLoadTask.java
  2. +5
    -0
      hz-pm-api/src/main/java/com/hz/pm/api/common/util/ThreadPoolUtil.java
  3. +0
    -1
      hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java
  4. +1
    -1
      hz-pm-api/src/main/resources/application-prod.yml

+ 0
- 82
hz-pm-api/src/main/java/com/hz/pm/api/common/handler/CpuLoadTask.java 查看文件

@@ -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));
});
}
}

}

+ 5
- 0
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());
}

}

+ 0
- 1
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());
}

//==================================================================================================================


+ 1
- 1
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
#设置上传 单个文件的大小


Loading…
取消
儲存