From 6da90a4c51ce104adca10bafb69202c1f9cedc3f Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 26 Aug 2010 14:17:14 +0000 Subject: [PATCH] Sample and test for DynamicRouter with routing logic changed via the ConrolBus --- .../jmx/config/DynamicRouterTests-context.xml | 44 +++++++++++ .../jmx/config/DynamicRouterTests.java | 78 +++++++++++++++++++ .../jmx/config/SimpleDynamicRouter.java | 78 +++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml create mode 100644 spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests.java create mode 100644 spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/SimpleDynamicRouter.java diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml new file mode 100644 index 0000000000..a106451c7b --- /dev/null +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests.java new file mode 100644 index 0000000000..1f54746ce6 --- /dev/null +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests.java @@ -0,0 +1,78 @@ +/* + * 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.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.integration.Message; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.core.MessageBuilder; +import org.springframework.integration.core.MessageChannel; +import org.springframework.integration.core.StringMessage; +import org.springframework.integration.jmx.JmxHeaders; +import org.springframework.jmx.export.annotation.ManagedOperation; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.Assert; + +import static org.junit.Assert.assertEquals; + +/** + * @author Oleg Zhurakousky + * + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class DynamicRouterTests { + @Autowired + @Qualifier("controlChannel") + private MessageChannel controlChannel; + + @Autowired + @Qualifier("routingChannel") + private MessageChannel routingChannel; + + @Autowired + @Qualifier("processAChannel") + private QueueChannel processAChannel; + + @Autowired + @Qualifier("processBChannel") + private QueueChannel processBChannel; + + @Autowired + @Qualifier("processCChannel") + private QueueChannel processCChannel; + + @Test + public void testRouteChange() throws Exception { + routingChannel.send(new StringMessage("123")); + assertEquals("123", processAChannel.receive().getPayload()); + routingChannel.send(MessageBuilder.withPayload(123).build()); + assertEquals(123, processBChannel.receive().getPayload()); + + controlChannel.send(MessageBuilder.withPayload(new String[]{"java.lang.String", "processCChannel"}).build()); + + routingChannel.send(new StringMessage("123")); + assertEquals("123", processCChannel.receive().getPayload()); + } +} diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/SimpleDynamicRouter.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/SimpleDynamicRouter.java new file mode 100644 index 0000000000..33fdee5384 --- /dev/null +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/SimpleDynamicRouter.java @@ -0,0 +1,78 @@ +/* + * 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.HashMap; +import java.util.Map; + +import org.springframework.jmx.export.annotation.ManagedOperation; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.util.Assert; + +/** + * @author Oleg Zhurakousky + * + */ +/** + * + * + */ +@ManagedResource +public class SimpleDynamicRouter{ + private final Map channelMappings = new HashMap(); + /** + * + * @param channelMappings + */ + public SimpleDynamicRouter(Map channelMappings){ + Assert.notEmpty(channelMappings, "you must provide at least one channel mappings"); + for (String key : channelMappings.keySet()) { + this.channelMappings.put(key, channelMappings.get(key)); + } + } + /** + * + * @param key + * @param channelName + */ + @ManagedOperation + public void addChannelMapping(String key, String channelName){ + this.channelMappings.put(key, channelName); + } + /** + * + * @param key + */ + public void removeChannelMapping(String key){ + this.channelMappings.remove(key); + } + /** + * + * @return + */ + public Map getChannelMappings(){ + return channelMappings; + } + /** + * + * @param key + * @return + */ + public String route(Object key){ + String className = key.getClass().getName(); + return this.channelMappings.get(className); + } +}