From cb6c7fb423bb7347bbe62c4ec5e2845302967b59 Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Thu, 14 Apr 2016 16:43:55 -0400 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-6252 Update for some added thread safety. Adds method healthStatus that will regenrate the status from the healthList data which is more intuitive than the getCurrentStatus which doesn't update state and requires periodic calls to healthList to capture current metrics. (cherry picked from commit 19fd084a83c990f9fb75a5f2becb48ac808a1b36) --- .../activemq/broker/jmx/HealthView.java | 25 ++++++++++++++----- .../activemq/broker/jmx/HealthViewMBean.java | 13 ++++++++++ .../broker/jmx/HealthViewMBeanTest.java | 5 ++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthView.java b/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthView.java index 2b6341ace3..47a153ed94 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthView.java +++ b/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthView.java @@ -35,8 +35,8 @@ import org.apache.activemq.usage.SystemUsage; public class HealthView implements HealthViewMBean { - ManagedRegionBroker broker; - String currentState = "Good"; + private ManagedRegionBroker broker; + private volatile String currentState = "Good"; public HealthView(ManagedRegionBroker broker) { this.broker = broker; @@ -87,6 +87,7 @@ public class HealthView implements HealthViewMBean { while (dir != null && !dir.isDirectory()) { dir = dir.getParentFile(); } + long storeSize = adapter.size(); long storeLimit = usage.getStoreUsage().getLimit(); long dirFreeSpace = dir.getUsableSpace(); @@ -166,18 +167,30 @@ public class HealthView implements HealthViewMBean { } } + StringBuilder currentState = new StringBuilder(); if (answer != null && !answer.isEmpty()) { - this.currentState = "Getting Worried {"; + currentState.append("Getting Worried {"); for (HealthStatus hs : answer) { - currentState += hs + " , "; + currentState.append(hs).append(" , "); } - currentState += " }"; + currentState.append(" }"); } else { - this.currentState = "Good"; + currentState.append("Good"); } + + this.currentState = currentState.toString(); + return answer; } + @Override + public String healthStatus() throws Exception { + // Must invoke healthList in order to update state. + healthList(); + + return getCurrentStatus(); + } + /** * @return String representation of the current Broker state */ diff --git a/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthViewMBean.java b/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthViewMBean.java index 2adf14008a..32fe95b320 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthViewMBean.java +++ b/activemq-broker/src/main/java/org/apache/activemq/broker/jmx/HealthViewMBean.java @@ -33,13 +33,26 @@ public interface HealthViewMBean { * like jolokia to access JMX. * * If in doubt, please use the {@link #getCurrentStatus()} method instead! + * + * @return a list of HealthStatus objects that describe the health of the Broker. */ @MBeanInfo("List of warnings and errors about the current health of the Broker - empty list is Good!") List healthList() throws Exception; /** + * @return a String representation of current Broker health state. + */ + @MBeanInfo("String representation of current Broker state") + String healthStatus() throws Exception; + + /** + * Warning, this method only return a value if the health or healthList method has previously + * been called. The value is not updated on its own and requires periodic calls to the health + * or healthList methods to refresh its value. + * * @return String representation of the current Broker state */ @MBeanInfo("String representation of current Broker state") String getCurrentStatus(); + } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/jmx/HealthViewMBeanTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/jmx/HealthViewMBeanTest.java index 18eb0c767b..e47ca0d4e2 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/jmx/HealthViewMBeanTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/jmx/HealthViewMBeanTest.java @@ -104,6 +104,11 @@ public class HealthViewMBeanTest extends EmbeddedBrokerTestSupport { } assertEquals(2, list.size()); + + String healthStatus = health.healthStatus(); + String currentStatus = health.getCurrentStatus(); + + assertEquals(healthStatus, currentStatus); } protected ObjectName assertRegisteredObjectName(String name) throws MalformedObjectNameException, NullPointerException {