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.
This commit is contained in:
Timothy Bish 2016-04-14 16:43:55 -04:00
parent 092c56d70c
commit 19fd084a83
3 changed files with 37 additions and 6 deletions

View File

@ -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
*/

View File

@ -33,13 +33,26 @@ public interface HealthViewMBean {
* like <a href="http://jolokia.org/">jolokia</a> 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<HealthStatus> 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();
}

View File

@ -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 {