Implemented RecipientListRouter - a convenience subclass of MultiChannelRouter for statically configuring lists of recipient channels (or channel names).

This commit is contained in:
Mark Fisher
2008-01-02 23:18:36 +00:00
parent 57bb42270a
commit 4d7a0bae5a
2 changed files with 191 additions and 0 deletions

View File

@@ -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<MessageChannel> channels) {
this.setChannelResolver(new RecipientListChannelResolver(channels));
}
private static class RecipientListChannelResolver implements MultiChannelResolver {
private List<MessageChannel> channels;
RecipientListChannelResolver(List<MessageChannel> channels) {
this.channels = channels;
}
public List<MessageChannel> 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;
}
}
}

View File

@@ -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<MessageChannel> channels = new ArrayList<MessageChannel>();
channels.add(channel1);
channels.add(channel2);
RecipientListRouter router = new RecipientListRouter();
router.setChannels(channels);
router.afterPropertiesSet();
Message<String> 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<String> 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<String> 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<MessageChannel> channels = new ArrayList<MessageChannel>();
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();
}
}