在swt里多线程操作snmp轮循的问题

yours1213 2008-04-23 09:13:05
我现在的程序是想做成在一个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();

}

}
...全文
211 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yours1213 2008-04-24
  • 打赏
  • 举报
回复
谢谢了哈.结帖
zhmt 2008-04-23
  • 打赏
  • 举报
回复
for (int i = Integer.parseInt(startip[3]); i <= Integer
.parseInt(endip[3]); i++) {
final int index = i;
final String ip = strStartip + index;
final Runnable refresh = new Runnable() {
public void run() {
// 轮循
try {
oltconInterface.getIP(ip);
String string = oltconInterface
.getMoreOid("1.3.6.1.2.1.1.5");// 1.3.6.1.4.1.17409.2.4.2.1.1
System.out.println(string);

// boolean
// b=oltconInterface.setOid("1.3.6.1.2.1.1.5.0",
// "bbbbb");
// if (b==true) {
// System.out.println("bbbbb");
// }

} catch (Exception e) {
// TODO: handle exception
}
}
};
new Thread(refresh).start();
}

你试试这样挂起不
yours1213 2008-04-23
  • 打赏
  • 举报
回复
有人知道吗?急死人了
yours1213 2008-04-23
  • 打赏
  • 举报
回复
好象不是try的问题.因为我把刚才你说的那个问题改在for里面后一样挂起
yours1213 2008-04-23
  • 打赏
  • 举报
回复
for (int i = Integer.parseInt(startip[3]); i <=Integer.parseInt(endip[3]); i++)
{
final int index=i;
final String ip=strStartip+index;
final Runnable refresh=new Runnable()
{
public void run()
{
Runnable runnable=new Runnable()
{
public void run()
{
try {
sShell.getDisplay().asyncExec(new Runnable()
{
public void run()
{
//轮循
try {
oltconInterface.getIP(ip);
String string=oltconInterface.getMoreOid("1.3.6.1.2.1.1.5");//1.3.6.1.4.1.17409.2.4.2.1.1
System.out.println(string);

// boolean b=oltconInterface.setOid("1.3.6.1.2.1.1.5.0", "bbbbb");
// if (b==true) {
// System.out.println("bbbbb");
// }

} catch (Exception e) {
// TODO: handle exception
}

}
});
Thread.sleep(5000);
} catch (Exception eee) {
eee.printStackTrace();
}
}
};
new Thread(runnable).start();
}
};
BusyIndicator.showWhile(sShell.getDisplay(), refresh);
}
这是我另一个,try在for里面,一样要挂起
zhmt 2008-04-23
  • 打赏
  • 举报
回复
根据你现在的写法,当跑出timeoutexception的时候就退出for循环了,当然不会再继续跑下去了。
把try catch 移到for循环内就ok。
zhmt 2008-04-23
  • 打赏
  • 举报
回复
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();
}
}
};
你的这段代码写的有问题
zhmt 2008-04-23
  • 打赏
  • 举报
回复
但是 如果你能处理得当,用thread也是一样的,
它们不建议用thread,是因为很多人都不能很好的处理并发问题。

你再仔细看看asyncExec或者其它相关的类的用法。下面这篇文章应该对你作用:
http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461e1c3ab6e62c290d579394263c45f8545ce8f2336f34003db699d58b41dfb0922e2e9f2743235dc40b069644ef9d49648466875a98fc58ace6a165d9f983&p=882a964f9c815bdd18be9b7f0d0881&user=baidu
zhmt 2008-04-23
  • 打赏
  • 举报
回复
其实 我对ui编程不熟悉

其次,你后来的程序我看问题不大,所以就怀疑是你asyncExec的问题。

具体是什么原因,你可以查查asyncExec的用法。

其实也没什么不能用thread的,主要是用了以后,与ui交互的话不容易处理,牵涉到并发的问题。
yours1213 2008-04-23
  • 打赏
  • 举报
回复
呼唤zhmt~~~
yours1213 2008-04-23
  • 打赏
  • 举报
回复
是不是应该这样理解:asyncExec是针对ui的,如果仅仅是做后台处理就只使用java里的thread就ok了.是不是这样的呢?如果是这样的话,那又出来一个问题:如果我在loop的同时就要更新ui某个控件比如tree.是不是要在里面再添个asyncExec里去刷新tree呢
yours1213 2008-04-23
  • 打赏
  • 举报
回复
可以了.不过swt里用线程说是要用asyncExec,而不能直接用java的thread吗?为什么这里就必要要用java里的线程呢?
请zhmt或者其他高手回答下呢

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧