FLUME-1673. MonitoredCounterGroup publishes this reference to platform MBean server in constructor.
(Hari Shreedharan via Mike Percy) Project: http://git-wip-us.apache.org/repos/asf/flume/repo Commit: http://git-wip-us.apache.org/repos/asf/flume/commit/ab8dd8d5 Tree: http://git-wip-us.apache.org/repos/asf/flume/tree/ab8dd8d5 Diff: http://git-wip-us.apache.org/repos/asf/flume/diff/ab8dd8d5 Branch: refs/heads/FLUME-1502 Commit: ab8dd8d570e4181e12de490bc26937f35349ff62 Parents: e61e395 Author: Mike Percy <[email protected]> Authored: Wed Oct 31 21:04:36 2012 -0700 Committer: Mike Percy <[email protected]> Committed: Wed Oct 31 21:04:36 2012 -0700 ---------------------------------------------------------------------- .../instrumentation/MonitoredCounterGroup.java | 37 ++++++++++----- .../instrumentation/TestMonitoredCounterGroup.java | 3 + 2 files changed, 28 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flume/blob/ab8dd8d5/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java ---------------------------------------------------------------------- diff --git a/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java b/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java index 1d0c3ce..502fe9e 100644 --- a/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java +++ b/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java @@ -41,6 +41,7 @@ public abstract class MonitoredCounterGroup { private AtomicLong startTime; private AtomicLong stopTime; + private volatile boolean registered = false; protected MonitoredCounterGroup(Type type, String name, String... attrs) { @@ -59,21 +60,11 @@ public abstract class MonitoredCounterGroup { startTime = new AtomicLong(0L); stopTime = new AtomicLong(0L); - try { - ObjectName objName = new ObjectName("org.apache.flume." - + type.name().toLowerCase() + ":type=" + this.name); - - ManagementFactory.getPlatformMBeanServer().registerMBean(this, objName); - - LOG.info("Monitoried counter group for type: " + type + ", name: " + name - + ", registered successfully."); - } catch (Exception ex) { - LOG.error("Failed to register monitored counter group for type: " - + type + ", name: " + name, ex); - } } public void start() { + + register(); stopTime.set(0L); for (String counter : counterMap.keySet()) { counterMap.get(counter).set(0L); @@ -82,6 +73,28 @@ public abstract class MonitoredCounterGroup { LOG.info("Component type: " + type + ", name: " + name + " started"); } + /** + * Registers the counter. This method should be used only for testing, and + * there should be no need for any implementations to directly call this + * method. + */ + void register() { + if (!registered) { + try { + ObjectName objName = new ObjectName("org.apache.flume." + + type.name().toLowerCase() + ":type=" + this.name); + + ManagementFactory.getPlatformMBeanServer().registerMBean(this, objName); + registered = true; + LOG.info("Monitoried counter group for type: " + type + ", name: " + name + + ", registered successfully."); + } catch (Exception ex) { + LOG.error("Failed to register monitored counter group for type: " + + type + ", name: " + name, ex); + } + } + } + public void stop() { stopTime.set(System.currentTimeMillis()); LOG.info("Component type: " + type + ", name: " + name + " stopped"); http://git-wip-us.apache.org/repos/asf/flume/blob/ab8dd8d5/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java ---------------------------------------------------------------------- diff --git a/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java b/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java index 0a730e9..e417fb3 100644 --- a/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java +++ b/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java @@ -103,6 +103,7 @@ public class TestMonitoredCounterGroup { String name = getRandomName(); SinkCounter skc = new SinkCounter(name); + skc.register(); ObjectName on = new ObjectName(SINK_OBJ_NAME_PREFIX + name); assertSkCounterState(on, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L); @@ -182,6 +183,7 @@ public class TestMonitoredCounterGroup { String name = getRandomName(); ChannelCounter chc = new ChannelCounter(name); + chc.register(); ObjectName on = new ObjectName(CHANNEL_OBJ_NAME_PREFIX + name); assertChCounterState(on, 0L, 0L, 0L, 0L, 0L); @@ -238,6 +240,7 @@ public class TestMonitoredCounterGroup { String name = getRandomName(); SourceCounter srcc = new SourceCounter(name); + srcc.register(); ObjectName on = new ObjectName(SOURCE_OBJ_NAME_PREFIX + name); assertSrcCounterState(on, 0L, 0L, 0L, 0L, 0L, 0L);
