INT-1265, INT-1266, INT-1267: create new mbean exporter and move monitoring to there

This commit is contained in:
David Syer
2010-09-03 12:02:54 +00:00
parent 69b296443a
commit 53f975311a
36 changed files with 2501 additions and 293 deletions

View File

@@ -0,0 +1,37 @@
package org.springframework.integration;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.integration.control.ControlBusXmlTests;
import org.springframework.integration.monitor.MessageChannelsMonitorIntegrationTests;
/*
* Copyright 2009-2010 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.
*/
/**
* A test suite that is ignored, but can be resurrected to help debug ordering issues in tests.
*
* @author Dave Syer
*
*/
@RunWith(Suite.class)
@SuiteClasses(value = { ControlBusXmlTests.class, MessageChannelsMonitorIntegrationTests.class })
@Ignore
public class IgnoredTestSuite {
}

View File

@@ -19,10 +19,9 @@ package org.springframework.integration.control;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import javax.management.MBeanServer;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.Message;
@@ -30,8 +29,9 @@ import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.jmx.JmxHeaders;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.jmx.support.JmxUtils;
import org.springframework.jmx.support.MBeanServerFactoryBean;
/**
* @author Mark Fisher
@@ -39,8 +39,6 @@ import org.springframework.jmx.support.JmxUtils;
*/
public class ControlBusOperationChannelTests {
private final MBeanServer server = JmxUtils.locateMBeanServer();
private final String domain = "domain.test";
@@ -49,12 +47,9 @@ public class ControlBusOperationChannelTests {
GenericApplicationContext context = new GenericApplicationContext();
RootBeanDefinition endpointDef = new RootBeanDefinition(EventDrivenConsumer.class);
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new DirectChannel());
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new TestHandler());
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RootBeanDefinition(TestHandler.class));
context.registerBeanDefinition("testEndpoint", endpointDef);
RootBeanDefinition busDef = new RootBeanDefinition(ControlBus.class);
busDef.getConstructorArgumentValues().addGenericArgumentValue(server);
busDef.getConstructorArgumentValues().addGenericArgumentValue(domain);
context.registerBeanDefinition("controlBus", busDef);
registerControlBus(context, domain);
context.refresh();
ControlBus controlBus = context.getBean("controlBus", ControlBus.class);
EventDrivenConsumer endpoint = context.getBean("testEndpoint", EventDrivenConsumer.class);
@@ -74,6 +69,20 @@ public class ControlBusOperationChannelTests {
context.close();
}
private BeanDefinition registerControlBus(GenericApplicationContext context, String domain) {
RootBeanDefinition serverDef = new RootBeanDefinition(MBeanServerFactoryBean.class);
serverDef.getPropertyValues().add("locateExistingServerIfPossible", true);
context.registerBeanDefinition("mbeanServer", serverDef);
BeanDefinition exporterDef = new RootBeanDefinition(IntegrationMBeanExporter.class);
exporterDef.getPropertyValues().addPropertyValue("server", new RuntimeBeanReference("mbeanServer"));
exporterDef.getPropertyValues().addPropertyValue("domain", domain);
context.registerBeanDefinition("exporter", exporterDef);
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("exporter"));
context.registerBeanDefinition("controlBus", controlBusDef);
return exporterDef;
}
private static class TestHandler implements MessageHandler {
public void handleMessage(Message<?> message) {

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.control;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;
@@ -29,20 +30,21 @@ import javax.management.ObjectName;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.handler.BridgeHandler;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.monitor.LifecycleMessageHandlerMonitor;
import org.springframework.integration.monitor.QueueChannelMonitor;
import org.springframework.integration.monitor.SimpleMessageChannelMonitor;
import org.springframework.jmx.support.MBeanServerFactoryBean;
import org.springframework.jmx.support.ObjectNameManager;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -76,62 +78,51 @@ public class ControlBusTests {
this.context.close();
}
@Test
public void directChannelRegistered() throws Exception {
context.registerBeanDefinition("directChannel", new RootBeanDefinition(DirectChannel.class));
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.test1");
context.registerBeanDefinition("controlBus", controlBusDef);
registerControlBus(context, "domain.test1");
context.refresh();
MBeanServer mbeanServer = context.getBean("mbeanServer", MBeanServer.class);
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance("domain.test1:type=channel,name=directChannel"));
assertEquals(DirectChannel.class.getName(), instance.getClassName());
ObjectInstance instance = mbeanServer.getObjectInstance(ObjectNameManager
.getInstance("domain.test1:type=MessageChannel,name=directChannel"));
assertEquals(SimpleMessageChannelMonitor.class.getName(), instance.getClassName());
}
@Test
public void anonymousDirectChannelRegistered() throws Exception {
context.registerBeanDefinition("org.springframework.integration.generated#0", new RootBeanDefinition(DirectChannel.class));
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.test1b");
context.registerBeanDefinition("controlBus", controlBusDef);
context.registerBeanDefinition("org.springframework.integration.generated#0", new RootBeanDefinition(
DirectChannel.class));
registerControlBus(context, "domain.test1b");
context.refresh();
MBeanServer mbeanServer = context.getBean("mbeanServer", MBeanServer.class);
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance("domain.test1b:type=channel,name=anonymous,generated=org.springframework.integration.generated#0"));
assertEquals(DirectChannel.class.getName(), instance.getClassName());
ObjectInstance instance = mbeanServer
.getObjectInstance(ObjectNameManager
.getInstance("domain.test1b:type=MessageChannel,name=org.springframework.integration.generated#0,source=anonymous"));
assertEquals(SimpleMessageChannelMonitor.class.getName(), instance.getClassName());
}
@Test
public void staticObjectNamePropertiesRegistered() throws Exception {
context.registerBeanDefinition("directChannel", new RootBeanDefinition(DirectChannel.class));
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.test1a");
controlBusDef.getPropertyValues().add("objectNameStaticProperties", Collections.singletonMap("foo","bar"));
context.registerBeanDefinition("controlBus", controlBusDef);
BeanDefinition exporterDef = registerControlBus(context, "domain.test1a");
exporterDef.getPropertyValues().add("objectNameStaticProperties", Collections.singletonMap("foo", "bar"));
context.refresh();
MBeanServer mbeanServer = context.getBean("mbeanServer", MBeanServer.class);
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance("domain.test1a:type=channel,foo=bar,name=directChannel"));
assertEquals(DirectChannel.class.getName(), instance.getClassName());
ObjectInstance instance = mbeanServer.getObjectInstance(ObjectNameManager
.getInstance("domain.test1a:type=MessageChannel,name=directChannel,foo=bar"));
assertEquals(SimpleMessageChannelMonitor.class.getName(), instance.getClassName());
}
@Test
public void queueChannelRegistered() throws Exception {
context.registerBeanDefinition("queueChannel", new RootBeanDefinition(QueueChannel.class));
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.test2");
context.registerBeanDefinition("controlBus", controlBusDef);
registerControlBus(context, "domain.test2");
context.refresh();
MBeanServer mbeanServer = context.getBean("mbeanServer", MBeanServer.class);
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance("domain.test2:type=channel,name=queueChannel"));
assertEquals(QueueChannel.class.getName(), instance.getClassName());
ObjectInstance instance = mbeanServer.getObjectInstance(ObjectNameManager
.getInstance("domain.test2:type=MessageChannel,name=queueChannel"));
assertEquals(QueueChannelMonitor.class.getName(), instance.getClassName());
}
@Test
@@ -139,17 +130,14 @@ public class ControlBusTests {
context.registerBeanDefinition("testChannel", new RootBeanDefinition(DirectChannel.class));
RootBeanDefinition endpointDef = new RootBeanDefinition(EventDrivenConsumer.class);
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("testChannel"));
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new BridgeHandler());
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RootBeanDefinition(BridgeHandler.class));
context.registerBeanDefinition("eventDrivenConsumer", endpointDef);
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.test3");
context.registerBeanDefinition("controlBus", controlBusDef);
registerControlBus(context, "domain.test3");
context.refresh();
MBeanServer mbeanServer = context.getBean("mbeanServer", MBeanServer.class);
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance("domain.test3:type=endpoint,name=eventDrivenConsumer"));
assertEquals(EventDrivenConsumer.class.getName(), instance.getClassName());
ObjectInstance instance = mbeanServer.getObjectInstance(ObjectNameManager
.getInstance("domain.test3:type=MessageHandler,name=eventDrivenConsumer,bean=endpoint"));
assertEquals(LifecycleMessageHandlerMonitor.class.getName(), instance.getClassName());
}
@Test
@@ -157,19 +145,16 @@ public class ControlBusTests {
context.registerBeanDefinition("testChannel", new RootBeanDefinition(QueueChannel.class));
RootBeanDefinition endpointDef = new RootBeanDefinition(PollingConsumer.class);
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("testChannel"));
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new BridgeHandler());
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RootBeanDefinition(BridgeHandler.class));
endpointDef.getPropertyValues().add("trigger", new PeriodicTrigger(10000));
context.registerBeanDefinition("pollingConsumer", endpointDef);
context.registerBeanDefinition("taskScheduler", new RootBeanDefinition(ThreadPoolTaskScheduler.class));
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.test4");
context.registerBeanDefinition("controlBus", controlBusDef);
registerControlBus(context, "domain.test4");
context.refresh();
MBeanServer mbeanServer = context.getBean("mbeanServer", MBeanServer.class);
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance("domain.test4:type=endpoint,name=pollingConsumer"));
assertEquals(PollingConsumer.class.getName(), instance.getClassName());
ObjectInstance instance = mbeanServer.getObjectInstance(ObjectNameManager
.getInstance("domain.test4:type=MessageHandler,name=pollingConsumer,bean=endpoint"));
assertEquals(LifecycleMessageHandlerMonitor.class.getName(), instance.getClassName());
}
@Test
@@ -180,6 +165,7 @@ public class ControlBusTests {
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("testChannel"));
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new MessageHandler() {
private final AtomicInteger count = new AtomicInteger();
public void handleMessage(Message<?> message) {
int current = count.incrementAndGet();
if (current % 10 == 0) {
@@ -188,24 +174,23 @@ public class ControlBusTests {
}
});
context.registerBeanDefinition("testEndpoint", endpointDef);
registerControlBus(context, "domain.channel.monitor");
context.refresh();
MBeanServer server = context.getBean("mbeanServer", MBeanServer.class);
ObjectName objectName = ObjectNameManager.getInstance("domain.channel.monitor:type=MessageChannel,name=testChannel");
assertNotNull(server.getObjectInstance(objectName));
}
private BeanDefinition registerControlBus(GenericApplicationContext context, String domain) {
BeanDefinition exporterDef = new RootBeanDefinition(IntegrationMBeanExporter.class);
exporterDef.getPropertyValues().addPropertyValue("server", new RuntimeBeanReference("mbeanServer"));
exporterDef.getPropertyValues().addPropertyValue("domain", domain);
context.registerBeanDefinition("exporter", exporterDef);
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.channel.monitor");
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("exporter"));
context.registerBeanDefinition("controlBus", controlBusDef);
context.refresh();
MessageChannel channel = context.getBean("testChannel", MessageChannel.class);
for (int i = 0; i < 100; i++) {
try {
channel.send(new GenericMessage<String>("foo"));
}
catch (Exception e) {
// ignore
}
}
MBeanServer server = context.getBean("mbeanServer", MBeanServer.class);
ObjectName objectName = ObjectNameManager.getInstance("domain.channel.monitor:type=channel,name=testChannel");
assertEquals(90L, server.getAttribute(objectName, "SendSuccessCount"));
assertEquals(10L, server.getAttribute(objectName, "SendErrorCount"));
return exporterDef;
}
}

View File

@@ -22,9 +22,14 @@
</int:bridge>
<bean id="controlBus" class="org.springframework.integration.control.ControlBus">
<constructor-arg ref="mbeanExporter" />
<constructor-arg ref="mbeanServer" />
</bean>
<bean id="mbeanExporter" class="org.springframework.integration.monitor.IntegrationMBeanExporter">
<property name="server" ref="mbeanServer" />
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>

View File

@@ -26,11 +26,11 @@ import javax.management.ObjectInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.monitor.LifecycleMessageHandlerMonitor;
import org.springframework.integration.monitor.QueueChannelMonitor;
import org.springframework.integration.monitor.SimpleMessageChannelMonitor;
import org.springframework.jmx.support.ObjectNameManager;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -41,7 +41,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
public class ControlBusXmlTests {
private static final String DOMAIN = "org.springframework.integration";
private static final String DOMAIN = "spring.application";
@Autowired
@@ -51,37 +51,39 @@ public class ControlBusXmlTests {
@Test
public void directChannelRegistered() throws Exception {
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance(DOMAIN + ":type=channel,name=testDirectChannel"));
assertEquals(DirectChannel.class.getName(), instance.getClassName());
ObjectNameManager.getInstance(DOMAIN + ":type=MessageChannel,name=testDirectChannel"));
assertEquals(SimpleMessageChannelMonitor.class.getName(), instance.getClassName());
}
@Test
public void queueChannelRegistered() throws Exception {
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance(DOMAIN + ":type=channel,name=testQueueChannel"));
assertEquals(QueueChannel.class.getName(), instance.getClassName());
ObjectNameManager.getInstance(DOMAIN + ":type=MessageChannel,name=testQueueChannel"));
assertEquals(QueueChannelMonitor.class.getName(), instance.getClassName());
}
@Test
public void eventDrivenConsumerRegistered() throws Exception {
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance(DOMAIN + ":type=endpoint,name=testEventDrivenBridge"));
assertEquals(EventDrivenConsumer.class.getName(), instance.getClassName());
ObjectNameManager.getInstance(DOMAIN + ":type=MessageHandler,name=testEventDrivenBridge,bean=endpoint"));
assertEquals(LifecycleMessageHandlerMonitor.class.getName(), instance.getClassName());
}
@Test
public void anonymousConsumerRegistered() throws Exception {
Set<ObjectInstance> instances = mbeanServer.queryMBeans(
ObjectNameManager.getInstance(DOMAIN + ":type=endpoint,name=anonymous,*"), null);
ObjectNameManager.getInstance(DOMAIN + ":type=MessageHandler,bean=anonymous,*"), null);
assertEquals(1, instances.size());
assertEquals(EventDrivenConsumer.class.getName(), instances.iterator().next().getClassName());
assertEquals(LifecycleMessageHandlerMonitor.class.getName(), instances.iterator().next().getClassName());
}
@Test
// Assume this one runs last and force MBeans to be unregistered to avoid clashes in later tests
@DirtiesContext
public void pollingConsumerRegistered() throws Exception {
ObjectInstance instance = mbeanServer.getObjectInstance(
ObjectNameManager.getInstance(DOMAIN + ":type=endpoint,name=testPollingBridge"));
assertEquals(PollingConsumer.class.getName(), instance.getClassName());
ObjectNameManager.getInstance(DOMAIN + ":type=MessageHandler,name=testPollingBridge,bean=endpoint"));
assertEquals(LifecycleMessageHandlerMonitor.class.getName(), instance.getClassName());
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<int:channel id="requests" />
<int:channel id="intermediate">
<int:queue capacity="99" />
</int:channel>
<int:bridge id="bridge" input-channel="requests" output-channel="intermediate"/>
<bean id="integrationExecutions" class="org.springframework.integration.monitor.IntegrationMBeanExporter">
<property name="server" ref="mbeanServer" />
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>
</beans>

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2009-2010 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.assertNotNull;
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.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ChannelIntegrationTests {
@Autowired
private MessageChannel requests;
@Autowired
private PollableChannel intermediate;
@Autowired
private IntegrationMBeanExporter messageChannelsMonitor;
@Test
@DirtiesContext
public void testMessageChannelStatistics() throws Exception {
requests.send(new GenericMessage<String>("foo"));
double duration = messageChannelsMonitor.getChannelMeanSendDuration("" + requests);
assertTrue("No statistics for requests channel", duration >= 0);
duration = messageChannelsMonitor.getChannelMeanSendDuration("" + intermediate);
assertTrue("No statistics for intermediate channel", duration >= 0);
assertNotNull(intermediate.receive(100L));
double count = messageChannelsMonitor.getChannelReceiveCount("" + intermediate);
assertTrue("No statistics for intermediate channel", count >= 0);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2002-2010 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 org.junit.Test;
/**
* @author Dave Syer
*
*/
public class ExponentialMovingAverageCumulativeHistoryTests {
private ExponentialMovingAverageCumulativeHistory history = new ExponentialMovingAverageCumulativeHistory(10);
@Test
public void testGetCount() {
assertEquals(0, history.getCount());
history.append(1);
assertEquals(1, history.getCount());
}
@Test
public void testGetMean() throws Exception {
assertEquals(0, history.getMean(), 0.01);
history.append(1);
history.append(1);
assertEquals(1, history.getMean(), 0.01);
}
@Test
public void testGetStandardDeviation() throws Exception {
assertEquals(0, history.getStandardDeviation(), 0.01);
history.append(1);
history.append(1);
assertEquals(0, history.getStandardDeviation(), 0.01);
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2002-2010 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.assertTrue;
import org.junit.Test;
/**
* @author Dave Syer
*
*/
public class ExponentialMovingAverageRateCumulativeHistoryTests {
private ExponentialMovingAverageRateCumulativeHistory history = new ExponentialMovingAverageRateCumulativeHistory(
1., 10., 10);
@Test
public void testGetCount() {
assertEquals(0, history.getCount());
history.increment();
assertEquals(1, history.getCount());
}
@Test
public void testGetTimeSinceLastMeasurement() throws Exception {
history.increment();
Thread.sleep(20L);
assertTrue(history.getTimeSinceLastMeasurement() > 0);
}
@Test
public void testGetEarlyMean() throws Exception {
assertEquals(0, history.getMean(), 0.01);
Thread.sleep(20L);
history.increment();
assertEquals(50, history.getMean(), 10);
}
@Test
public void testGetMean() throws Exception {
assertEquals(0, history.getMean(), 0.01);
Thread.sleep(20L);
history.increment();
Thread.sleep(20L);
history.increment();
assertEquals(50, history.getMean(), 10);
Thread.sleep(20L);
assertEquals(35, history.getMean(), 10);
}
@Test
public void testGetStandardDeviation() throws Exception {
assertEquals(0, history.getStandardDeviation(), 0.01);
Thread.sleep(20L);
history.increment();
Thread.sleep(22L);
history.increment();
Thread.sleep(18L);
assertEquals(1.5, history.getStandardDeviation(), 1);
}
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2002-2010 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.assertTrue;
import org.junit.Test;
/**
* @author Dave Syer
*
*/
public class ExponentialMovingAverageRatioCumulativeHistoryTests {
private ExponentialMovingAverageRatioCumulativeHistory history = new ExponentialMovingAverageRatioCumulativeHistory(
0.5, 10);
@Test
public void testGetCount() {
assertEquals(0, history.getCount());
history.success();
assertEquals(1, history.getCount());
}
@Test
public void testGetTimeSinceLastMeasurement() throws Exception {
history.success();
Thread.sleep(20L);
assertTrue(history.getTimeSinceLastMeasurement() > 0);
}
@Test
public void testGetEarlyMean() throws Exception {
assertEquals(0, history.getMean(), 0.01);
history.success();
assertEquals(1, history.getMean(), 0.01);
}
@Test
public void testGetEarlyFailure() throws Exception {
assertEquals(0, history.getMean(), 0.01);
history.failure();
assertEquals(0, history.getMean(), 0.01);
}
@Test
public void testDecayedMean() throws Exception {
history.failure();
Thread.sleep(200L);
assertEquals(average(0, Math.exp(-0.4)), history.getMean(), 0.01);
}
@Test
public void testGetMean() throws Exception {
assertEquals(0, history.getMean(), 0.01);
history.success();
assertEquals(1, history.getMean(), 0.01);
history.success();
assertEquals(1, history.getMean(), 0.01);
history.success();
assertEquals(1, history.getMean(), 0.01);
}
@Test
public void testGetMeanFailuresHighRate() throws Exception {
assertEquals(0, history.getMean(), 0.01);
history.success();
assertEquals(average(1), history.getMean(), 0.01);
history.failure();
assertEquals(average(1, 0.5), history.getMean(), 0.1);
history.success();
assertEquals(average(1, 0.5, 0.67), history.getMean(), 0.1);
}
@Test
public void testGetMeanFailuresLowRate() throws Exception {
assertEquals(0, history.getMean(), 0.01);
history.failure();
assertEquals(average(0), history.getMean(), 0.01);
history.failure();
assertEquals(average(0, 0), history.getMean(), 0.01);
history.success();
assertEquals(average(0, 0, 0.33), history.getMean(), 0.1);
}
@Test
public void testGetStandardDeviation() throws Exception {
assertEquals(0, history.getStandardDeviation(), 0.01);
history.success();
assertEquals(0, history.getStandardDeviation(), 1);
}
private double average(double... values) {
int count = 0;
double sum = 0;
for (double d : values) {
sum += d;
count++;
}
return sum / count;
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2009-2010 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.assertTrue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.message.GenericMessage;
public class HandlerMonitoringIntegrationTests {
private static Log logger = LogFactory.getLog(HandlerMonitoringIntegrationTests.class);
private MessageChannel channel;
private Service service;
private IntegrationMBeanExporter messageHandlersMonitor;
public void setMessageHandlersMonitor(IntegrationMBeanExporter messageHandlersMonitor) {
this.messageHandlersMonitor = messageHandlersMonitor;
}
public void setService(Service service) {
this.service = service;
}
@Test
public void testSendAndHandleWithEndpointName() throws Exception {
// The handler monitor is registered under the endpoint id (since it is explicit)
doTest("explicit-handler.xml", "input", "explicit");
}
@Test
public void testSendAndHandleWithAnonymousHandler() throws Exception {
doTest("anonymous-handler.xml", "anonymous", "anonymous");
}
@Test
public void testSendAndHandleWithProxiedHandler() throws Exception {
doTest("proxy-handler.xml", "anonymous", "anonymous");
}
@Test
public void testErrorLogger() throws Exception {
ClassPathXmlApplicationContext context = createContext("anonymous-handler.xml", "anonymous");
try {
assertTrue(messageHandlersMonitor.getHandlerNames().contains("errorLogger"));
}
finally {
context.close();
}
}
private void doTest(String config, String channelName, String monitor) throws Exception {
ClassPathXmlApplicationContext context = createContext(config, channelName);
try {
int before = service.getCounter();
channel.send(new GenericMessage<String>("bar"));
assertEquals(before + 1, service.getCounter());
double duration = messageHandlersMonitor.getHandlerMeanDuration(monitor);
assertTrue("No statistics for input channel", duration > 0);
} finally {
context.close();
}
}
private ClassPathXmlApplicationContext createContext(String config, String channelName) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config, getClass());
context.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
channel = context.getBean(channelName, MessageChannel.class);
return context;
}
public static interface Service {
void execute(String input) throws Exception;
int getCounter();
}
public static class SimpleService implements Service {
private int counter;
public void execute(String input) throws Exception {
Thread.sleep(10L); // make the duration non-zero
counter++;
}
public int getCounter() {
return counter;
}
}
@Aspect
public static class HandlerInterceptor {
@Before("execution(* *..*Tests*(String)) && args(input)")
public void around(String input) {
logger.debug("Handling: "+input);
}
}
}

View File

@@ -0,0 +1,211 @@
/*
* Copyright 2009-2010 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.assertTrue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.message.GenericMessage;
public class MessageChannelsMonitorIntegrationTests {
private static Log logger = LogFactory.getLog(MessageChannelsMonitorIntegrationTests.class);
private MessageChannel channel;
private Service service;
private IntegrationMBeanExporter messageChannelsMonitor;
public void setMessageHandlersMonitor(IntegrationMBeanExporter messageChannelsMonitor) {
this.messageChannelsMonitor = messageChannelsMonitor;
}
public void setService(Service service) {
this.service = service;
}
@Test
public void testSendWithAnonymousHandler() throws Exception {
doTest("anonymous-channel.xml", "anonymous");
}
@Test
public void testSendWithProxiedChannel() throws Exception {
doTest("proxy-channel.xml", "anonymous");
}
@Test
public void testRates() throws Exception {
ClassPathXmlApplicationContext context = createContext("anonymous-channel.xml", "anonymous");
try {
int before = service.getCounter();
for (int i = 0; i < 50; i++) {
channel.send(new GenericMessage<String>("bar"));
Thread.sleep(20L);
}
assertEquals(before + 50, service.getCounter());
// The handler monitor is registered under the endpoint id (since it is explicit)
double sends = messageChannelsMonitor.getChannelSendCount("" + channel);
assertEquals("No send statistics for input channel", 50, sends, 0.01);
double rate = messageChannelsMonitor.getChannelSendRate("" + channel);
assertTrue(String.format("Unexpected rate statistics for input channel %f %f", 60000. / 20L, rate),
60000. / 20L > rate && rate > 0);
}
finally {
context.close();
}
}
@Test
public void testErrors() throws Exception {
ClassPathXmlApplicationContext context = createContext("anonymous-channel.xml", "anonymous");
try {
int before = service.getCounter();
for (int i = 0; i < 5; i++) {
channel.send(new GenericMessage<String>("bar"));
Thread.sleep(20L);
}
try {
channel.send(new GenericMessage<String>("fail"));
}
catch (MessageHandlingException e) {
// ignore
}
for (int i = 0; i < 5; i++) {
channel.send(new GenericMessage<String>("bar"));
Thread.sleep(20L);
}
assertEquals(before + 10, service.getCounter());
// The handler monitor is registered under the endpoint id (since it is explicit)
double sends = messageChannelsMonitor.getChannelSendCount("" + channel);
assertEquals("No send statistics for input channel", 11, sends, 0.01);
double errors = messageChannelsMonitor.getChannelSendErrorCount("" + channel);
assertEquals("No error statistics for input channel", 1, errors, 0.01);
double rate = messageChannelsMonitor.getChannelErrorRate("" + channel);
assertTrue(String.format("Unexpected error statistics for input channel %f %f", 60000. / 20L, rate),
60000. / 20L > rate && rate > 0);
}
finally {
context.close();
}
}
@Test
public void testQueues() throws Exception {
ClassPathXmlApplicationContext context = createContext("queue-channel.xml", "queue");
try {
int before = service.getCounter();
for (int i = 0; i < 5; i++) {
channel.send(new GenericMessage<String>("bar"));
Thread.sleep(20L);
}
try {
channel.send(new GenericMessage<String>("fail"));
}
catch (MessageHandlingException e) {
// ignore
}
for (int i = 0; i < 5; i++) {
channel.send(new GenericMessage<String>("bar"));
Thread.sleep(20L);
}
assertEquals(before + 10, service.getCounter());
// The handler monitor is registered under the endpoint id (since it is explicit)
long sends = messageChannelsMonitor.getChannelSendCount("" + channel);
assertEquals("No send statistics for input channel", 11, sends);
long receives = messageChannelsMonitor.getChannelReceiveCount("" + channel);
assertEquals("No send statistics for input channel", 11, receives);
long errors = messageChannelsMonitor.getChannelSendErrorCount("" + channel);
assertEquals("Expect no errors for input channel (handler fails)", 0, errors);
}
finally {
context.close();
}
}
private void doTest(String config, String channelName) throws Exception {
ClassPathXmlApplicationContext context = createContext(config, channelName);
try {
int before = service.getCounter();
channel.send(new GenericMessage<String>("bar"));
assertEquals(before + 1, service.getCounter());
// The handler monitor is registered under the endpoint id (since it is explicit)
double sends = messageChannelsMonitor.getChannelSendCount("" + channel);
assertEquals("No statistics for input channel", 1, sends, 0.01);
}
finally {
context.close();
}
}
private ClassPathXmlApplicationContext createContext(String config, String channelName) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config, getClass());
context.getAutowireCapableBeanFactory().autowireBeanProperties(this,
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
channel = context.getBean(channelName, MessageChannel.class);
return context;
}
public static class Service {
private int counter;
public void execute(String input) {
if ("fail".equals(input)) {
throw new RuntimeException("Planned");
}
counter++;
}
public int getCounter() {
return counter;
}
}
@Aspect
public static class ChannelInterceptor {
@Before("execution(* *..MessageChannel+.send(*)) && args(input)")
public void around(Message<?> input) {
logger.debug("Handling: " + input);
}
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<import resource="common-context.xml" />
<int:channel id="anonymous" />
<int:service-activator input-channel="anonymous" ref="service" />
<bean id="service" class="org.springframework.integration.monitor.MessageChannelsMonitorIntegrationTests$Service" />
</beans>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<import resource="common-context.xml" />
<int:channel id="anonymous" />
<int:service-activator input-channel="anonymous" ref="service" />
<bean id="service" class="org.springframework.integration.monitor.HandlerMonitoringIntegrationTests$SimpleService" />
</beans>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="integrationExecutions" class="org.springframework.integration.monitor.IntegrationMBeanExporter">
<property name="server" ref="mbeanServer" />
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>
</beans>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<import resource="common-context.xml" />
<int:channel id="input" />
<int:service-activator id="explicit" input-channel="input" ref="service" />
<bean id="service" class="org.springframework.integration.monitor.HandlerMonitoringIntegrationTests$SimpleService" />
</beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<import resource="common-context.xml" />
<aop:aspectj-autoproxy/>
<int:channel id="anonymous" />
<int:service-activator input-channel="anonymous" ref="service" />
<bean id="service" class="org.springframework.integration.monitor.MessageChannelsMonitorIntegrationTests$Service" />
<bean id="interceptor" class="org.springframework.integration.monitor.MessageChannelsMonitorIntegrationTests$ChannelInterceptor" />
</beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<import resource="common-context.xml" />
<aop:aspectj-autoproxy/>
<int:channel id="anonymous" />
<int:service-activator input-channel="anonymous" ref="service" />
<bean id="service" class="org.springframework.integration.monitor.HandlerMonitoringIntegrationTests$SimpleService" />
<bean id="interceptor" class="org.springframework.integration.monitor.HandlerMonitoringIntegrationTests$HandlerInterceptor" />
</beans>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<import resource="common-context.xml" />
<int:channel id="queue">
<int:queue/>
</int:channel>
<int:poller default="true" fixed-rate="10"/>
<int:service-activator input-channel="queue" ref="service"/>
<bean id="service" class="org.springframework.integration.monitor.MessageChannelsMonitorIntegrationTests$Service" />
</beans>