在swt里多线程操作snmp轮循的问题
我现在的程序是想做成在一个IP范围内通过snmp4j轮循get某一个oid或者多个oid.snmp4j中target.setTimeout(5000);//超时.我现在的做法是在界面上用asyncExec去调用snmp4j方法去轮循.因为我想让轮循在后台操作,同时我也可以去处理其他界面问题.但现在的问题是:如果某个ip的oid取不到,他就会挂起;我分析了下因为snmp设置了个超时,所以他肯定要走满这5秒才能去取下一个oid或者访问下一个ip.但我在外面已经用了个asyncExec,按道理它应该在后台去处理撒.为什么现在没用呢.代码如下:
/**
* 通过线程收集信息---------------------------不对
*/
Runnable runnable=new Runnable()
{
public void run()
{
try {
final string ip="192.168.10.";
for(int i=1;i<10;i++)
{
final int j=i;
sShell.getDisplay().asyncExec(new Runnable()
{
public void run()
{
snmp(ip+j);
}
});
Thread.sleep(5000);
}
} catch (Exception eee) {
eee.printStackTrace();
}
}
};
new Thread(runnable).start();
//--------------------------------
/**
* snmp
*/
private void snmp(string ip)
{
try {
//建立一个udp地址,被管理设备的udp
Address targetAddress=GenericAddress.parse("udp:"+ip+"/161");
//建立一个snmp传输消息接口
TransportMapping transport=new DefaultUdpTransportMapping();
Snmp snmp=new Snmp(transport);
transport.listen();
//创建target
CommunityTarget target=new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(1);//重复次数
target.setTimeout(5000);//超时
target.setVersion(SnmpConstants.version2c);//版本
//创建pdu,snmp协议的数据单元
PDU pdu=new PDU();
//变量绑定
VariableBinding vb=new VariableBinding(new OID(text.getText()));
pdu.add(vb);
//设置pdu动作;
pdu.setType(PDU.GETNEXT);
//发送pdu获取信息并返回一个响应pdu
PDU responsePdu=walk(snmp, pdu, target);//获取多个oid,可以先不管这个
//分析响应pdu包
if(responsePdu!=null)
{
if(responsePdu.getErrorIndex()==responsePdu.noError&&responsePdu.getErrorStatus()==responsePdu.noError)
{
String pause=responsePdu.getVariableBindings().toString();
int gettype=responsePdu.getType();
String typeString=responsePdu.getTypeString(gettype);
String getvalue=pause.substring(pause.indexOf("=")+2,pause.indexOf(']'));
String oid=pause.substring(pause.indexOf("VBS[")+2,pause.indexOf("=")-1);
System.out.println(pause);
System.out.println(typeString);
System.out.println(getvalue);
System.out.println(oid);
}
else {
System.out.println("get error:"+responsePdu.getErrorStatusText());
}
}
else {
System.out.println("get response error");
}
snmp.close();
} catch (Exception e) {
e.printStackTrace();
}
}