gpt4 book ai didi

java - ResponseListener 在 snmp4j 中的工作原理

转载 作者:搜寻专家 更新时间:2023-11-01 02:44:17 26 4
gpt4 key购买 nike

我编写了一个简单的程序来了解 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?

最佳答案

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

null 返回值表示它已超时完成。是的,我猜这是因为您在 ResponseListener 中 hibernate 了 5 秒钟,这会阻塞消息接收线程之一。

对于 ExecutorService,最好将处理负载从 SNMP4j 消息线程移到您自己的代码中,您可以在其中控制队列等。或者您可以使用同步的 List 和一个计数 Semaphore,它将唤醒一个线程来处理接收到的消息。

关于java - ResponseListener 在 snmp4j 中的工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26274031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com