ResponseListener如何在snmp4j中工作

时间:2014-10-09 08:44:44

标签: java multithreading asynchronous snmp snmp4j

我编写了一个简单的程序来了解snmp4j异步请求/响应的工作原理:

TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();

Address targetAddress = GenericAddress.parse("udp:192.168.1.116/161");
CommunityTarget target = new CommunityTarget();
target.setAddress(targetAddress);
target.setCommunity(new OctetString("nid"));
target.setRetries(0);
target.setTimeout(30000);
target.setVersion(SnmpConstants.version2c);

ResponseListener listener = new ResponseListener() {
    @Override
    public void onResponse(ResponseEvent event) {
        ((Snmp) event.getSource()).cancel(event.getRequest(), this);
        System.out.println("Received response PDU is: " + event.getResponse());
        System.out.println("response listener thread id: " + Thread.currentThread().getId());
        System.out.println("**********************************");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
};

System.out.println("main thread id: " + Thread.currentThread().getId());
for (int i = 0; i < 10; i++) {
    PDU pdu1 = new PDU();
    pdu1.add(new VariableBinding(new OID("1.3.6.1.4.1.22420.2.5.3.1.1.1")));
    pdu1.add(new VariableBinding(new OID("1.3.6.1.4.1.22420.2.5.3.1.1.2")));
    pdu1.setType(PDU.GET);
    snmp.send(pdu1, target, null, listener);
}

Thread.sleep(100000); 

示例输出如下:

main thread id: 1
Received response PDU is: RESPONSE[requestID=205585942, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************
Received response PDU is: RESPONSE[requestID=205585943, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************
Received response PDU is: RESPONSE[requestID=205585944, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************
Received response PDU is: RESPONSE[requestID=205585945, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************
Received response PDU is: RESPONSE[requestID=205585946, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************
Received response PDU is: RESPONSE[requestID=205585947, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************
Received response PDU is: null
response listener thread id: 9
**********************************
Received response PDU is: RESPONSE[requestID=205585948, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************
Received response PDU is: null
response listener thread id: 9
**********************************
Received response PDU is: RESPONSE[requestID=205585950, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.4.1.22420.2.5.3.1.1.1 = 1; 1.3.6.1.4.1.22420.2.5.3.1.1.2 = 2]]
response listener thread id: 8
**********************************

输出显示多个线程ID。这意味着并行处理响应数据包。我对吗?线程ID为9的两个响应返回null。有什么理由吗?在使用同步模型时,我无法得到它。这是因为Thread.sleepResponseListener内。因此,如果我无法在ResponseListener内阻止工作,并且我想并行处理响应,我需要添加ExecuterService

1 个答案:

答案 0 :(得分:4)

SNMP4j在内部使用线程池来处理消息。 ResponseListener s onResponse()是从那些线程中调用的,您的线程ID是您正在查看的。与同步模式不同,它是在您自己的线程中执行的。

null返回值表示它在超时时间内完成。是的,我猜它是因为你在ResponseListener中睡了5秒钟,这阻止了一个消息接收线程。

对于ExecutorService,将处理负载从SNMP4j消息线程移到您自己的代码中是个好主意,您可以在其中控制队列等。或者您可以使用synchronized {{ 1}}和一个计数List,它会唤醒一个线程来处理收到的消息。