blob: 812697860bde472cadc02239b8b58c80fd24efdb (
plain)
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
|
package com.nis.nmsclient.thread;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
public class WritePidThread implements Runnable{
@Override
public void run() {
String SYSTEM_PATH = System.getProperty("user.dir");
// Thread.currentThread().setName("写PID线程");
Thread.currentThread().setName("Write The PID Thread");
Logger logger = Logger.getLogger(WritePidThread.class);
/* 获取程序运行PID */
String path = Contants.localAgentPidFile;//2015-11-25:之前一直写的是"NMSClientPid.temp",不对配置的是agentPid.temp
String name = ManagementFactory.getRuntimeMXBean().getName();
logger.info("当前程序PID:>"+(name.split("@")[0]));
/* 判断系统类型是否写文件 */
String os = System.getProperty("os.name");
if(os!=null && !os.toLowerCase().startsWith("windows")){
logger.info("非Windows系统 结束执行");
return ;
}
/* 获取输出文件并检查文件路径是否存在 */
File file = new File(path);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
/* 将PID写入文件 */
FileWriter writer = null;
try {
writer = new FileWriter(file);
writer.write(name.split("@")[0]);
writer.flush();
logger.info("写PID完成");
} catch (IOException e) {
logger.error("Write PID failure", e);
}finally{
try {
if(writer!=null)
writer.close();
writer = null;
} catch (IOException e) {
logger.error("", e);
}
logger.info("线程关闭");
}
}
public static void main(String [] args) {
new Thread(new WritePidThread()).start();
}
public static void pl (Object obj) {
System.out.println(obj==null?null:obj.toString());
}
}
|