首页>代码>java本机监控程序>/Appmonitor_LiuShang_098/src/com/ambimmort/commons/appmonitor/AppMonitorClient.java
package com.ambimmort.commons.appmonitor; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Properties; import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; /** * 应用程序监视器 * AppMonitorClient是一个单例类 * @author Administrator * */ public class AppMonitorClient { private static AppMonitorClient instance= null; //心跳 private int heatbeat = 0; private int zero = 0; private String appMonitorServer = null; private int appMonitorServerPort; private Properties config = new Properties(); private Timer timer = new Timer(); private AppMonitorClient(){ init(); } public static AppMonitorClient getInstance(){ if(instance == null){ instance = new AppMonitorClient(); } return instance; } /** * 初始化方法,读入运行目录下的appmonitor.conf配置文件 */ public void init(){ try { config.load(new FileInputStream("./appmonitor.conf")); heatbeat = Integer.parseInt(config.getProperty("appmonitor.client.heatbeat")); String tmp = config.getProperty("appmonitor.server"); zero = Integer.parseInt(config.getProperty("appmonitor.client.zero")); String[] ds = tmp.split(":"); appMonitorServer = ds[0]; appMonitorServerPort = Integer.parseInt(ds[1]); System.out.println("heart beat : "+heatbeat); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 启动一个定时任务.定期的向AppMonitor服务器发送心跳。 */ public void start(){ timer.schedule(new TimerTask() { @Override public void run() { /** * 创建一个socket向服务器发送 信息,发送的信息如下: * appmonitor.client.appname */ try { Socket socket = new Socket(appMonitorServer, appMonitorServerPort); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); HeartBeatBean hbb = new HeartBeatBean(); hbb.setName(config.getProperty("appmonitor.client.appname")); hbb.setScriptBean(config.getProperty("appmonitor.client.startScript")); hbb.setTtl(Long.parseLong(config.getProperty("appmonitor.client.ttl"))); oos.writeObject(hbb); oos.close(); } catch (UnknownHostException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } }, zero, heatbeat); } public static void main(String[] args) { AppMonitorClient.getInstance().start(); } }

zzzyyy1 LV2
2024年2月26日
阳光正好 LV2
2021年10月29日
rruizy LV3
2021年6月30日
linjuny LV3
2021年6月28日
徐庚新 LV1
2020年11月4日
wangmeicong LV12
2020年7月15日
huzh035 LV3
2020年3月17日
2016021 LV2
2019年6月10日
1324488732 LV27
2018年11月19日
最代码官方 LV168
2018年9月24日

yulinfeng LV15
2024年7月15日
zzzyyy1 LV2
2024年2月26日
escape1023
2024年2月13日
暂无贡献等级
denglu123321 LV4
2023年12月27日
xp95323 LV15
2023年11月27日
Molly123 LV8
2023年10月21日
edpwyg LV14
2023年10月21日
三几个 LV11
2023年8月8日
matintalorr LV10
2023年5月9日
Q256688
2023年5月1日
暂无贡献等级