用到一个插件hyperic-sigar
1:下载hyperic-sigar后解压,把sigar-amd64-winnt.dll(64位机器,32位用sigar-x86-winnt.dll)放到你本机的jdk\bin、jdk\jre\bin、jre\bin目录下。
2:maven引入hyperic-sigar依赖,这样就可以用了。
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
3:hyperic-sigar解压后到hyperic-sigar-1.6.4\bindings\java\examples目录下这里面有很多官方提供的例子,可以直接复制到你的工程下运行测试。
我用的是websocket+java-timer做的。
public class KeepRunTime extends TimerTask {
private static Sigar sigar = new Sigar();
@Override
public void run() {
RunTime rt = new RunTime();
rt.setMem(mem());
rt.setCpus(cpu());
DataPushService.pushData(JSONObject.toJSONString(new WebMessage(rt, WebMessage.RUNTIME)));
}
// CPU使用率
public static TreeSet<Cpu> cpu() {
try {
TreeSet<Cpu> cpus = new TreeSet<Cpu>();
CpuPerc\[\] list = sigar.getCpuPercList();
for (int i=1; i<list.length+1; i++) {
Cpu cpu = new Cpu();
cpu.setNo(i);
cpu.setCpuName("CPU-"+i);
cpu.setCpuCombined(CpuPerc.format(list\[i-1\].getCombined()));
cpus.add(cpu);
}
return cpus;
} catch (SigarException e) {
System.err.println("获取CPU使用率异常");
e.printStackTrace();
return null;
}
}
// MEM使用率
public static String mem() {
try {
Mem mem = sigar.getMem();
// 内存总量
Long memTotal = mem.getTotal() / 1024L;
// 当前内存使用量
Long memUsed = mem.getUsed() / 1024L;
// 使用率
Double usePercent = ((memUsed\*1.0)/memTotal)\*100;
// 四舍五入
return String.format("%.2f", usePercent);
} catch (SigarException e) {
System.err.println("获取MEM使用率异常");
e.printStackTrace();
return null;
}
}
}
public class RunTime {
// 内存使用率
private String mem;
// CPU使用率
private TreeSet<Cpu> cpus;
public String getMem() {
return mem;
}
public void setMem(String mem) {
this.mem = mem;
}
public TreeSet<Cpu> getCpus() {
return cpus;
}
public void setCpus(TreeSet<Cpu> cpus) {
this.cpus = cpus;
}
@Override
public String toString() {
return "RunTime \[mem=" + mem + ", cpus=" + cpus + "\]";
}
}
public class Cpu implements Comparable
private int no;
private String cpuName;
private String cpuCombined;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getCpuName() {
return cpuName;
}
public void setCpuName(String cpuName) {
this.cpuName = cpuName;
}
public String getCpuCombined() {
return cpuCombined;
}
public void setCpuCombined(String cpuCombined) {
this.cpuCombined = cpuCombined;
}
// 正序
@Override
public int compareTo(Cpu o) {
return (this.no - o.no);
}
@Override
public String toString() {
return "Cpu \[no=" + no + ", cpuName=" + cpuName + ", cpuCombined="
+ cpuCombined + "\]";
}
}
js:
var runtime = function(data) {
$("#mem").html(data.obj.mem + '%');
var cpus = data.obj.cpus;
var html = "<p>";
for (var i = 1; i < cpus.length + 1; i++) {
html += "<b>" + cpus\[i-1\].cpuName + ":</b>" +
"<span class='runtime' id='span-cpu'>"+ cpus\[i-1\].cpuCombined + "</span>";
if (i % 2 == 0) {
html += "</p><p>";
}
}
$("#cpu").html(html);
};
效果:
这里面用了websocket,没提出来具体的代码。
下次我写一个基于netty、websocket.io、springmvc、mybatis、h2database、httpclient、fastjson、https的整合,我现在就在写这样一个项目,等写好了做一个简化版的发上来。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章