I have written a simple program to understand how snmp4j asynchronous request/response works:
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);
sample output is like this:
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
**********************************
Output shows multiple thread ids. This means response packets are processed in parallel. Am I right? Two responses with thread id 9 return null
. Is there any reason for it? I don't get it when working with synchronous model. Is this because Thread.sleep
inside ResponseListener
. So If I can't do blocking work inside ResponseListener
and I want to process responses in parallel I need to add ExecuterService
?