snmp4j 異步獲取節點信息
阿新 • • 發佈:2017-07-21
ble 出現異常 使用 ptr address void 意思 int transport
1. 主要代碼如下:
public class ResponseListenerTest { public static void main(String[] args) throws IOException, InterruptedException { Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.listen(); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(GenericAddress.parse("udp:192.168.100.61/161")); target.setRetries(1); target.setTimeout(2000); target.setVersion(SnmpConstants.version1); PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0"))); pdu.setType(PDU.GET); ResponseListener listener= new ResponseListener() { @Override public void onResponse(ResponseEvent event) { PDU resp = event.getResponse(); VariableBinding vb = resp.get(0); System.out.println(vb.getOid().toString() + "^^^^" + vb.getVariable()); } }; CountDownLatch latch= new CountDownLatch(1); snmp.get(pdu, target, null, listener); latch.await(2, TimeUnit.SECONDS); } }
2. 運行結果如下:
3. 在上面的例子中,也可以使用線程的方式處理:
但是需要註意的是:sleep的時間要小於 setTimeout 的時間,否則會出現異常。
4. 其中:
target.setTimeout(2000); // 意思為:當發送請求後 2秒鐘沒有返回響應信息,表示已經超時了。
target.setRetries(1); // 意思為:當上面的邏輯超時後,再次發送請求的次數,為1次。
snmp4j 異步獲取節點信息