package com.ard.qs.task; import com.ard.qs.service.IRedisCatchStorageService; import com.ard.qs.utils.SystemInfoUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; /** * 获取系统信息写入redis */ @Slf4j @Component public class SystemInfoTimerTask { @Autowired private IRedisCatchStorageService redisCatchStorageService; @Scheduled(fixedRate = 2000) //每2秒执行一次 public void execute() { try { double cpuInfo = SystemInfoUtils.getCpuInfo(); redisCatchStorageService.addCpuInfo(cpuInfo); double memInfo = SystemInfoUtils.getMemInfo(); redisCatchStorageService.addMemInfo(memInfo); Map networkInterfaces = SystemInfoUtils.getNetworkInterfaces(); redisCatchStorageService.addNetInfo(networkInterfaces); List> diskInfo = SystemInfoUtils.getDiskInfo(); redisCatchStorageService.addDiskInfo(diskInfo); } catch (InterruptedException e) { log.error("[获取系统信息失败] {}", e.getMessage()); Thread.currentThread().interrupt(); } catch (Exception e) { log.error("[获取系统信息异常] {}", e.getMessage(), e); } } }