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
?
SNMP4j uses Thread pools internally to process messages. The ResponseListener
s's onResponse()
is called from those threads, whose thread IDs you're seeing. Unlike in synchronous mode were it is executed in your own thread.
The null
return value means it finished in timeout. And yes, I would guess it's because you're sleeping for 5 seconds in the ResponseListener
, which blocks one of the message-receiving thread.
As for the ExecutorService
, it would be a good idea to move the processing load out of SNMP4j message threads into your own code, where you can control the queue, etc. Alternatively you can use a synchronized List
and a counting Semaphore
that would wake up a thread to process the received messages.