Main Function
Monitoring node application configuration file nodes.txt, if the file has changed (the monitoring file was last modified time), load change of target monitoring nodes.
Application http connection periodically requests, receives responses, a complete heartbeat detection, and the number of failures to support transmission intervals, and the interface configuration information and respond to error handling processing.
Environmental dependent
oracle jdk 1.8
http-client-4.5.2
http-core-4.4.4
nodes.txt format
Each row of the form
127.0.0.1:80,81,82
. 1
main classes described
FileChangeWatcher inherited Thread, from time to time to check whether Each file is modified. And generate action listener.doFileChange ();
@Override
public void RUN () {
Long Last the getLastModified = ();
the while (to true) {
Long the getLastModified tmp = ();
IF (! = Last tmp) {
! IF (listener = null ) {
listener.doFileChange ();
}
Last = tmp;
}
the try {
TimeUnit.MILLISECONDS.sleep (timeMs);
} the catch (InterruptedException E) {
logger.error ( "interrupted", E);
}
}
}
HostStore 接口,提供host信息列表
public interface HostStore {
Map<String, Set<Integer>> getHostAsMap();
List<String> getHostAsList();
}
ChangableHostStore
public interface ChangableHostStore extends HostStore{
interface HostChangeListener{
void doHostChange();
}
void setHostChangeListener(HostChangeListener listener);
}
FileHostStore 实现ChangableHostStore接口,提供解析nodes.txt文件的功能
public Map<String, Set<Integer>> loadHostFromFile(File f){
Map<String, Set<Integer>> result = new HashMap<>();
try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)))){
String tmp;
while((tmp = br.readLine()) != null){
String[] split = tmp.split(":");
String[] split1 = split[1].split(",");
Set<Integer> collect = Stream.of(split1)
.map(Integer::decode)
.collect(Collectors.toSet());
if(result.containsKey(split[0])){
result.get(split[0]).addAll(collect);
}else{
result.put(split[0], collect);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
The catch} (IOException E) {
e.printStackTrace ();
}
return Result;
}
AppMonitor main heartbeat inlet
configuration parameters
public AppMonitor (File File, failRetryTime int, int intervalSendMs) {
this.file = File; monitoring file // nodes.txt
this.failRetryTime = failRetryTime; // connection failure retry
this.intervalSendMs = intervalSendMs; // heartbeat packet transmission interval
isHostUpdated = new AtomicBoolean (false); // if node list update flag
removeIfFail = false; // whether the node is no longer a failure to monitor
client = newClient (); // create HttpClient
hostStore = new new FileHostStore (file, the this); // responsible for monitoring file changes
eventThreadPool = Executors.newFixedThreadPool (EVENTTHREADNUM); // event processing thread pool
loadHostList (); // load node
}
highlight start monitoring tasks
public void start(){
String host;
while(true) {
while (hostList.size() == 0);
/*
同步锁定hostList,还原isHostUpdated的状态
*/
synchronized (hostList){
if (hostList.size() == 0)
continue;
host = hostList.remove();
isHostUpdated.set(false);
}
int restTry = failRetryTime + 1;//剩余的连接次数
do {
HttpGet httpGet = new HttpGet("http://" + host);
try (CloseableHttpResponse response = client.execute(httpGet)){
if (response.getStatusLine (). getStatusCode ( ) == 200) {// Here only view corresponding normal heartbeat detection success!
IF (needDealRtnMsg) {
String MSG = readMsgFromResponse (Response);
submitRtnDeal (MSG); // returns the processing tank submitted to the information processing event
}
} the else {
Continue;
}
} the catch (IOException E) {
logger.error ( "IOException") ;
the Continue;
}
BREAK; // if this is executed, represents the heartbeat detection is successful, then restTry must be greater than 0, the following failure of treatment with a reverse proposition No!
the while} (- restTry> 0);
IF (restTry <= 0) {
submitFailDeal (host); // Error handling submitted to the event processing tank
}
the try {
TimeUnit.MILLISECONDS.sleep (intervalSendMs);
} the catch (InterruptedException E) {
logger.error ( "Wrong Occur in Sleeping", E);
}
IF ( && restTry removeIfFail <= 0) {
Continue;
}
/ *
lockstep hostlist, locking hostlist been changed twice, then lose the Host
* /
the synchronized (hostlist) {
IF (isHostUpdated.get ()) {
Continue;
}
hostlist .add (host);
}
}
}
Resource region (7 points)
Most code has new syntax jdk1.8 to see are not used? You hit me ah
resource is too expensive? No time to reconstruct, written in Java, shy → _ →
the Java code