Creating a TransportMapping and Snmp instance for each request (or even a few requests) like shown below, generates a big overhead and IO latency through creating and closing UDP (or TCP) ports on the operating system. This overhead will let the approach below causing timeouts and showing very low performance.

Bad example:

public ResponseEvent doSingleRequest(PDU pdu) {
  CommunityTarget target = new CommunityTarget();
  Address addr = new UdpAddress("127.0.0.1/161");
  target.setCommunity(new OctetString("public"));
  target.setAddress(addr);
  target.setVersion(SnmpConstants.version2c);

  TransportMapping transport = new DefaultUdpTransportMapping();
  Snmp snmp = new Snmp(transport);
  snmp.listen();
  ResponseEvent response = snmp.send(pdu, target);
  snmp.close();
  return responseEvent;
}

Better example:

class SnmpRequestProcessor {

...

  public SnmpRequestProcessor() {
    ..
  }

  public void setup() {
    CommunityTarget target = new CommunityTarget();
    Address addr = new UdpAddress("127.0.0.1/161");
    target.setCommunity(new OctetString("public"));
    target.setAddress(addr);
    target.setVersion(SnmpConstants.version2c);

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

  public ResponseEvent doSingleRequest(PDU pdu) {
    ResponseEvent response = snmp.send(pdu, target);
    return responseEvent;
  }

  public void shutDown() {
    snmp.close();
  }

The above bad example

  • No labels