ACommonThread.java
2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.sitech.base;
import java.io.File;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.apache.log4j.Logger;
import com.sitech.ismp.messageObject.HeartbeatObject;
import com.sitech.util.Formater;
import com.sitech.util.mq.MQConstants;
import com.sitech.util.mq.TunnelFactory;
public abstract class ACommonThread extends Thread {
private Logger heartbeat = Logger.getLogger("HEARTBEAT");
static final int UP = 1;
static final int DOWN = 2;
static final int UNKNOWN = 3;
private String moduleId;
private String processName;
public ACommonThread(String moduleId, String processName) {
this.moduleId = moduleId;
this.processName = processName;
}
protected boolean isRun = true;
private Date startTime;
@Override
public void run() {
startTime = new Date();
process();
}
protected void doHeartBeat() {
heartbeat.info("Do heartbeat.");
HeartbeatObject heartbeat = new HeartbeatObject();
heartbeat.setModuleId(this.moduleId);
heartbeat.setProcessName(this.processName);
heartbeat.setProcessStartTime(startTime);
heartbeat.setReportTime(new Date());
heartbeat.setProcessStatus(UP);
heartbeat.setProcessCpu(getCpuUsage());
heartbeat.setProcessMem(getTotalMemory());
heartbeat.setProcessErrInfo("-");
heartbeat.setVersion(getVersion());
TunnelFactory.getTunnel(MQConstants.Q_ROPORT_FROM_AGENT).writeData(heartbeat);
}
/**
* 1分钟内平均CPU利用率
* @return
*/
private String getCpuUsage() {
return "-";
}
/**
* @return JVM内存大小,单位k
*/
private String getTotalMemory() {
long totalMemory = Runtime.getRuntime().totalMemory();
return Formater.formatDecimalKpivalue(String.valueOf(totalMemory / 1024L));
}
protected abstract void process();
protected String getVersion() {
String version = "-";
try{
File file = new File(AgentProperties.AGENT_HOME + "/libs/agent.jar");
JarFile jarFile = new JarFile(file);
Manifest manifest = jarFile.getManifest();
Attributes att = manifest.getMainAttributes();
String specVersion = att.getValue("Specification-Version");
String buildVersion = att.getValue("Implementation-Version");
version = "V" + specVersion + "_B" + buildVersion;
} catch (Exception e) {
heartbeat.error("Exception while getAgentVersion", e);
}
return version;
}
public boolean isRun() {
return isRun;
}
public void setRun(boolean isRun) {
this.isRun = isRun;
}
public String getModuleId() {
return moduleId;
}
public void setModuleId(String moduleId) {
this.moduleId = moduleId;
}
}