• snmp4j 异步获取节点信息


    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次。
     
  • 相关阅读:
    Git学习笔记(一)
    sql复制表结构及复制表数据
    C#连接Oracle数据库查询数据
    ExcelHelper.cs
    MongoHelper.cs
    Accesshelper.cs
    Android_布局属性大全
    [WinForm]Dundas Chart控件学习(附源码)
    [Android]Adb connection Error:远程主机强迫关闭了一个现有的连接
    html to openxml
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7218248.html
Copyright © 2020-2023  润新知