Repository: ambari Updated Branches: refs/heads/trunk 9ac82a595 -> 413231099
AMBARI-16186. Allow UDP SNMP disptacher port to be configurable. Fix for dispatch(). (mpapirkovskyy) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/d033fa71 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d033fa71 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d033fa71 Branch: refs/heads/trunk Commit: d033fa7141013328cff7e4b9aae4a5e17fb84235 Parents: 9ac82a5 Author: Myroslav Papirkovskyi <[email protected]> Authored: Fri Apr 29 20:00:43 2016 +0300 Committer: Myroslav Papirkovskyi <[email protected]> Committed: Fri Apr 29 20:09:36 2016 +0300 ---------------------------------------------------------------------- .../dispatchers/SNMPDispatcher.java | 29 +++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/d033fa71/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java index a33b815..24681d1 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java @@ -50,6 +50,7 @@ import org.snmp4j.smi.OctetString; import org.snmp4j.smi.UdpAddress; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; +import org.snmp4j.transport.UdpTransportMapping; import org.snmp4j.util.DefaultPDUFactory; import com.google.inject.Singleton; @@ -84,9 +85,11 @@ public class SNMPDispatcher implements NotificationDispatcher { private Snmp snmp; - private Integer port; + private final Integer port; + private volatile UdpTransportMapping transportMapping; public SNMPDispatcher(Snmp snmp) { + this.port = null; this.snmp = snmp; } @@ -103,12 +106,23 @@ public class SNMPDispatcher implements NotificationDispatcher { if(port != null && port >= 0 && port <= '\uffff') { //restrict invalid ports to avoid exception on socket create this.port = port; - } - if (port != null) { - LOG.info("Setting SNMP dispatch port: " + port); - this.snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(port), true)); } else { - this.snmp = new Snmp(new DefaultUdpTransportMapping()); + this.port = null; + } + } + + private void createTransportMapping() throws IOException { + if (transportMapping == null) { + synchronized (this) { + if (transportMapping == null) { + if (port != null) { + LOG.info("Setting SNMP dispatch port: " + port); + transportMapping = new DefaultUdpTransportMapping(new UdpAddress(port), true); + } else { + transportMapping = new DefaultUdpTransportMapping(); + } + } + } } } @@ -135,7 +149,8 @@ public class SNMPDispatcher implements NotificationDispatcher { public void dispatch(Notification notification) { LOG.info("Sending SNMP trap: {}", notification.Subject); try { - snmp = new Snmp(new DefaultUdpTransportMapping()); + createTransportMapping(); + snmp = new Snmp(transportMapping); SnmpVersion snmpVersion = getSnmpVersion(notification.DispatchProperties); sendTraps(notification, snmpVersion); successCallback(notification);
