diff --git a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java index b9770297a1..22b1d767a7 100644 --- a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java +++ b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java @@ -659,6 +659,14 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP return channelsByName.keySet().toArray(new String[channelsByName.size()]); } + public MessageHandlerMetrics getHandlerMetrics(String name) { + if (handlersByName.containsKey(name)) { + return handlersByName.get(name); + } + logger.debug("No handler found for (" + name + ")"); + return null; + } + public Statistics getHandlerDuration(String name) { if (handlersByName.containsKey(name)) { return handlersByName.get(name).getDuration(); @@ -667,6 +675,14 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP return null; } + public MessageSourceMetrics getSourceMetrics(String name) { + if (sourcesByName.containsKey(name)) { + return sourcesByName.get(name); + } + logger.debug("No source found for (" + name + ")"); + return null; + } + public int getSourceMessageCount(String name) { return (int) getSourceMessageCountLong(name); } @@ -679,6 +695,38 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP return -1; } + public MessageChannelMetrics getChannelMetrics(String name) { + if (channelsByName.containsKey(name)) { + return channelsByName.get(name); + } + logger.debug("No channel found for (" + name + ")"); + return null; + } + + public int getChannelSendCount(String name) { + return (int) getChannelSendCountLong(name); + } + + public long getChannelSendCountLong(String name) { + if (channelsByName.containsKey(name)) { + return channelsByName.get(name).getSendCountLong(); + } + logger.debug("No channel found for (" + name + ")"); + return -1; + } + + public int getChannelSendErrorCount(String name) { + return (int) getChannelSendErrorCountLong(name); + } + + public long getChannelSendErrorCountLong(String name) { + if (channelsByName.containsKey(name)) { + return channelsByName.get(name).getSendErrorCountLong(); + } + logger.debug("No channel found for (" + name + ")"); + return -1; + } + public int getChannelReceiveCount(String name) { return (int) getChannelReceiveCountLong(name); } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml index 1263451ade..b2bb2d3f14 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml @@ -8,10 +8,11 @@ - + - + @@ -22,4 +23,8 @@ + + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java index c1edbd8613..a46a109126 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java @@ -1,31 +1,42 @@ /* - * Copyright 2009-2010 the original author or authors. - * + * Copyright 2009-2015 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ package org.springframework.integration.monitor; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.MessageDeliveryException; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +/** + * @author Dave Syer + * @author Gary Russell + * @since 2.0 + */ @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) +@DirtiesContext public class ChannelIntegrationTests { @Autowired @@ -39,19 +50,43 @@ public class ChannelIntegrationTests { @Test public void testMessageChannelStatistics() throws Exception { - + requests.send(new GenericMessage("foo")); + String intermediateChannelName = "" + intermediate; + + assertEquals(1, messageChannelsMonitor.getChannelSendCount(intermediateChannelName)); + double rate = messageChannelsMonitor.getChannelSendRate("" + requests).getMean(); assertTrue("No statistics for requests channel", rate >= 0); - rate = messageChannelsMonitor.getChannelSendRate("" + intermediate).getMean(); + rate = messageChannelsMonitor.getChannelSendRate(intermediateChannelName).getMean(); assertTrue("No statistics for intermediate channel", rate >= 0); - + assertNotNull(intermediate.receive(100L)); - double count = messageChannelsMonitor.getChannelReceiveCount("" + intermediate); - assertTrue("No statistics for intermediate channel", count >= 0); - + assertEquals(1, messageChannelsMonitor.getChannelReceiveCount(intermediateChannelName)); + + requests.send(new GenericMessage("foo")); + try { + requests.send(new GenericMessage("foo")); + } + catch (MessageDeliveryException e) { + } + + assertEquals(3, messageChannelsMonitor.getChannelSendCount(intermediateChannelName)); + + assertEquals(1, messageChannelsMonitor.getChannelSendErrorCount(intermediateChannelName)); + + MessageHandlerMetrics handlerMetrics = messageChannelsMonitor.getHandlerMetrics("bridge"); + + assertEquals(3, handlerMetrics.getHandleCount()); + assertEquals(1, handlerMetrics.getErrorCount()); + + Thread.sleep(50); + + assertTrue(messageChannelsMonitor.getSourceMessageCount("source") > 0); + assertTrue(messageChannelsMonitor.getSourceMetrics("source").getMessageCount() > 0); + } }