📄 collectorthread.java
字号:
/**
*
* <p>Title:CollectorThread </p>
*
* <p>Description:线程 </p>
*
* <p>作用:接受生产者发送的XML格式的数据文件,并进行相应的处理</p>
*
* <p>使用方法:PortalCollectorServer线程每接收到一个数据文件,启动一个CollectorThead进行处理</p>
*
* <p>Copyright: Copyright (c) 2006-03-23</p>
*
* <p>Company:HIT </p>
*
* @author Yao Yuanzhe
*/
package portal.collector;
import java.io.*;
import java.net.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class CollectorThread implements Runnable {
private Socket socket;
private InetAddress add ; //发送方的ip地址
//private int remotePort ;
public CollectorThread(Socket socket){
this.socket = socket;
add = socket.getInetAddress();//通过socket得到发送方的ip地址
}
public void run() {
try {
socket.setSoTimeout(60000); //设置超时时间
/*得到发送方发送来的数据文件*/
InputStream is = socket.getInputStream() ;
/*使用Jdom对此数据文件进行解析*/
SAXBuilder sax = new SAXBuilder();
Document dom = sax.build(is);
Element rootelem=dom.getRootElement(); //得到请求的根标识符
List rootlist=rootelem.getChildren(); //得到请求的一系列第二级标识符
if("group_metadata".equals(rootelem.getName())) { /*如果是groupxml*/
System.out.println("-----get group data at port:8086 from host:" + add.toString() + "-------- " );
/*遍历命令的第二级标识符,即所有组信息和普通节点信息*/
for(int i=0;i<rootlist.size();i++){
/*对每一个第二级标识符进行处理,得到每一个的查询结果;如果第二级标志符无效,内容为空*/
Element currentelem=(Element)rootlist.get(i);
String stringname = currentelem.getName();
if (stringname.equals("Global_GroupId"))System.out.println(stringname + ":"+ rootelem.getChildText("Global_GroupId"));
else if (stringname.equals("MaxNodes_In_Group"))System.out.println(" MaxNodes_In_Group are :"+ currentelem.getText());
else if (stringname.equals("CurrentNodes_In_Group"))System.out.println(" CurrentNodes_In_Group are :"+ currentelem.getText());
else if (stringname.equals("GlobalHostId_Of_HeadNode"))System.out.println(" GlobalHostId_Of_HeadNode:"+ currentelem.getText());
else if (stringname.equals("GlobalHostId_Of_OtherNodes"))System.out.println(" GlobalHostId_Of_OtherNodes :"+ currentelem.getText());
else if (stringname.equals("Total_Cpus"))System.out.println(" Total_Cpus are :"+ currentelem.getText());
else if (stringname.equals("Total_Diskspace"))System.out.println(" Total_Diskspace :"+ currentelem.getText());
else if (stringname.equals("host")){
System.out.println(" host :");
List Children = currentelem.getChildren();
for(int j=0; j<Children.size(); j++) {
Element elmtChild = (Element) Children.get(j);
String string = elmtChild.getName();
if (string.equals("Os")||string.equals("Cpu")||string.equals("Load")||string.equals("Java")||string.equals("Disk")||string.equals("Memory")||string.equals("Swap")||string.equals("Netinfo")){
System.out.println(" " + string + " :");
List Child = elmtChild.getChildren();
for(int k=0; k<Child.size(); k++) {
Element elmtCh = (Element) Child.get(k);
String str = elmtCh.getName();
System.out.println( " "+ str +" : "+ elmtCh.getText());
}
}
else System.out.println( " "+ string +" : "+ elmtChild.getText());
}
}else if (stringname.equals("NetworkInfo")){
System.out.println("NetworkInfo :");
List Children = currentelem.getChildren();
for(int j=0; j<Children.size(); j++) {
Element elmtChild = (Element) Children.get(j);
String string = elmtChild.getName();
System.out.println( " "+ string +" : "+ elmtChild.getText());
}
}
else System.out.println( stringname +" : "+ currentelem.getText());
/*switch (string)
{
case "Global_GroupId":
System.out.println(" Global_GroupId are:"+ currentelem.getChildText("Global_GroupId"));
break;
case "MaxNodes_In_Group":
System.out.println(" MaxNodes_In_Group are:"+ currentelem.getChildText("MaxNodes_In_Group"));
break;
case "CurrentNodes_In_Group":
System.out.println(" CurrentNodes_In_Group are:"+ currentelem.getChildText("CurrentNodes_In_Group"));
break;
case "GlobalHostId_Of_HeadNode":
System.out.println(" CurrentNodes_In_Group are:"+ currentelem.getChildText("GlobalHostId_Of_HeadNode"));
break;
case "GlobalHostId_Of_OtherNodes":
System.out.println(" GlobalHostId_Of_OtherNodes are:"+ currentelem.getChildText("GlobalHostId_Of_OtherNodes"));
break;
case "Total_Cpus":
System.out.println(" Total_Cpus:"+ currentelem.getChildText("Total_Cpus"));
break;
case "Total_Diskspace":
System.out.println(" Total_Diskspace:"+ currentelem.getChildText("Total_Diskspace"));
break;
default:
// bad input
System.out.println( currentelem.getChildText()+" is not a valid element " );
break;
}*/
}
}
else System.out.println(" RootElement is invalid! ");
} catch (Exception e) {
System.out.println(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -