资源情况监测方法

This commit is contained in:
2026-02-12 11:29:36 +08:00
parent 175238e291
commit e9784f7957
3 changed files with 369 additions and 0 deletions
@@ -0,0 +1,40 @@
package org.jeecg.modules.system.task;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.util.ContainerMetricsUtil;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Slf4j
public class MonitorSystemJob {
@XxlJob(value = "systemInfoJob")
public ReturnT<String> systemInfoJob(String time) {
try {
// Map<String, Object> metrics = SystemInfoUtil.logContainerMetrics();
// // 使用 Spring 的 JSON 工具转为标准 JSON
// String jsonResult = new ObjectMapper().writeValueAsString(metrics);
//
// return new ReturnT<>(jsonResult);
// 第一次调用:初始化 CPU 计算(返回 0%)
ContainerMetricsUtil.logAndCollectMetrics();
// 等待 1 秒以获取有效 CPU 使用率
Thread.sleep(1000);
// 第二次调用:获取真实 CPU 使用率
Map<String, Object> metrics = ContainerMetricsUtil.logAndCollectMetrics();
// 转为 JSON 返回给 XXL-JOB
String json = new ObjectMapper().writeValueAsString(metrics);
return new ReturnT<>(ReturnT.SUCCESS_CODE, json);
} catch (Exception e) {
return ReturnT.FAIL;
}
}
}