java獲取JVM的CPU占用率、內存占用率、線程數及服務器的網口吞吐率、磁盤讀寫速率


怎么說呢,本人菜鳥一枚,費了幾天時間,終於做了一個用java獲取JVM的CPU占用率、內存占用率、線程數及服務器的網口吞吐率、磁盤讀寫速率的實現。
其中windows環境下獲取jvm 的cpu占用率這里是參考網上別人的東西(在此感謝提供參考的網友),其他的都是基於自己的想法做出來的。該工具類親測
在win7 64位系統和CentOS6系統沒有問題,其他的暫時沒有測過,歡迎高手指教,勿噴勿噴,感謝手下留情~

運行環境:jdk 1.8 + tomcat 7 + Spring 4.3
tomcat 容器啟動及進入init()進行JVM PID的獲取及執行任務
可能會碰到的問題:無法引入com.sun.management.OperatingSystemMXBean
解決方案:Eclipse默認把這些受訪問限制的API設成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的
Deprecated and restricted API中的Forbidden references(access rules)
選為Warning就可以編譯通過。




import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Formatter;
import java.util.StringTokenizer;
import java.util.Timer;
import java.util.TimerTask;

 
         

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

 
         

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

 
         

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.nufront.euht.model.SysPerformInfo;
import com.nufront.euht.service.api.sys.SysPerformInfoServiceI;
import com.nufront.euht.util.UuidUtil;
import com.sun.management.OperatingSystemMXBean;

 
         

/**
* 網管信息信息信息采集類
*
* @author liming.cen
* @date 2017-4-11
*/
@Service
public class SysInfoAcquirerService {

 
         

private static final int CPUTIME = 5000;
/**
* 網管進程信息采集周期(注意:PERIOD_TIME 一定要大於 SLEEP_TIME )
*/
private static final int PERIOD_TIME = 1000 * 60 * 15;
/**
* 此類中Thread.sleep()里的線程睡眠時間
*/
private static final int SLEEP_TIME = 1000 * 60 * 9;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private String isWindowsOrLinux = isWindowsOrLinux();
private String pid = "";
private Timer sysInfoGetTimer = new Timer("sysInfoGet");
@Autowired
private SysPerformInfoServiceI sysPerformInfoService;

 
         

public Logger log = Logger.getLogger(SysInfoAcquirerService.class);

 
         

/**
* 初始化bean的時候就立即獲取JVM進程的PID及執行任務
*
* @return
*/
@PostConstruct
public void init() {
if (isWindowsOrLinux.equals("windows")) { // 判斷操作系統類型是否為:windows
getJvmPIDOnWindows();
} else {
getJvmPIDOnLinux();
}
sysInfoGetTimer.schedule(new SysInfoAcquirerTimerTask(), 10 * 1000, PERIOD_TIME);
}

 
         

/**
* 判斷是服務器的系統類型是Windows 還是 Linux
*
* @return
*/
public String isWindowsOrLinux() {
String osName = System.getProperty("os.name");
String sysName = "";
if (osName.toLowerCase().startsWith("windows")) {
sysName = "windows";
} else if (osName.toLowerCase().startsWith("linux")) {
sysName = "linux";
}
return sysName;
}

 
         

/**
* 獲取JVM 的CPU占用率(%)
*
* @return
*/
public String getCPURate() {
String cpuRate = "";
if (isWindowsOrLinux.equals("windows")) { // 判斷操作系統類型是否為:windows
cpuRate = getCPURateForWindows();
} else {
cpuRate = getCPURateForLinux();
}
return cpuRate;
}

 
         

/**
* windows環境下獲取JVM的PID
*/
public void getJvmPIDOnWindows() {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
pid = runtime.getName().split("@")[0];
log.info("PID of JVM:" + pid);
}

 
         

/**
* linux環境下獲取JVM的PID
*/
public void getJvmPIDOnLinux() {
String command = "pidof java";
BufferedReader in = null;
Process pro = null;
try {
pro = Runtime.getRuntime().exec(new String[] { "sh", "-c", command });
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
StringTokenizer ts = new StringTokenizer(in.readLine());
pid = ts.nextToken();
log.info("PID of JVM:" + pid);
} catch (IOException e) {
e.printStackTrace();
}
}

 
         

/**
* 獲取JVM的內存占用率(%)
*
* @return
*/
public String getMemoryRate() {
String memRate = "";
if (isWindowsOrLinux.equals("windows")) { // 判斷操作系統類型是否為:windows
memRate = getMemoryRateForWindows();// 查詢windows系統的cpu占用率
} else {
memRate = getMemoryRateForLinux();// 查詢linux系統的cpu占用率
}
return memRate;
}

 
         

/**
* 獲取JVM 線程數
*
* @return
*/
public int getThreadCount() {
int threadCount = 0;
if (isWindowsOrLinux.equals("windows")) { // 判斷操作系統類型是否為:windows
threadCount = getThreadCountForWindows();// 查詢windows系統的線程數
} else {
threadCount = getThreadCountForLinux();// 查詢linux系統的線程數
}
return threadCount;
}

 
         

/**
* 獲取磁盤讀寫速率(MB/s)
*
* @return
*/
// public String getDiskAccess() { // TODO:待完成
// String diskAccess = "";
// if (isWindowsOrLinux.equals("windows")) { // 判斷操作系統類型是否為:windows
// // diskAccess = getDiskAccessForWindows(); // 查詢windows系統的磁盤讀寫速率
// } else {
// diskAccess = getDiskAccessForLinux(); // 查詢linux系統的磁盤讀寫速率
// }
// return diskAccess;
// }

 
         

/**
* 獲取網口吞吐量(MB/s)
*
* @return
*/
public String getNetworkThroughput() {
String throughput = "";
if (isWindowsOrLinux.equals("windows")) { // 判斷操作系統類型是否為:windows
throughput = getNetworkThroughputForWindows(); // 查詢windows系統的磁盤讀寫速率
} else {
throughput = getNetworkThroughputForLinux(); // 查詢linux系統的磁盤讀寫速率
}
return throughput;
}

 
         

/**
* 獲取Windows環境下網口的上下行速率
*
* @return
*/
public String getNetworkThroughputForWindows() {
Process pro1 = null;
Process pro2 = null;
Runtime r = Runtime.getRuntime();
BufferedReader input = null;
String rxPercent = "";
String txPercent = "";
JSONObject jsonObject = new JSONObject();
try {
String command = "netstat -e";
pro1 = r.exec(command);
input = new BufferedReader(new InputStreamReader(pro1.getInputStream()));
String result1[] = readInLine(input, "windows");
Thread.sleep(SLEEP_TIME);
pro2 = r.exec(command);
input = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
String result2[] = readInLine(input, "windows");
rxPercent = formatNumber((Long.parseLong(result2[0]) - Long.parseLong(result1[0]))
/ (float) (1024 * 1024 * (SLEEP_TIME / 1000))); // 上行速率(MB/s)
txPercent = formatNumber((Long.parseLong(result2[1]) - Long.parseLong(result1[1]))
/ (float) (1024 * 1024 * (SLEEP_TIME / 1000))); // 下行速率(MB/s)
input.close();
pro1.destroy();
pro2.destroy();
} catch (Exception e) {
log.error(e.getMessage());
}
jsonObject.put("rxPercent", rxPercent); // 下行速率
jsonObject.put("txPercent", txPercent); // 上行速率
return JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue);
}

 
         

/**
* 獲取Linux環境下網口的上下行速率
*
* @return
*/
public String getNetworkThroughputForLinux() {
Process pro1 = null;
Process pro2 = null;
Runtime r = Runtime.getRuntime();
BufferedReader input = null;
String rxPercent = "";
String txPercent = "";
JSONObject jsonObject = new JSONObject();
try {
String command = "watch ifconfig";
pro1 = r.exec(command);
input = new BufferedReader(new InputStreamReader(pro1.getInputStream()));

 
         

String result1[] = readInLine(input, "linux");
Thread.sleep(SLEEP_TIME);
pro2 = r.exec(command);
input = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
String result2[] = readInLine(input, "linux");
rxPercent = formatNumber((Long.parseLong(result2[0]) - Long.parseLong(result1[0]))
/ (float) (1024 * 1024 * (SLEEP_TIME / 1000))); // 下行速率(MB/s)
txPercent = formatNumber((Long.parseLong(result2[1]) - Long.parseLong(result1[1]))
/ (float) (1024 * 1024 * (SLEEP_TIME / 1000))); // 上行速率(MB/s)
input.close();
pro1.destroy();
pro2.destroy();
} catch (Exception e) {
log.error(e.getMessage());
}
jsonObject.put("rxPercent", rxPercent); // 下行速率
jsonObject.put("txPercent", txPercent); // 上行速率
return JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue);
}

 
         

/**
* 獲取windows環境下JVM的cpu占用率
*
* @return
*/
public String getCPURateForWindows() {
try {
String procCmd = System.getenv("windir") + "\\system32\\wbem\\wmic.exe process "
+ " get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 取進程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
long cpuRate = PERCENT * (busytime) / (busytime + idletime);
if (cpuRate > 100) {
cpuRate = 100;
} else if (cpuRate < 0) {
cpuRate = 0;
}
return String.valueOf(PERCENT * (busytime) / (busytime + idletime));

 
         

} else {
return "0.0";
}
} catch (Exception ex) {
ex.printStackTrace();
return "0.0";
}
}

 
         

/**
* 獲取linux環境下JVM的cpu占用率
*
* @return
*/
public String getCPURateForLinux() {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader brStat = null;
StringTokenizer tokenStat = null;
String user = "";
String linuxVersion = System.getProperty("os.version");
try {
System.out.println("Linux版本: " + linuxVersion);

 
         

Process process = Runtime.getRuntime().exec(new String[] { "sh", "-c", "top -b -p " + pid });
try {
// top命令默認3秒動態更新結果信息,讓線程睡眠5秒以便獲取最新結果
Thread.sleep(CPUTIME);
is = process.getInputStream();
} catch (InterruptedException e) {
e.printStackTrace();
}
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);

 
         

if (linuxVersion.equals("2.4")) {
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();

 
         

tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
user = tokenStat.nextToken();
tokenStat.nextToken();
String system = tokenStat.nextToken();
tokenStat.nextToken();
String nice = tokenStat.nextToken();

 
         

System.out.println(user + " , " + system + " , " + nice);

 
         

user = user.substring(0, user.indexOf("%"));
system = system.substring(0, system.indexOf("%"));
nice = nice.substring(0, nice.indexOf("%"));

 
         

float userUsage = new Float(user).floatValue();
float systemUsage = new Float(system).floatValue();
float niceUsage = new Float(nice).floatValue();
return String.valueOf((userUsage + systemUsage + niceUsage) / 100);
} else {
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
String userUsage = tokenStat.nextToken(); // 用戶空間占用CPU百分比
user = userUsage.substring(0, userUsage.indexOf("%"));
process.destroy();
}

 
         

} catch (IOException ioe) {
System.out.println(ioe.getMessage());
freeResource(is, isr, brStat);
return "100";
} finally {
freeResource(is, isr, brStat);
}
return user; // jvm cpu占用率
}

 
         

/**
* 獲取Linux環境下JVM的內存占用率
*
* @return
*/
public String getMemoryRateForLinux() {
Process pro = null;
Runtime r = Runtime.getRuntime();
String remCount = "";
try {
String command = "top -b -n 1 -H -p" + pid;
pro = r.exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
in.readLine();
in.readLine();
in.readLine();
in.readLine();
in.readLine();
in.readLine();
in.readLine();
StringTokenizer ts = new StringTokenizer(in.readLine());
int i = 1;
while (ts.hasMoreTokens()) {
i++;
ts.nextToken();
if (i == 10) {
remCount = ts.nextToken();
}
}
in.close();
pro.destroy();
} catch (Exception e) {
log.error(e.getMessage());
}
return remCount;
}

 
         

/**
* 獲取windows環境下jvm的內存占用率
*
* @return
*/
public String getMemoryRateForWindows() {
String command = "TASKLIST /NH /FO CSV /FI \"PID EQ " + pid + " \"";
String remCount = ""; // jvm物理內存占用量
BufferedReader in = null;
String result = "";
try {
Process pro = Runtime.getRuntime().exec(command);
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
StringTokenizer ts = new StringTokenizer(in.readLine(), "\"");
int i = 1;
while (ts.hasMoreTokens()) {
i++;
ts.nextToken();
if (i == 9) {
remCount = ts.nextToken().replace(",", "").replace("K", "").trim();
}
}
long physicalJvmMem = Long.parseLong(remCount) / 1024; // jvm物理內存占用量(MB)
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long physicalTotal = osmxb.getTotalPhysicalMemorySize() / (1024 * 1024); // 獲取服務器總物理內存(MB)
result = formatNumber(physicalJvmMem / (float) physicalTotal);
if (Float.parseFloat(result) > 1) { // 占用率最多只能是100%
result = "1";
} else if (Float.parseFloat(result) < 0) {
result = "0";
}
in.close();
pro.destroy();
} catch (Exception e) {
log.error("getThreadCountForWindows()報異常:" + e);
}
return String.valueOf((Float.parseFloat(result) * 100));
}

 
         

/**
* 獲取linux磁盤讀寫速率
*
* @return
*/
// public String getDiskAccessForLinux() {
// Process pro = null;
// Runtime r = Runtime.getRuntime();
// String command = "time dd if=/dev/zero of=/temp.log bs=1024k count=1000";
// // 讀取和寫入1G數據
// BufferedReader in = null;
// float result = 0.0f;
// try {
// pro = r.exec(new String[] { "sh", "-c", command });
// // pro.getInputStream()取不到結果內容,反而是getErrorStream()可以取到值
// in = new BufferedReader(new InputStreamReader(pro.getErrorStream()));
// in.readLine();
// in.readLine();
// if (getLocale().indexOf("zh") != -1) { // 中文語言環境
// StringTokenizer ts = new StringTokenizer(in.readLine(), ",");
// ts.nextToken();
// ts.nextToken();
// result = Float.parseFloat(ts.nextToken().split(" ")[0]);
// System.out.println("中文環境");
// } else { // 英文語言環境
// StringTokenizer ts = new StringTokenizer(in.readLine());
// ts.nextToken();
// ts.nextToken();
// ts.nextToken();
// ts.nextToken();
// ts.nextToken();
// ts.nextToken();
// ts.nextToken();
// result = Float.parseFloat(ts.nextToken().split(" ")[0]);
// System.out.println("英文環境");
// }
// r.exec("rm -f /temp.log"); // 將生成的文件刪掉
// in.close();
// pro.destroy();
// } catch (IOException e) {
// System.out.println(e.getMessage());
// }
// return String.valueOf(result);
// }

 
         

/**
* 獲取Linux服務器的語言環境
*
* @return
*/
public String getLocale() {
Process pro = null;
Runtime r = Runtime.getRuntime();
String command = "locale";
BufferedReader in = null;
StringTokenizer ts = null;
try {
pro = r.exec(command);
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
ts = new StringTokenizer(in.readLine());
in.close();
pro.destroy();
} catch (IOException e) {
System.out.println(e.getMessage());
}
return ts.nextToken();
}

 
         

/**
* 獲取Linux環境下JVM的線程數
*
* @return
*/
public int getThreadCountForLinux() {
Process pro = null;
Runtime r = Runtime.getRuntime();
String command = "top -b -n 1 -H -p " + pid;
BufferedReader in = null;
int result = 0;
try {
pro = r.exec(new String[] { "sh", "-c", command });
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
in.readLine();
StringTokenizer ts = new StringTokenizer(in.readLine());
ts.nextToken();
result = Integer.parseInt(ts.nextToken());
in.close();
pro.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return result;

 
         

}

 
         

/**
* 獲取Windows環境下JVM的線程數
*
* @return
*/
public int getThreadCountForWindows() {
String command = "wmic process " + pid + " list brief";
int count = 0;
BufferedReader in = null;
try {
Process pro = Runtime.getRuntime().exec(command);
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
// testGetInput(in);
in.readLine();
in.readLine();
StringTokenizer ts = new StringTokenizer(in.readLine());
int i = 1;

 
         

while (ts.hasMoreTokens()) {
i++;
ts.nextToken();
if (i == 5) {
count = Integer.parseInt(ts.nextToken());
}
}
in.close();
pro.destroy();
} catch (Exception e) {
log.error("getThreadCountForWindows()報異常:" + e);
}
return count;
}

 
         

private void freeResource(InputStream is, InputStreamReader isr, BufferedReader br) {
try {
if (is != null)
is.close();
if (isr != null)
isr.close();
if (br != null)
br.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}

 
         

/**
*
* 讀取CPU信息
*
* @param proc
* @return
*
*/
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
// Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 字段出現順序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = this.substring(line, capidx, cmdidx - 1).trim();
String cmd = this.substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("javaw.exe") >= 0) {
continue;
}
// log.info("line="+line);
if (caption.equals("System Idle Process") || caption.equals("System")) {
idletime += Long.valueOf(this.substring(line, kmtidx, rocidx - 1).trim()).longValue();
idletime += Long.valueOf(this.substring(line, umtidx, wocidx - 1).trim()).longValue();
continue;
}

 
         

kneltime += Long.valueOf(this.substring(line, kmtidx, rocidx - 1).trim()).longValue();
usertime += Long.valueOf(this.substring(line, umtidx, wocidx - 1).trim()).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}

 
         

/**
* 獲取網口上下行速率
*
* @param input
* @return
*/
public String[] readInLine(BufferedReader input, String osType) {
String rxResult = "";
String txResult = "";
StringTokenizer tokenStat = null;
try {
if (osType.equals("linux")) { // 獲取linux環境下的網口上下行速率
String result[] = input.readLine().split(" ");
int j = 0, k = 0;
for (int i = 0; i < result.length; i++) {
if (result[i].indexOf("RX") != -1) {
j++;
if (j == 2) {
rxResult = result[i + 1].split(":")[1];
}
}
if (result[i].indexOf("TX") != -1) {
k++;
if (k == 2) {
txResult = result[i + 1].split(":")[1];
break;
}
}
}

 
         

} else { // 獲取windows環境下的網口上下行速率
input.readLine();
input.readLine();
input.readLine();
input.readLine();
tokenStat = new StringTokenizer(input.readLine());
tokenStat.nextToken();
rxResult = tokenStat.nextToken();
txResult = tokenStat.nextToken();
}
} catch (Exception e) {
log.error(e.getMessage());
}
String arr[] = { rxResult, txResult };
return arr;
}

 
         

/**
* 由於String.subString對漢字處理存在問題(把一個漢字視為一個字節),因此在 包含漢字的字符串時存在隱患,現調整如下:
*
* @param src
* 要截取的字符串
* @param start_idx
* 開始坐標(包括該坐標)
* @param end_idx
* 截止坐標(包括該坐標)
* @return
*/
private String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++) {
tgt += (char) b[i];
}
return tgt;
}

 
         

/**
* 格式化浮點數(float 和 double),保留兩位小數
*
* @param obj
* @return
*/
private String formatNumber(Object obj) {
String result = "";
if (obj.getClass().getSimpleName().equals("Float")) {
result = new Formatter().format("%.2f", (float) obj).toString();
} else if (obj.getClass().getSimpleName().equals("Double")) {
result = new Formatter().format("%.2f", (double) obj).toString();
}
return result;
}

 
         

/**
* 測試方法 :監測java執行相關命令后是否能獲取到結果集(注:此方法執行后會中斷程序的執行,測試完后請注釋掉)
*
* @param br
*/
public void testGetInput(BufferedReader in) {
int y = 0;
try {
while ((y = in.read()) != -1) {
System.out.print((char) y);
}
} catch (IOException e) {
e.printStackTrace();
}
return;
}

 
         

class SysInfoAcquirerTimerTask extends TimerTask {

 
         

@Override
public void run() {
try {
System.out.println("任務開始:");
long startTime = System.currentTimeMillis();
int threadCount = getThreadCount();
String cpuRate = getCPURate(); // CPU使用率
String memoryRate = getMemoryRate(); // 內存占用率
JSONObject jsonObj = JSON.parseObject(getNetworkThroughput());
String upSpeed = jsonObj.getString("txPercent");// 上行速度
String downSpeed = jsonObj.getString("rxPercent"); // 下行速度
System.out.println("JVM PID:" + pid);
System.out.println("JVM 線程數:" + threadCount);
System.out.println("內存占用率:" + memoryRate + "%");
System.out.println("CPU使用率:" + cpuRate + "%");
System.out.println("上行速度:" + upSpeed + "MB/s 下行速度:" + downSpeed + "MB/s");

//后續操作為將采集到的數據存放到數據庫,可自行設計
SysPerformInfo sysPerformInfo = new SysPerformInfo();
sysPerformInfo.setId(UuidUtil.getUuid());
sysPerformInfo.setCpuRate(Float.parseFloat(cpuRate));
sysPerformInfo.setMemoryRate(Float.parseFloat(memoryRate));
sysPerformInfo.setThreadCount(threadCount);
sysPerformInfo.setUpSpeed(Float.parseFloat(upSpeed));
sysPerformInfo.setDownSpeed(Float.parseFloat(downSpeed));
sysPerformInfoService.insertPerformInfo(sysPerformInfo); // 將采集到的數據插入數據庫
long endTime = System.currentTimeMillis();
System.out.println("任務總耗時:" + (endTime - startTime) / (1000 * 60) + "分鍾");
} catch (Exception e) {
e.printStackTrace();
log.error(e.toString());
}
}

 
         

}

 
         

@PreDestroy
public void destroy() {
log.info("do nothing in @PreDestroy method");
}
}


  

 

  
寫的好不好都只能暫時這樣了,願此博文幫助到您,3Q!

 

 
        


 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM