zoukankan      html  css  js  c++  java
  • 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次。
     
  • 相关阅读:
    两个不同于LR和jmeter的性能测试工具
    一个基于集成jenkins的测试平台
    sparkR原理
    Python 出现需要使用fPIC重新编译的问题
    eclipse中使用jython
    R中逻辑运算
    R语言的一些笔记
    Distributed R
    R语言和大数据
    Mysql command line
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7218248.html
Copyright © 2011-2022 走看看