Sample and test for DynamicRouter with routing logic changed via the ConrolBus

This commit is contained in:
Oleg Zhurakousky
2010-08-26 14:17:14 +00:00
parent c6835aa207
commit 6da90a4c51
3 changed files with 200 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx-2.0.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jmx="http://www.springframework.org/schema/integration/jmx">
<context:mbean-export />
<context:mbean-server />
<int-jmx:operation-invoking-channel-adapter
id="controlChannel"
object-name="org.springframework.integration.jmx.config:type=SimpleDynamicRouter,name=dynamicRouter"
operation-name="addChannelMapping" />
<int:router input-channel="routingChannel" ref="dynamicRouter" method="route"
default-output-channel="errorChannel"
ignore-channel-name-resolution-failures="true"/>
<bean id="dynamicRouter"
class="org.springframework.integration.jmx.config.SimpleDynamicRouter">
<constructor-arg>
<map>
<entry key="java.lang.String" value="processAChannel" />
<entry key="java.lang.Integer" value="processBChannel" />
</map>
</constructor-arg>
</bean>
<int:channel id="processAChannel">
<int:queue/>
</int:channel>
<int:channel id="processBChannel">
<int:queue/>
</int:channel>
<int:channel id="processCChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -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());
}
}

View File

@@ -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<String, String> channelMappings = new HashMap<String, String>();
/**
*
* @param channelMappings
*/
public SimpleDynamicRouter(Map<String, String> 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<String, String> getChannelMappings(){
return channelMappings;
}
/**
*
* @param key
* @return
*/
public String route(Object key){
String className = key.getClass().getName();
return this.channelMappings.get(className);
}
}