renamed modules org.springframework.integration.* -> spring-integration-*
@Ignore'd SimpleTcpNetOutboundGatewayTests#testOutboundClose() to avoid failure; this failure is correlated to the module name change, but hard to understand how it would be caused by it
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.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.support.RootBeanDefinition;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.jmx.JmxHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageHandler;
|
||||
import org.springframework.jmx.support.JmxUtils;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class ControlBusOperationChannelTests {
|
||||
|
||||
private final MBeanServer server = JmxUtils.locateMBeanServer();
|
||||
|
||||
private final String domain = "domain.test";
|
||||
|
||||
|
||||
@Test
|
||||
public void replyProducingOperation() throws Exception {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
RootBeanDefinition endpointDef = new RootBeanDefinition(EventDrivenConsumer.class);
|
||||
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new DirectChannel());
|
||||
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new TestHandler());
|
||||
context.registerBeanDefinition("testEndpoint", endpointDef);
|
||||
RootBeanDefinition busDef = new RootBeanDefinition(ControlBus.class);
|
||||
busDef.getConstructorArgumentValues().addGenericArgumentValue(server);
|
||||
busDef.getConstructorArgumentValues().addGenericArgumentValue(domain);
|
||||
context.registerBeanDefinition("controlBus", busDef);
|
||||
context.refresh();
|
||||
ControlBus controlBus = context.getBean("controlBus", ControlBus.class);
|
||||
EventDrivenConsumer endpoint = context.getBean("testEndpoint", EventDrivenConsumer.class);
|
||||
Message<?> stopMessage = MessageBuilder.withPayload("test")
|
||||
.setHeader(ControlBus.TARGET_BEAN_NAME, "testEndpoint")
|
||||
.setHeader(JmxHeaders.OPERATION_NAME, "stop")
|
||||
.build();
|
||||
Message<?> startMessage = MessageBuilder.withPayload("test")
|
||||
.setHeader(ControlBus.TARGET_BEAN_NAME, "testEndpoint")
|
||||
.setHeader(JmxHeaders.OPERATION_NAME, "start")
|
||||
.build();
|
||||
assertTrue("endpoint should be running after startup", endpoint.isRunning());
|
||||
controlBus.getOperationChannel().send(stopMessage);
|
||||
assertFalse("endpoint should have been stopped", endpoint.isRunning());
|
||||
controlBus.getOperationChannel().send(startMessage);
|
||||
assertTrue("endpoint should be running after being restarted", endpoint.isRunning());
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
private static class TestHandler implements MessageHandler {
|
||||
public void handleMessage(Message<?> message) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* 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.control;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.MBeanServerFactory;
|
||||
import javax.management.ObjectInstance;
|
||||
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.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.endpoint.PollingConsumer;
|
||||
import org.springframework.integration.handler.BridgeHandler;
|
||||
import org.springframework.integration.message.MessageHandler;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
||||
import org.springframework.jmx.support.ObjectNameManager;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.PeriodicTrigger;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class ControlBusTests {
|
||||
|
||||
private volatile GenericApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void createContext() {
|
||||
this.context = new GenericApplicationContext();
|
||||
RootBeanDefinition serverDef = new RootBeanDefinition(MBeanServerFactoryBean.class);
|
||||
serverDef.getPropertyValues().add("locateExistingServerIfPossible", true);
|
||||
context.registerBeanDefinition("mbeanServer", serverDef);
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeContext() {
|
||||
MBeanServer mbeanServer = this.context.getBean("mbeanServer", MBeanServer.class);
|
||||
try {
|
||||
MBeanServerFactory.releaseMBeanServer(mbeanServer);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
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);
|
||||
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());
|
||||
}
|
||||
|
||||
@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);
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eventDrivenConsumerRegistered() throws Exception {
|
||||
context.registerBeanDefinition("testChannel", new RootBeanDefinition(DirectChannel.class));
|
||||
RootBeanDefinition endpointDef = new RootBeanDefinition(EventDrivenConsumer.class);
|
||||
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("testChannel"));
|
||||
endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new BridgeHandler());
|
||||
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);
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pollingConsumerRegistered() throws Exception {
|
||||
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.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);
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void channelMonitoring() throws Exception {
|
||||
RootBeanDefinition channelDef = new RootBeanDefinition(DirectChannel.class);
|
||||
context.registerBeanDefinition("testChannel", channelDef);
|
||||
RootBeanDefinition endpointDef = new RootBeanDefinition(EventDrivenConsumer.class);
|
||||
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) {
|
||||
throw new RuntimeException("intentional test failure");
|
||||
}
|
||||
}
|
||||
});
|
||||
context.registerBeanDefinition("testEndpoint", endpointDef);
|
||||
BeanDefinition controlBusDef = new RootBeanDefinition(ControlBus.class);
|
||||
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("mbeanServer"));
|
||||
controlBusDef.getConstructorArgumentValues().addGenericArgumentValue("domain.channel.monitor");
|
||||
context.registerBeanDefinition("controlBus", controlBusDef);
|
||||
context.refresh();
|
||||
MessageChannel channel = context.getBean("testChannel", MessageChannel.class);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
try {
|
||||
channel.send(new StringMessage("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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?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="testDirectChannel"/>
|
||||
|
||||
<int:channel id="testQueueChannel">
|
||||
<int:queue capacity="99"/>
|
||||
</int:channel>
|
||||
|
||||
<int:bridge id="testEventDrivenBridge" input-channel="testDirectChannel"/>
|
||||
|
||||
<int:bridge id="testPollingBridge" input-channel="testQueueChannel" output-channel="nullChannel">
|
||||
<int:poller max-messages-per-poll="1">
|
||||
<int:interval-trigger interval="10000"/>
|
||||
</int:poller>
|
||||
</int:bridge>
|
||||
|
||||
<bean id="controlBus" class="org.springframework.integration.control.ControlBus">
|
||||
<constructor-arg ref="mbeanServer"/>
|
||||
</bean>
|
||||
|
||||
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
|
||||
<property name="locateExistingServerIfPossible" value="true"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -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.control;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
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.jmx.support.ObjectNameManager;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class ControlBusXmlTests {
|
||||
|
||||
private static final String DOMAIN = "org.springframework.integration";
|
||||
|
||||
|
||||
@Autowired
|
||||
private MBeanServer mbeanServer;
|
||||
|
||||
|
||||
@Test
|
||||
public void directChannelRegistered() throws Exception {
|
||||
ObjectInstance instance = mbeanServer.getObjectInstance(
|
||||
ObjectNameManager.getInstance(DOMAIN + ":type=channel,name=testDirectChannel"));
|
||||
assertEquals(DirectChannel.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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eventDrivenConsumerRegistered() throws Exception {
|
||||
ObjectInstance instance = mbeanServer.getObjectInstance(
|
||||
ObjectNameManager.getInstance(DOMAIN + ":type=endpoint,name=testEventDrivenBridge"));
|
||||
assertEquals(EventDrivenConsumer.class.getName(), instance.getClassName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pollingConsumerRegistered() throws Exception {
|
||||
ObjectInstance instance = mbeanServer.getObjectInstance(
|
||||
ObjectNameManager.getInstance(DOMAIN + ":type=endpoint,name=testPollingBridge"));
|
||||
assertEquals(PollingConsumer.class.getName(), instance.getClassName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.jmx;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
||||
import org.springframework.jmx.support.ObjectNameManager;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class AttributePollingMessageSourceTests {
|
||||
|
||||
private final TestCounter counter = new TestCounter();
|
||||
|
||||
private volatile MBeanServer server;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
MBeanServerFactoryBean factoryBean = new MBeanServerFactoryBean();
|
||||
factoryBean.setLocateExistingServerIfPossible(true);
|
||||
factoryBean.afterPropertiesSet();
|
||||
this.server = factoryBean.getObject();
|
||||
this.server.registerMBean(this.counter, ObjectNameManager.getInstance("test:name=counter"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void basicPolling() {
|
||||
AttributePollingMessageSource source = new AttributePollingMessageSource();
|
||||
source.setAttributeName("Count");
|
||||
source.setObjectName("test:name=counter");
|
||||
source.setServer(this.server);
|
||||
Message<?> message1 = source.receive();
|
||||
assertNotNull(message1);
|
||||
assertEquals(0, message1.getPayload());
|
||||
this.counter.increment();
|
||||
Message<?> message2 = source.receive();
|
||||
assertNotNull(message2);
|
||||
assertEquals(1, message2.getPayload());
|
||||
}
|
||||
|
||||
|
||||
public static interface TestCounterMBean {
|
||||
int getCount();
|
||||
}
|
||||
|
||||
|
||||
public static class TestCounter implements TestCounterMBean {
|
||||
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
|
||||
public int getCount() {
|
||||
return this.counter.get();
|
||||
}
|
||||
|
||||
public void increment() {
|
||||
this.counter.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* 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.jmx;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.Notification;
|
||||
import javax.management.NotificationFilter;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.jmx.export.MBeanExporter;
|
||||
import org.springframework.jmx.export.notification.NotificationPublisher;
|
||||
import org.springframework.jmx.export.notification.NotificationPublisherAware;
|
||||
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
||||
import org.springframework.jmx.support.ObjectNameManager;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class NotificationListeningMessageProducerTests {
|
||||
|
||||
private volatile MBeanServer server;
|
||||
|
||||
private volatile ObjectName objectName;
|
||||
|
||||
private final NumberHolder numberHolder = new NumberHolder();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
MBeanServerFactoryBean serverFactoryBean = new MBeanServerFactoryBean();
|
||||
serverFactoryBean.setLocateExistingServerIfPossible(true);
|
||||
serverFactoryBean.afterPropertiesSet();
|
||||
this.server = serverFactoryBean.getObject();
|
||||
MBeanExporter exporter = new MBeanExporter();
|
||||
exporter.setAutodetect(false);
|
||||
exporter.afterPropertiesSet();
|
||||
this.objectName = ObjectNameManager.getInstance("si:name=numberHolder");
|
||||
exporter.registerManagedResource(this.numberHolder, this.objectName);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() throws Exception {
|
||||
this.server.unregisterMBean(this.objectName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleNotification() throws Exception {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
NotificationListeningMessageProducer adapter = new NotificationListeningMessageProducer();
|
||||
adapter.setServer(this.server);
|
||||
adapter.setObjectName(this.objectName);
|
||||
adapter.setOutputChannel(outputChannel);
|
||||
adapter.afterPropertiesSet();
|
||||
adapter.start();
|
||||
this.numberHolder.publish("foo");
|
||||
Message<?> message = outputChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertTrue(message.getPayload() instanceof Notification);
|
||||
Notification notification = (Notification) message.getPayload();
|
||||
assertEquals("foo", notification.getMessage());
|
||||
assertEquals(objectName, notification.getSource());
|
||||
assertNull(message.getHeaders().get(JmxHeaders.NOTIFICATION_HANDBACK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notificationWithHandback() throws Exception {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
NotificationListeningMessageProducer adapter = new NotificationListeningMessageProducer();
|
||||
adapter.setServer(this.server);
|
||||
adapter.setObjectName(this.objectName);
|
||||
adapter.setOutputChannel(outputChannel);
|
||||
Integer handback = new Integer(123);
|
||||
adapter.setHandback(handback);
|
||||
adapter.afterPropertiesSet();
|
||||
adapter.start();
|
||||
this.numberHolder.publish("foo");
|
||||
Message<?> message = outputChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertTrue(message.getPayload() instanceof Notification);
|
||||
Notification notification = (Notification) message.getPayload();
|
||||
assertEquals("foo", notification.getMessage());
|
||||
assertEquals(objectName, notification.getSource());
|
||||
assertEquals(handback, message.getHeaders().get(JmxHeaders.NOTIFICATION_HANDBACK));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void notificationWithFilter() throws Exception {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
NotificationListeningMessageProducer adapter = new NotificationListeningMessageProducer();
|
||||
adapter.setServer(this.server);
|
||||
adapter.setObjectName(this.objectName);
|
||||
adapter.setOutputChannel(outputChannel);
|
||||
adapter.setFilter(new NotificationFilter() {
|
||||
public boolean isNotificationEnabled(Notification notification) {
|
||||
return !notification.getMessage().equals("bad");
|
||||
}
|
||||
});
|
||||
adapter.afterPropertiesSet();
|
||||
adapter.start();
|
||||
this.numberHolder.publish("bad");
|
||||
Message<?> message = outputChannel.receive(0);
|
||||
assertNull(message);
|
||||
this.numberHolder.publish("okay");
|
||||
message = outputChannel.receive(0);
|
||||
assertNotNull(message);
|
||||
assertTrue(message.getPayload() instanceof Notification);
|
||||
Notification notification = (Notification) message.getPayload();
|
||||
assertEquals("okay", notification.getMessage());
|
||||
}
|
||||
|
||||
|
||||
public static class NumberHolder implements NotificationPublisherAware {
|
||||
|
||||
private final AtomicInteger number = new AtomicInteger();
|
||||
|
||||
private final AtomicInteger sequence = new AtomicInteger();
|
||||
|
||||
private volatile NotificationPublisher notificationPublisher;
|
||||
|
||||
public int getNumber() {
|
||||
return this.number.get();
|
||||
}
|
||||
|
||||
public void setNumber(int value) {
|
||||
this.number.set(value);
|
||||
}
|
||||
|
||||
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
|
||||
this.notificationPublisher = notificationPublisher;
|
||||
}
|
||||
|
||||
public void publish(String message) {
|
||||
Notification notification = new Notification("testType", this, sequence.getAndIncrement(), message);
|
||||
this.notificationPublisher.sendNotification(notification);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.jmx;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.Notification;
|
||||
import javax.management.NotificationListener;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.jmx.export.MBeanExporter;
|
||||
import org.springframework.jmx.support.ObjectNameManager;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class NotificationPublishingMessageHandlerTests {
|
||||
|
||||
private final StaticApplicationContext context = new StaticApplicationContext();
|
||||
|
||||
private final TestNotificationListener listener = new TestNotificationListener();
|
||||
|
||||
private volatile ObjectName publisherObjectName;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.publisherObjectName = ObjectNameManager.getInstance("test:type=publisher");
|
||||
context.registerSingleton("exporter", MBeanExporter.class);
|
||||
RootBeanDefinition publisherDefinition = new RootBeanDefinition(NotificationPublishingMessageHandler.class);
|
||||
publisherDefinition.getConstructorArgumentValues().addGenericArgumentValue(this.publisherObjectName);
|
||||
publisherDefinition.getPropertyValues().add("defaultNotificationType", "test.type");
|
||||
context.registerBeanDefinition("testPublisher", publisherDefinition);
|
||||
context.refresh();
|
||||
MBeanExporter exporter = context.getBean(MBeanExporter.class);
|
||||
exporter.getServer().addNotificationListener(publisherObjectName, this.listener, null, null);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
this.listener.clearNotifications();
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void simplePublish() {
|
||||
NotificationPublishingMessageHandler adapter = context.getBean(NotificationPublishingMessageHandler.class);
|
||||
assertEquals(0, this.listener.notifications.size());
|
||||
adapter.handleMessage(new StringMessage("foo"));
|
||||
assertEquals(1, this.listener.notifications.size());
|
||||
Notification notification = this.listener.notifications.get(0);
|
||||
assertEquals(this.publisherObjectName, notification.getSource());
|
||||
assertEquals("foo", notification.getMessage());
|
||||
assertEquals("test.type", notification.getType());
|
||||
}
|
||||
|
||||
|
||||
public static class TestNotificationListener implements NotificationListener {
|
||||
|
||||
private final List<Notification> notifications = new ArrayList<Notification>();
|
||||
|
||||
public void handleNotification(Notification notification, Object handback) {
|
||||
this.notifications.add(notification);
|
||||
}
|
||||
|
||||
void clearNotifications() {
|
||||
this.notifications.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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.jmx;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessagingException;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
||||
import org.springframework.jmx.support.ObjectNameManager;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class OperationInvokingMessageHandlerTests {
|
||||
|
||||
private final String objectName = "si:name=test";
|
||||
|
||||
private volatile MBeanServer server;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
MBeanServerFactoryBean factoryBean = new MBeanServerFactoryBean();
|
||||
factoryBean.setLocateExistingServerIfPossible(true);
|
||||
factoryBean.afterPropertiesSet();
|
||||
this.server = factoryBean.getObject();
|
||||
this.server.registerMBean(new TestOps(), ObjectNameManager.getInstance(this.objectName));
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() throws Exception {
|
||||
this.server.unregisterMBean(ObjectNameManager.getInstance(this.objectName));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void invocationWithMapPayload() throws Exception {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler();
|
||||
handler.setServer(this.server);
|
||||
handler.setDefaultObjectName(this.objectName);
|
||||
handler.setOutputChannel(outputChannel);
|
||||
handler.afterPropertiesSet();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
params.put("p1", "foo");
|
||||
params.put("p2", "bar");
|
||||
Message<?> message = MessageBuilder.withPayload(params)
|
||||
.setHeader(JmxHeaders.OPERATION_NAME, "x").build();
|
||||
handler.handleMessage(message);
|
||||
Message<?> reply = outputChannel.receive(0);
|
||||
assertNotNull(reply);
|
||||
assertEquals("foobar", reply.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invocationWithPayloadNoReturnValue() throws Exception {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler();
|
||||
handler.setServer(this.server);
|
||||
handler.setDefaultObjectName(this.objectName);
|
||||
handler.setOutputChannel(outputChannel);
|
||||
handler.afterPropertiesSet();
|
||||
Message<?> message = MessageBuilder.withPayload("foo")
|
||||
.setHeader(JmxHeaders.OPERATION_NAME, "y").build();
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
|
||||
@Test(expected=MessagingException.class)
|
||||
public void invocationWithMapPayloadNotEnoughParameters() throws Exception {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler();
|
||||
handler.setServer(this.server);
|
||||
handler.setDefaultObjectName(this.objectName);
|
||||
handler.setOutputChannel(outputChannel);
|
||||
handler.afterPropertiesSet();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
params.put("p1", "foo");
|
||||
Message<?> message = MessageBuilder.withPayload(params)
|
||||
.setHeader(JmxHeaders.OPERATION_NAME, "x").build();
|
||||
handler.handleMessage(message);
|
||||
Message<?> reply = outputChannel.receive(0);
|
||||
assertNotNull(reply);
|
||||
assertEquals("foobar", reply.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invocationWithListPayload() throws Exception {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler();
|
||||
handler.setServer(this.server);
|
||||
handler.setDefaultObjectName(this.objectName);
|
||||
handler.setOutputChannel(outputChannel);
|
||||
handler.afterPropertiesSet();
|
||||
List<Object> params = Arrays.asList(new Object[] { "foo", new Integer(123) });
|
||||
Message<?> message = MessageBuilder.withPayload(params)
|
||||
.setHeader(JmxHeaders.OPERATION_NAME, "x").build();
|
||||
handler.handleMessage(message);
|
||||
Message<?> reply = outputChannel.receive(0);
|
||||
assertNotNull(reply);
|
||||
assertEquals("foo123", reply.getPayload());
|
||||
}
|
||||
|
||||
public static interface TestOpsMBean {
|
||||
|
||||
String x(String s1, String s2);
|
||||
|
||||
String x(String s, Integer i);
|
||||
|
||||
void y(String s);
|
||||
}
|
||||
|
||||
|
||||
public static class TestOps implements TestOpsMBean {
|
||||
|
||||
public String x(String s1, String s2) {
|
||||
return s1 + s2;
|
||||
}
|
||||
|
||||
public String x(String s, Integer i) {
|
||||
return s + i;
|
||||
}
|
||||
|
||||
public void y(String s){}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/jmx
|
||||
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
|
||||
|
||||
<context:mbean-export/>
|
||||
<context:mbean-server/>
|
||||
|
||||
<si:channel id="channel">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<jmx:attribute-polling-channel-adapter id="adapter"
|
||||
channel="channel"
|
||||
object-name="org.springframework.integration.jmx.config:type=TestBean,name=testBean1"
|
||||
attribute-name="FirstMessage"
|
||||
auto-startup="false">
|
||||
<si:poller max-messages-per-poll="1">
|
||||
<si:interval-trigger interval="2000"/>
|
||||
</si:poller>
|
||||
</jmx:attribute-polling-channel-adapter>
|
||||
|
||||
<bean id="testBean1" class="org.springframework.integration.jmx.config.TestBean"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class AttributePollingChannelAdapterParserTests {
|
||||
|
||||
@Autowired
|
||||
private PollableChannel channel;
|
||||
|
||||
@Autowired
|
||||
private SourcePollingChannelAdapter adapter;
|
||||
|
||||
@Autowired
|
||||
private TestBean testBean;
|
||||
|
||||
|
||||
@Test
|
||||
public void pollForAttribute() throws Exception {
|
||||
testBean.test("foo");
|
||||
adapter.start();
|
||||
Message<?> result = channel.receive(1000);
|
||||
assertNotNull(result);
|
||||
assertEquals("foo", result.getPayload());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/jmx
|
||||
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
|
||||
|
||||
<context:mbean-server id="mbs"/>
|
||||
|
||||
<si:channel id="testChannel"/>
|
||||
|
||||
<jmx:control-bus id="controlBus" mbean-server="mbs" operation-channel="testChannel" domain="tests.ControlBusParser"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.integration.control.ControlBus;
|
||||
import org.springframework.jmx.export.MBeanExporter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class ControlBusParserTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
ControlBus controlBus = this.context.getBean("controlBus", ControlBus.class);
|
||||
assertEquals(controlBus.getOperationChannel(), this.context.getBean("testChannel"));
|
||||
MBeanServer server = this.context.getBean("mbs", MBeanServer.class);
|
||||
MBeanExporter exporter = (MBeanExporter) new DirectFieldAccessor(controlBus).getPropertyValue("exporter");
|
||||
assertEquals(server, exporter.getServer());
|
||||
exporter.destroy();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/jmx
|
||||
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
|
||||
|
||||
<context:mbean-export/>
|
||||
<context:mbean-server/>
|
||||
|
||||
<si:channel id="channel">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<jmx:notification-listening-channel-adapter id="adapter"
|
||||
channel="channel"
|
||||
object-name="org.springframework.integration.jmx.config:type=TestPublisher,name=testPublisher"/>
|
||||
|
||||
<bean id="testPublisher" class="org.springframework.integration.jmx.config.TestPublisher"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import javax.management.Notification;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class NotificationListeningChannelAdapterParserTests {
|
||||
|
||||
@Autowired
|
||||
private PollableChannel channel;
|
||||
|
||||
@Autowired
|
||||
private TestPublisher testPublisher;
|
||||
|
||||
|
||||
@Test
|
||||
public void receiveNotification() throws Exception {
|
||||
assertNull(channel.receive(0));
|
||||
testPublisher.send("ABC");
|
||||
Message<?> message = channel.receive(1000);
|
||||
assertNotNull(message);
|
||||
assertEquals(Notification.class, message.getPayload().getClass());
|
||||
assertEquals("ABC", ((Notification) message.getPayload()).getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?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:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/jmx
|
||||
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
|
||||
|
||||
<context:mbean-export/>
|
||||
<context:mbean-server/>
|
||||
|
||||
<si:channel id="channel"/>
|
||||
|
||||
<jmx:notification-publishing-channel-adapter
|
||||
id="adapter" channel="channel"
|
||||
object-name="test.publisher:name=publisher"
|
||||
default-notification-type="default.type"/>
|
||||
|
||||
<bean id="testListener" class="org.springframework.integration.jmx.config.TestListener"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import javax.management.Notification;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.jmx.JmxHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class NotificationPublishingChannelAdapterParserTests {
|
||||
|
||||
@Autowired
|
||||
private MessageChannel channel;
|
||||
|
||||
@Autowired
|
||||
private TestListener listener;
|
||||
|
||||
@After
|
||||
public void clearListener() {
|
||||
listener.lastNotification = null;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void publishStringMessag() throws Exception {
|
||||
assertNull(listener.lastNotification);
|
||||
Message<?> message = MessageBuilder.withPayload("XYZ")
|
||||
.setHeader(JmxHeaders.NOTIFICATION_TYPE, "test.type").build();
|
||||
channel.send(message);
|
||||
assertNotNull(listener.lastNotification);
|
||||
Notification notification = listener.lastNotification;
|
||||
assertEquals("XYZ", notification.getMessage());
|
||||
assertEquals("test.type", notification.getType());
|
||||
assertNull(notification.getUserData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void publishUserData() throws Exception {
|
||||
assertNull(listener.lastNotification);
|
||||
TestData data = new TestData();
|
||||
Message<?> message = MessageBuilder.withPayload(data)
|
||||
.setHeader(JmxHeaders.NOTIFICATION_TYPE, "test.type").build();
|
||||
channel.send(message);
|
||||
assertNotNull(listener.lastNotification);
|
||||
Notification notification = listener.lastNotification;
|
||||
assertNull(notification.getMessage());
|
||||
assertEquals(data, notification.getUserData());
|
||||
assertEquals("test.type", notification.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultNotificationType() throws Exception {
|
||||
assertNull(listener.lastNotification);
|
||||
Message<?> message = MessageBuilder.withPayload("test").build();
|
||||
channel.send(message);
|
||||
assertNotNull(listener.lastNotification);
|
||||
Notification notification = listener.lastNotification;
|
||||
assertEquals("default.type", notification.getType());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static class TestData {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/jmx
|
||||
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
|
||||
|
||||
<context:mbean-export/>
|
||||
<context:mbean-server/>
|
||||
|
||||
<jmx:operation-invoking-channel-adapter id="withDefaultsChannel"
|
||||
default-object-name="org.springframework.integration.jmx.config:type=TestBean,name=testBeanForDefaults"
|
||||
default-operation-name="test"/>
|
||||
|
||||
<jmx:operation-invoking-channel-adapter id="withoutDefaultsChannel"/>
|
||||
|
||||
<bean id="testBeanForDefaults" class="org.springframework.integration.jmx.config.TestBean"/>
|
||||
|
||||
<bean id="testBeanForNoDefaults" class="org.springframework.integration.jmx.config.TestBean"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.jmx.JmxHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class OperationInvokingChannelAdapterParserTests {
|
||||
|
||||
@Autowired
|
||||
private MessageChannel withDefaultsChannel;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel withoutDefaultsChannel;
|
||||
|
||||
@Autowired
|
||||
private TestBean testBeanForDefaults;
|
||||
|
||||
@Autowired
|
||||
private TestBean testBeanForNoDefaults;
|
||||
|
||||
|
||||
@After
|
||||
public void resetLists() {
|
||||
testBeanForDefaults.messages.clear();
|
||||
testBeanForNoDefaults.messages.clear();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void adapterWithDefaults() throws Exception {
|
||||
assertEquals(0, testBeanForDefaults.messages.size());
|
||||
assertEquals(0, testBeanForNoDefaults.messages.size());
|
||||
withDefaultsChannel.send(new StringMessage("test1"));
|
||||
withDefaultsChannel.send(new StringMessage("test2"));
|
||||
withDefaultsChannel.send(new StringMessage("test3"));
|
||||
assertEquals(3, testBeanForDefaults.messages.size());
|
||||
assertEquals(0, testBeanForNoDefaults.messages.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adapterWithoutDefaults() throws Exception {
|
||||
assertEquals(0, testBeanForDefaults.messages.size());
|
||||
assertEquals(0, testBeanForNoDefaults.messages.size());
|
||||
withoutDefaultsChannel.send(createMessageWithHeaders("1"));
|
||||
withoutDefaultsChannel.send(createMessageWithHeaders("2"));
|
||||
withoutDefaultsChannel.send(createMessageWithHeaders("3"));
|
||||
assertEquals(0, testBeanForDefaults.messages.size());
|
||||
assertEquals(3, testBeanForNoDefaults.messages.size());
|
||||
}
|
||||
|
||||
|
||||
private static Message<String> createMessageWithHeaders(String payload) {
|
||||
String objectName = "org.springframework.integration.jmx.config:name=testBeanForNoDefaults,type=TestBean";
|
||||
return MessageBuilder.withPayload(payload)
|
||||
.setHeader(JmxHeaders.OBJECT_NAME, objectName)
|
||||
.setHeader(JmxHeaders.OPERATION_NAME, "test")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.jmx.export.annotation.ManagedOperation;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@ManagedResource
|
||||
public class TestBean {
|
||||
|
||||
final List<String> messages = new ArrayList<String>();
|
||||
|
||||
@ManagedAttribute
|
||||
public String getFirstMessage() {
|
||||
return (messages.size() > 0) ? messages.get(0) : null;
|
||||
}
|
||||
|
||||
@ManagedOperation
|
||||
public void test(String text) {
|
||||
this.messages.add(text);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.Notification;
|
||||
import javax.management.NotificationListener;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.jmx.support.ObjectNameManager;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class TestListener implements NotificationListener, BeanFactoryAware {
|
||||
|
||||
Notification lastNotification;
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
MBeanServer server = beanFactory.getBean("mbeanServer", MBeanServer.class);
|
||||
try {
|
||||
server.addNotificationListener(
|
||||
ObjectNameManager.getInstance("test.publisher:name=publisher"), this, null, null);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleNotification(Notification notification, Object handback) {
|
||||
this.lastNotification = notification;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.jmx.config;
|
||||
|
||||
import javax.management.Notification;
|
||||
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
import org.springframework.jmx.export.notification.NotificationPublisher;
|
||||
import org.springframework.jmx.export.notification.NotificationPublisherAware;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
@ManagedResource
|
||||
public class TestPublisher implements NotificationPublisherAware {
|
||||
|
||||
private volatile NotificationPublisher notificationPublisher;
|
||||
|
||||
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
|
||||
this.notificationPublisher = notificationPublisher;
|
||||
}
|
||||
|
||||
public void send(String s) {
|
||||
Notification notification = new Notification("test.type",
|
||||
"org.springframework.integration.jmx.config:type=TestPublisher,name=testPublisher", 1, s);
|
||||
this.notificationPublisher.sendNotification(notification);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user