将检测端口封装为一个任务
public class CheckPortsTask extends SuperSupervisory{
private static OmComponentInstanceProcess omComponentInstanceProcess;
private List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
public boolean check(){
omComponentInstanceProcess = OmComponentInstanceProcess.getInstance();
list=omComponentInstanceProcess.getComponentInstanceProcessList();
for(int i=0;i<list.size();i++){
System.out.println(i);
String[] ports={""};
try{
ports=list.get(i).get("ports").toString().trim().split(",");
}catch(Exception e){
System.out.println(list.get(i).get("server_ip")+":"+"端口为空!");
continue;
}
NetWorkMonitor netWorkMonitor = new NetWorkMonitor(list.get(i).get("server_ip").toString(), ports);
Set<String> workingports = netWorkMonitor.isPortWorking();
if(workingports.isEmpty())
if("running".equals(list.get(i).get("status"))||"abnormal_exit".equals(list.get(i).get("status")))
procced("abnormal_exit",list.get(i).get("process_id").toString());
else
procced("stopped",list.get(i).get("process_id").toString());
else
procced("running",list.get(i).get("process_id").toString());
}
return false;
}
/**
* 将新状态写到数据库
* @param status
* @param process_idd
* @return
*/
public void procced(String status,String process_id){
System.out.print(status);
Map data=new HashMap();
data.put("process_id",process_id);
data.put("status", status);
omComponentInstanceProcess.updateOmComponentInstanceProcess(data);
}
}