INT-1452: fix names and counts attributes

Add global counts to mbean exporter
This commit is contained in:
Dave Syer
2010-09-21 09:31:29 +02:00
parent 3cfbbd9f79
commit 8cf6e474d2
6 changed files with 54 additions and 18 deletions

View File

@@ -125,7 +125,7 @@ abstract class AbstractMessageHandlerFactoryBean implements FactoryBean<MessageH
if (this.targetObject != null) {
Assert.state(this.expression == null,
"The 'targetObject' and 'expression' properties are mutually exclusive.");
if (this.targetObject instanceof MessageProcessor) {
if (this.targetObject instanceof MessageProcessor<?>) {
this.handler = this.createMessageProcessingHandler((MessageProcessor<?>) this.targetObject);
}
else {

View File

@@ -13,7 +13,6 @@
package org.springframework.integration.monitor;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -89,10 +88,6 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
public static final String DEFAULT_DOMAIN = "spring.application";
private Set<String> channelKeys = new HashSet<String>();
private Set<String> handlerKeys = new HashSet<String>();
private final AnnotationJmxAttributeSource attributeSource = new AnnotationJmxAttributeSource();
private ListableBeanFactory beanFactory;
@@ -165,11 +160,13 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MessageHandler) {
SimpleMessageHandlerMonitor monitor = null;
if (bean instanceof MessageProducer){ // we need to maintain semantics of the handler also being a producer see INT-1431
if (bean instanceof MessageProducer) { // we need to maintain semantics of the handler also being a producer
// see INT-1431
monitor = new SimpleMessageProducingHandlerMonitor((MessageHandler) bean);
} else {
}
else {
monitor = new SimpleMessageHandlerMonitor((MessageHandler) bean);
}
}
handlers.add(monitor);
return monitor;
}
@@ -285,23 +282,43 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
}
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "MessageChannel Channel Count")
public double getChannelCount() {
return channelKeys.size();
public int getChannelCount() {
return channelsByName.size();
}
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "MessageHandler Handler Count")
public double getHandlerCount() {
return handlerKeys.size();
public int getHandlerCount() {
return handlersByName.size();
}
@ManagedAttribute
public Collection<String> getHandlerNames() {
return handlersByName.keySet();
public String[] getHandlerNames() {
return handlersByName.keySet().toArray(new String[0]);
}
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Active Handler Count")
public int getActiveHandlerCount() {
int count = 0;
for (MessageHandlerMonitor monitor : handlers) {
count += monitor.getActiveCount();
}
return count;
}
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Queued Message Count")
public int getQueuedMessageCount() {
int count = 0;
for (MessageChannelMonitor monitor : channels) {
if (monitor instanceof QueueChannelMonitor) {
count += ((QueueChannelMonitor) monitor).getQueueSize();
}
}
return count;
}
@ManagedAttribute
public Collection<String> getChannelNames() {
return channelsByName.keySet();
public String[] getChannelNames() {
return channelsByName.keySet().toArray(new String[0]);
}
@ManagedOperation(description = "Get the JMX object name (as a String) for the specified Spring bean name")

View File

@@ -92,4 +92,8 @@ public class LifecycleMessageHandlerMonitor implements MessageHandlerMonitor, Li
return delegate.getSource();
}
public int getActiveCount() {
return delegate.getActiveCount();
}
}

View File

@@ -58,6 +58,9 @@ public interface MessageHandlerMonitor {
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Handler Standard Deviation Duration")
double getStandardDeviationDuration();
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Handler Active Status")
int getActiveCount();
/**
* @return summary statistics about the handler duration (milliseconds)
*/

View File

@@ -44,6 +44,8 @@ public class SimpleMessageHandlerMonitor implements MessageHandler, MessageHandl
private final MessageHandler handler;
private final AtomicInteger activeCount = new AtomicInteger();
private final AtomicInteger handleCount = new AtomicInteger();
private final AtomicInteger errorCount = new AtomicInteger();
@@ -94,6 +96,7 @@ public class SimpleMessageHandlerMonitor implements MessageHandler, MessageHandl
try {
timer.start();
handleCount.incrementAndGet();
activeCount.incrementAndGet();
handler.handleMessage(message);
@@ -105,6 +108,8 @@ public class SimpleMessageHandlerMonitor implements MessageHandler, MessageHandl
} catch (Error e) {
errorCount.incrementAndGet();
throw e;
} finally {
activeCount.decrementAndGet();
}
}
@@ -141,6 +146,11 @@ public class SimpleMessageHandlerMonitor implements MessageHandler, MessageHandl
return duration.getStandardDeviation();
}
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Handler Active Count")
public int getActiveCount() {
return activeCount.get();
}
public Statistics getDuration() {
return duration.getStatistics();
}

View File

@@ -15,6 +15,8 @@ package org.springframework.integration.monitor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.Aspect;
@@ -64,7 +66,7 @@ public class HandlerMonitoringIntegrationTests {
ClassPathXmlApplicationContext context = createContext("anonymous-handler.xml", "anonymous");
try {
assertTrue(messageHandlersMonitor.getHandlerNames().contains("errorLogger"));
assertTrue(Arrays.asList(messageHandlersMonitor.getHandlerNames()).contains("errorLogger"));
}
finally {
context.close();