use of org.snmp4j.event.ResponseListener in project opennms by OpenNMS.
the class Snmp4JStrategy method send.
private void send(Snmp4JAgentConfig agentConfig, PDU pdu, boolean expectResponse, CompletableFuture<SnmpValue[]> future) {
Snmp session;
try {
session = agentConfig.createSnmpSession();
} catch (IOException e) {
LOG.error("send: Could not create SNMP session for agent {}", agentConfig, e);
future.completeExceptionally(new Exception("Could not create SNMP session for agent"));
return;
}
if (expectResponse) {
try {
session.listen();
} catch (IOException e) {
LOG.error("send: error setting up listener for SNMP responses", e);
future.completeExceptionally(new Exception("error setting up listener for SNMP responses"));
return;
}
}
try {
if (expectResponse) {
session.send(pdu, agentConfig.getTarget(), null, new ResponseListener() {
@Override
public void onResponse(ResponseEvent responseEvent) {
if (expectResponse) {
try {
future.complete(processResponse(agentConfig, responseEvent));
} catch (IOException e) {
future.completeExceptionally(e);
} finally {
// Close the tracker using a separate thread
// This allows the SnmpWalker to clean up properly instead
// of interrupting execution as it's executing the callback
REAPER_EXECUTOR.submit(new Runnable() {
@Override
public void run() {
closeQuietly(session);
}
});
}
}
}
});
} else {
session.send(pdu, agentConfig.getTarget());
future.complete(null);
closeQuietly(session);
}
} catch (final IOException e) {
LOG.error("send: error during SNMP operation", e);
future.completeExceptionally(e);
} catch (final RuntimeException e) {
LOG.error("send: unexpected error during SNMP operation", e);
future.completeExceptionally(e);
}
}
Aggregations