SNM4J 异步获取数据

package com.sdstc.maxvalue.nms.services.cheng;


import java.io.IOException;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;


/**
 * @说明 SNMP4J测试
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
public class SnmpUtilAsyn {
private Snmp snmp = null;
private Address targetAddress = null;


public void initComm() throws IOException {
// 设置Agent方的IP和端口
targetAddress = GenericAddress.parse("udp:172.16.5.2/161");
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
}


public ResponseEvent sendPDU(PDU pdu) throws IOException {
// 设置 目标
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
// 通信不成功时的重试次数 N+1次
target.setRetries(2);
// 超时时间
target.setTimeout(2 * 1000);
// SNMP 版本
target.setVersion(SnmpConstants.version2c);


final CountDownLatch latch = new CountDownLatch(1);
// 设置监听对象
ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
if (event != null && event.getResponse() != null) {
System.out.println("---------->开始异步解析<------------");
readResponse(event);
}else{
latch.countDown();
}
}
};
// 发送报文
snmp.send(pdu, target, null, listener);
boolean wait;
try {
wait = latch.await(30, TimeUnit.SECONDS);
System.out.println("latch.await =:" + wait);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


snmp.close();

return null;
}


public void getPDU() throws IOException {
// PDU 对象
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.2")));
// 操作类型
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(Integer.MAX_VALUE);
pdu.setNonRepeaters(0);

ResponseEvent revent = sendPDU(pdu);
if(null != revent){
readResponse(revent);
}
}


@SuppressWarnings("unchecked")
public void readResponse(ResponseEvent respEvnt) {
// 解析Response
System.out.println("------------>解析Response<-------------");

Vector<VariableBinding> recVBs = respEvnt.getResponse()
.getVariableBindings();
for (int i = 0; i < recVBs.size(); i++) {
VariableBinding recVB = recVBs.elementAt(i);
System.out.println(recVB.getOid() + " : "
+ recVB.getVariable().toString());
}
}


public static void main(String[] args) {
try {
SnmpUtilAsyn util = new SnmpUtilAsyn();
util.initComm();
util.getPDU();
} catch (IOException e) {
e.printStackTrace();


}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值