From 4d7a0bae5ad2b29eea224d0bd9931fd3064af71c Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 2 Jan 2008 23:18:36 +0000 Subject: [PATCH] Implemented RecipientListRouter - a convenience subclass of MultiChannelRouter for statically configuring lists of recipient channels (or channel names). --- .../router/RecipientListRouter.java | 72 +++++++++++ .../router/RecipientListRouterTests.java | 119 ++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/router/RecipientListRouter.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/router/RecipientListRouterTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/router/RecipientListRouter.java b/spring-integration-core/src/main/java/org/springframework/integration/router/RecipientListRouter.java new file mode 100644 index 0000000000..5f5753288a --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/router/RecipientListRouter.java @@ -0,0 +1,72 @@ +/* + * Copyright 2002-2007 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.router; + +import java.util.List; + +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.message.Message; + +/** + * A simple extension of {@link MultiChannelRouter} that routes to a statically + * configured list of recipients. The recipients are provided either as a list + * of {@link MessageChannel} instances or as a String array of channel names. + * For dynamic recipient lists, implement either {@link MultiChannelResolver} or + * {@link MultiChannelNameResolver} and then explicitly configure an instance of + * {@link MultiChannelRouter}. + * + * @author Mark Fisher + */ +public class RecipientListRouter extends MultiChannelRouter { + + public void setChannelNames(String[] channelNames) { + this.setChannelNameResolver(new RecipientListChannelNameResolver(channelNames)); + } + + public void setChannels(List channels) { + this.setChannelResolver(new RecipientListChannelResolver(channels)); + } + + + private static class RecipientListChannelResolver implements MultiChannelResolver { + + private List channels; + + RecipientListChannelResolver(List channels) { + this.channels = channels; + } + + public List resolve(Message message) { + return this.channels; + } + } + + + private static class RecipientListChannelNameResolver implements MultiChannelNameResolver { + + private String[] channelNames; + + RecipientListChannelNameResolver(String[] channelNames) { + this.channelNames = channelNames; + } + + public String[] resolve(Message message) { + return this.channelNames; + } + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/router/RecipientListRouterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/router/RecipientListRouterTests.java new file mode 100644 index 0000000000..e4d2745350 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/router/RecipientListRouterTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2002-2007 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.router; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import org.springframework.integration.MessagingConfigurationException; +import org.springframework.integration.channel.ChannelRegistry; +import org.springframework.integration.channel.DefaultChannelRegistry; +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.channel.PointToPointChannel; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.StringMessage; + +/** + * @author Mark Fisher + */ +public class RecipientListRouterTests { + + @Test + public void testRoutingWithChannelList() { + PointToPointChannel channel1 = new PointToPointChannel(); + PointToPointChannel channel2 = new PointToPointChannel(); + List channels = new ArrayList(); + channels.add(channel1); + channels.add(channel2); + RecipientListRouter router = new RecipientListRouter(); + router.setChannels(channels); + router.afterPropertiesSet(); + Message message = new StringMessage("123", "test"); + router.handle(message); + Message result1 = channel1.receive(25); + assertNotNull(result1); + assertEquals("test", result1.getPayload()); + Message result2 = channel2.receive(25); + assertNotNull(result2); + assertEquals("test", result2.getPayload()); + } + + @Test + public void testRoutingWithChannelNames() { + PointToPointChannel channel1 = new PointToPointChannel(); + PointToPointChannel channel2 = new PointToPointChannel(); + ChannelRegistry channelRegistry = new DefaultChannelRegistry(); + channelRegistry.registerChannel("channel1", channel1); + channelRegistry.registerChannel("channel2", channel2); + RecipientListRouter router = new RecipientListRouter(); + router.setChannelNames(new String[] {"channel1", "channel2"}); + router.setChannelRegistry(channelRegistry); + router.afterPropertiesSet(); + Message message = new StringMessage("123", "test"); + router.handle(message); + Message result1 = channel1.receive(25); + assertNotNull(result1); + assertEquals("test", result1.getPayload()); + Message result2 = channel2.receive(25); + assertNotNull(result2); + assertEquals("test", result2.getPayload()); + } + + @Test + public void testRoutingToSingleChannelByName() { + PointToPointChannel channel1 = new PointToPointChannel(); + PointToPointChannel channel2 = new PointToPointChannel(); + ChannelRegistry channelRegistry = new DefaultChannelRegistry(); + channelRegistry.registerChannel("channel1", channel1); + channelRegistry.registerChannel("channel2", channel2); + RecipientListRouter router = new RecipientListRouter(); + router.setChannelNames(new String[] {"channel1"}); + router.setChannelRegistry(channelRegistry); + router.afterPropertiesSet(); + Message message = new StringMessage("123", "test"); + router.handle(message); + Message result1 = channel1.receive(25); + assertNotNull(result1); + assertEquals("test", result1.getPayload()); + Message result2 = channel2.receive(5); + assertNull(result2); + } + + @Test(expected=MessagingConfigurationException.class) + public void testConfigurationExceptionWhenBothChannelsAndNamesAreProvided() { + PointToPointChannel channel1 = new PointToPointChannel(); + PointToPointChannel channel2 = new PointToPointChannel(); + List channels = new ArrayList(); + channels.add(channel1); + channels.add(channel2); + ChannelRegistry channelRegistry = new DefaultChannelRegistry(); + channelRegistry.registerChannel("channel1", channel1); + channelRegistry.registerChannel("channel2", channel2); + RecipientListRouter router = new RecipientListRouter(); + router.setChannels(channels); + router.setChannelNames(new String[] {"channel1"}); + router.setChannelRegistry(channelRegistry); + router.afterPropertiesSet(); + } + +}