Refactor/Standardize Router XSD Schema Attributes

- Add more documentation for schema elements in spring-integration-xml-2.1.xsd
  - Simplify Handling of Channel Resolution (Failures) for Routers
  - Fix PayloadTypeRouterTests
  - Update reference docs, XSD Schemas
  - Rename 'channel-resolution-required' to 'resolution-required'
  - Updated all tests accordingly
This commit is contained in:
Gunnar Hillert
2011-09-06 14:39:38 -04:00
committed by Mark Fisher
parent b55520369d
commit 2c023f6fd1
16 changed files with 1399 additions and 614 deletions

View File

@@ -45,8 +45,6 @@ public class RouterFactoryBean extends AbstractStandardMessageHandlerFactoryBean
private volatile Boolean resolutionRequired;
private volatile Boolean ignoreChannelNameResolutionFailures;
private volatile Boolean applySequence;
private volatile Boolean ignoreSendFailures;
@@ -68,10 +66,6 @@ public class RouterFactoryBean extends AbstractStandardMessageHandlerFactoryBean
this.resolutionRequired = resolutionRequired;
}
public void setIgnoreChannelNameResolutionFailures(Boolean ignoreChannelNameResolutionFailures) {
this.ignoreChannelNameResolutionFailures = ignoreChannelNameResolutionFailures;
}
public void setApplySequence(Boolean applySequence) {
this.applySequence = applySequence;
}
@@ -128,9 +122,6 @@ public class RouterFactoryBean extends AbstractStandardMessageHandlerFactoryBean
if (this.timeout != null) {
router.setTimeout(timeout.longValue());
}
if (this.ignoreChannelNameResolutionFailures != null) {
router.setIgnoreChannelNameResolutionFailures(ignoreChannelNameResolutionFailures);
}
if (this.applySequence != null) {
router.setApplySequence(this.applySequence);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -44,7 +44,6 @@ public abstract class AbstractRouterParser extends AbstractConsumerEndpointParse
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "resolution-required");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "apply-sequence");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-send-failures");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-channel-name-resolution-failures");
BeanDefinition targetRouterBeanDefinition = this.parseRouter(element, parserContext);
builder.addPropertyValue("targetObject", targetRouterBeanDefinition);
return builder;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -78,7 +78,6 @@ public class DefaultRouterParser extends AbstractDelegatingConsumerEndpointParse
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "default-output-channel");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "timeout");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "resolution-required");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-channel-name-resolution-failures");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "apply-sequence");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-send-failures");
}

View File

@@ -30,6 +30,7 @@ import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageDeliveryException;
import org.springframework.integration.MessagingException;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.support.MessageBuilder;
@@ -47,14 +48,13 @@ import org.springframework.util.StringUtils;
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Gunnar Hillert
*/
@ManagedResource
public abstract class AbstractMessageRouter extends AbstractMessageHandler {
private volatile MessageChannel defaultOutputChannel;
private volatile boolean resolutionRequired;
private volatile boolean ignoreSendFailures;
private volatile boolean applySequence;
@@ -67,7 +67,7 @@ public abstract class AbstractMessageRouter extends AbstractMessageHandler {
private volatile ChannelResolver channelResolver;
private volatile boolean ignoreChannelNameResolutionFailures;
private volatile boolean resolutionRequired = true;
protected volatile Map<String, String> channelIdentifierMap = new ConcurrentHashMap<String, String>();
@@ -120,9 +120,12 @@ public abstract class AbstractMessageRouter extends AbstractMessageHandler {
}
/**
* Set the default channel where Messages should be sent if channel resolution fails to return any channels. If no
* default channel is provided, the router will either drop the Message or throw an Exception depending on the value
* of {@link #resolutionRequired}.
* Set the default channel where Messages should be sent if channel resolution
* fails to return any channels. If no default channel is provided and channel
* resolution fails to return any channels, the router will throw an
* {@link MessageDeliveryException}.
*
* If messages shall be ignored (dropped) instead, please provide a {@link NullChannel}.
*/
public void setDefaultOutputChannel(MessageChannel defaultOutputChannel) {
this.defaultOutputChannel = defaultOutputChannel;
@@ -136,21 +139,12 @@ public abstract class AbstractMessageRouter extends AbstractMessageHandler {
this.messagingTemplate.setSendTimeout(timeout);
}
/**
* Set whether this router should always be required to resolve at least one channel. The default is 'false'. To
* trigger an exception whenever the resolver returns null or an empty channel list, and this endpoint has no
* 'defaultOutputChannel' configured, set this value to 'true'.
*/
public void setResolutionRequired(boolean resolutionRequired) {
this.resolutionRequired = resolutionRequired;
}
/**
* Specify whether this router should ignore any failure to resolve a channel name to
* an actual MessageChannel instance when delegating to the ChannelResolver strategy.
*/
public void setIgnoreChannelNameResolutionFailures(boolean ignoreChannelNameResolutionFailures) {
this.ignoreChannelNameResolutionFailures = ignoreChannelNameResolutionFailures;
public void setResolutionRequired(boolean resolutionRequired) {
this.resolutionRequired = resolutionRequired;
}
/**
@@ -234,8 +228,7 @@ public abstract class AbstractMessageRouter extends AbstractMessageHandler {
if (!sent) {
if (this.defaultOutputChannel != null) {
this.messagingTemplate.send(this.defaultOutputChannel, message);
}
else if (this.resolutionRequired) {
} else {
throw new MessageDeliveryException(message,
"no channel resolved by router and no default output channel defined");
}
@@ -260,12 +253,12 @@ public abstract class AbstractMessageRouter extends AbstractMessageHandler {
channel = this.channelResolver.resolveChannelName(channelName);
}
catch (ChannelResolutionException e) {
if (!this.ignoreChannelNameResolutionFailures) {
if (this.resolutionRequired) {
throw new MessagingException(message,
"failed to resolve channel name '" + channelName + "'", e);
}
}
if (channel == null && !this.ignoreChannelNameResolutionFailures) {
if (channel == null && this.resolutionRequired) {
throw new MessagingException(message,
"failed to resolve channel name '" + channelName + "'");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -19,6 +19,7 @@ package org.springframework.integration.router;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -28,6 +29,7 @@ import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageDeliveryException;
import org.springframework.integration.MessagingException;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.channel.QueueChannel;
@@ -134,7 +136,15 @@ public class MethodInvokingRouterTests {
Message<?> result2 = barChannel.receive(0);
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -171,7 +181,14 @@ public class MethodInvokingRouterTests {
Message<?> result2 = barChannel.receive(0);
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -208,7 +225,14 @@ public class MethodInvokingRouterTests {
Message<?> result2 = barChannel.receive(0);
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -251,7 +275,13 @@ public class MethodInvokingRouterTests {
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -294,7 +324,13 @@ public class MethodInvokingRouterTests {
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -337,7 +373,13 @@ public class MethodInvokingRouterTests {
Message<?> result2b = barChannel.receive(0);
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -380,7 +422,14 @@ public class MethodInvokingRouterTests {
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -423,7 +472,14 @@ public class MethodInvokingRouterTests {
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}
@Test
@@ -466,7 +522,14 @@ public class MethodInvokingRouterTests {
assertEquals("bar", result2a.getPayload());
assertNotNull(result2b);
assertEquals("bar", result2b.getPayload());
router.handleMessage(badMessage);
try {
router.handleMessage(badMessage);
fail();
} catch (MessageDeliveryException e) {
/* Success */
}
}

View File

@@ -67,14 +67,15 @@ public class PayloadTypeRouterTests {
router.setChannelMapping(String.class.getName(), "newChannel");
assertEquals(1, router.getChannelIdentifiers(message1).size());
assertEquals("newChannel", router.getChannelIdentifiers(message1).iterator().next());
// validate exception is thrown if mappings were removed and
// channelResolutionRequires = true (which is the default)
// validate nothing happens if mappings were removed and resolutionRequires = false
router.removeChannelMapping(String.class.getName());
router.removeChannelMapping(Integer.class.getName());
router.handleMessage(message1);
// validate exception is thrown if mappings were removed and resolutionRequires = true
router.setResolutionRequired(true);
try {
router.handleMessage(message1);
fail();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -27,6 +27,7 @@ import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageDeliveryException;
import org.springframework.integration.MessagingException;
import org.springframework.integration.channel.QueueChannel;
@@ -37,11 +38,12 @@ import org.springframework.util.CollectionUtils;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Gunnar Hillert
*/
public class RouterTests {
@Test
public void nullChannelIgnoredByDefault() {
@Test(expected = MessageDeliveryException.class)
public void nullChannelRaisesMessageDeliveryExceptionByDefault() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@Override
protected List<Object> getChannelIdentifiers(Message<?> message) {
@@ -53,45 +55,7 @@ public class RouterTests {
}
@Test(expected = MessageDeliveryException.class)
public void nullChannelThrowsExceptionWhenResolutionRequired() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@Override
protected List<Object> getChannelIdentifiers(Message<?> message) {
return null;
}
};
router.setResolutionRequired(true);
Message<String> message = new GenericMessage<String>("test");
router.handleMessage(message);
}
@Test
public void emptyChannelListIgnoredByDefault() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@Override
protected List<Object> getChannelIdentifiers(Message<?> message) {
return null;
}
};
Message<String> message = new GenericMessage<String>("test");
router.handleMessage(message);
}
@Test(expected = MessageDeliveryException.class)
public void emptyChannelListThrowsExceptionWhenResolutionRequired() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@Override
protected List<Object> getChannelIdentifiers(Message<?> message) {
return null;
}
};
router.setResolutionRequired(true);
Message<String> message = new GenericMessage<String>("test");
router.handleMessage(message);
}
@Test
public void nullChannelIdentifierIgnoredByDefault() {
public void nullChannelIdentifierUsingChannelResolverRaisesMessageDeliveryExceptionByDefault() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@Override
protected List<Object> getChannelIdentifiers(Message<?> message) {
@@ -104,8 +68,8 @@ public class RouterTests {
router.handleMessage(message);
}
@Test
public void nullChannelIdentifierInListIgnoredByDefault() {
@Test(expected = MessageDeliveryException.class)
public void nullChannelIdentifierInListRaisesMessageDeliveryExceptionByDefault() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@Override
protected List<Object> getChannelIdentifiers(Message<?> message) {
@@ -119,35 +83,7 @@ public class RouterTests {
}
@Test(expected = MessageDeliveryException.class)
public void nullChannelIdentifierTriggersExceptionWhenResolutionRequired() {
AbstractMessageRouter router = new AbstractMessageRouter() {
protected List<Object> getChannelIdentifiers(Message<?> message) {
return null;
}
};
TestChannelResolver channelResolver = new TestChannelResolver();
router.setChannelResolver(channelResolver);
router.setResolutionRequired(true);
Message<String> message = new GenericMessage<String>("test");
router.handleMessage(message);
}
@Test(expected = MessageDeliveryException.class)
public void nullChannelIdentifierInListTriggersExceptionWhenResolutionRequired() {
AbstractMessageRouter router = new AbstractMessageRouter() {
protected List<Object> getChannelIdentifiers(Message<?> message) {
return Collections.singletonList(null);
}
};
TestChannelResolver channelResolver = new TestChannelResolver();
router.setChannelResolver(channelResolver);
router.setResolutionRequired(true);
Message<String> message = new GenericMessage<String>("test");
router.handleMessage(message);
}
@Test
public void emptyChannelNameArrayIgnoredByDefault() {
public void emptyChannelNameArrayRaisesMessageDeliveryExceptionByDefault() {
AbstractMessageRouter router = new AbstractMessageRouter() {
protected List<Object> getChannelIdentifiers(Message<?> message) {
return new ArrayList<Object>();
@@ -159,21 +95,6 @@ public class RouterTests {
router.handleMessage(message);
}
@Test(expected = MessageDeliveryException.class)
public void emptyChannelNameArrayThrowsExceptionWhenResolutionRequired() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] {});
}
};
TestChannelResolver channelResolver = new TestChannelResolver();
router.setChannelResolver(channelResolver);
router.setResolutionRequired(true);
Message<String> message = new GenericMessage<String>("test");
router.handleMessage(message);
}
@Test(expected = MessagingException.class)
public void channelMappingIsRequiredWhenResolvingChannelNames() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@@ -203,4 +124,282 @@ public class RouterTests {
assertEquals("test", reply.getPayload());
}
@Test
public void beanFactoryWithRouterAndMultipleCommaSeparatedChannelNames() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] { "testChannel1,testChannel2" });
}
};
QueueChannel testChannel1 = new QueueChannel();
QueueChannel testChannel2 = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("testChannel1", testChannel1);
context.getBeanFactory().registerSingleton("testChannel2", testChannel2);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
Message<?> reply1 = testChannel1.receive(0);
assertEquals("test", reply1.getPayload());
Message<?> reply2 = testChannel2.receive(0);
assertEquals("test", reply2.getPayload());
}
@Test(expected = MessagingException.class)
public void channelResolutionIsRequiredByDefault() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] { "testChannelDoesNotExist", "testChannel" });
}
};
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("testChannel", testChannel);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
}
@Test
public void unresolvableChannelIdentifierInListAreIgnoredWhenResolutionRequiredIsFalse() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] { "testChannelDoesNotExist", "testChannel" });
}
};
router.setResolutionRequired(false);
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("testChannel", testChannel);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
Message<?> reply = testChannel.receive(0);
assertEquals("test", reply.getPayload());
}
@Test
public void beanFactoryWithRouterAndChannelPrefix() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] { "MyChannel" });
}
};
router.setPrefix("testing_");
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("testing_MyChannel", testChannel);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
Message<?> reply = testChannel.receive(0);
assertEquals("test", reply.getPayload());
}
@Test(expected = MessagingException.class)
public void beanFactoryWithRouterAndChannelPrefixFailing() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] { "testing_MyChannel" });
}
};
router.setPrefix("testing_");
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("testing_MyChannel", testChannel);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
}
@Test
public void beanFactoryWithRouterAndChannelSuffix() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] { "MyChannel" });
}
};
router.setSuffix("_withSuffix");
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("MyChannel_withSuffix", testChannel);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
Message<?> reply = testChannel.receive(0);
assertEquals("test", reply.getPayload());
}
@Test(expected = MessagingException.class)
public void beanFactoryWithRouterAndChannelSuffixFailing() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new String[] { "MyChannel_withSuffix" });
}
};
router.setSuffix("_withSuffix");
QueueChannel testChannel = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("MyChannel_withSuffix", testChannel);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
}
@Test
public void beanFactoryWithRouterAndChannelIdentifiersInListWithinAList() {
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
List<String> channelNames1 = CollectionUtils.arrayToList(new String[] { "channel1" });
List<String> channelNames2 = CollectionUtils.arrayToList(new String[] { "channel2" });
List<Object> listWithListOfChannelNames = new ArrayList<Object>();
listWithListOfChannelNames.add(channelNames1);
listWithListOfChannelNames.add(channelNames2);
return listWithListOfChannelNames;
}
};
QueueChannel testChannel1 = new QueueChannel();
QueueChannel testChannel2 = new QueueChannel();
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("channel1", testChannel1);
context.getBeanFactory().registerSingleton("channel2", testChannel2);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
Message<?> reply1 = testChannel1.receive(0);
assertEquals("test", reply1.getPayload());
Message<?> reply2 = testChannel2.receive(0);
assertEquals("test", reply2.getPayload());
}
@Test
public void beanFactoryWithRouterAndChannelIdentifiersInMessageChannelArrayWithinAList() {
final QueueChannel testChannel1 = new QueueChannel();
final QueueChannel testChannel2 = new QueueChannel();
AbstractMessageRouter router = new AbstractMessageRouter() {
protected List<Object> getChannelIdentifiers(Message<?> message) {
MessageChannel[] channelNames1 = new MessageChannel[] { testChannel1 };
MessageChannel[] channelNames2 = new MessageChannel[] { testChannel2 };
List<Object> listWithListOfChannelNames = new ArrayList<Object>();
listWithListOfChannelNames.add(channelNames1);
listWithListOfChannelNames.add(channelNames2);
return listWithListOfChannelNames;
}
};
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("channel1", testChannel1);
context.getBeanFactory().registerSingleton("channel2", testChannel2);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
Message<?> reply1 = testChannel1.receive(0);
assertEquals("test", reply1.getPayload());
Message<?> reply2 = testChannel2.receive(0);
assertEquals("test", reply2.getPayload());
}
@Test
public void beanFactoryWithRouterAndRetrieveChannelIdentifiersUsingDefaultConversionService() {
final QueueChannel testChannel1 = new QueueChannel();
final QueueChannel testChannel2 = new QueueChannel();
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new Integer[] { 100, 200 });
}
};
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("100", testChannel1);
context.getBeanFactory().registerSingleton("200", testChannel2);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
Message<?> reply1 = testChannel1.receive(0);
assertEquals("test", reply1.getPayload());
Message<?> reply2 = testChannel2.receive(0);
assertEquals("test", reply2.getPayload());
}
private class CustomObjectWithChannelName {
String channel = "channel1";
public String getChannel() {
return this.channel;
}
}
@Test(expected = MessagingException.class)
public void beanFactoryWithRouterAndRetrieveChannelIdentifierUsingDefaultConversionServiceFailing() {
final QueueChannel testChannel1 = new QueueChannel();
AbstractMessageRouter router = new AbstractMessageRouter() {
@SuppressWarnings("unchecked")
protected List<Object> getChannelIdentifiers(Message<?> message) {
return CollectionUtils.arrayToList(new CustomObjectWithChannelName[] { new CustomObjectWithChannelName() });
}
};
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("channel1", testChannel1);
router.setBeanFactory(context);
router.handleMessage(new GenericMessage<String>("test"));
}
}

View File

@@ -3,10 +3,10 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/integration/spring-integration.xsd">
<int:router input-channel="inputChannel" default-output-channel="errorChannel" ignore-channel-name-resolution-failures="true" >
<int:router input-channel="inputChannel" default-output-channel="errorChannel" resolution-required="false">
<bean class="org.springframework.integration.router.config.RouterParserTests.NonExistingChannelRouter"/>
</int:router>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -49,6 +49,7 @@ import org.springframework.integration.test.util.TestUtils;
/**
* @author Mark Fisher
* @author Jonas Partner
* @author Gunnar Hillert
*/
public class RouterParserTests {
@@ -121,12 +122,11 @@ public class RouterParserTests {
input.send(new GenericMessage<Integer>(3));
}
@Test
public void testIgnoreChannelNameResolutionFailures() {
public void testResolutionRequiredIsTrue() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"routerParserTests.xml", this.getClass());
context.start();
MessageChannel input = (MessageChannel) context.getBean("ignoreChannelNameResolutionFailuresInput");
MessageChannel input = (MessageChannel) context.getBean("resolutionRequiredIsTrueInput");
input.send(new GenericMessage<String>("channelThatDoesNotExist"));
}
@@ -177,7 +177,7 @@ public class RouterParserTests {
assertEquals(new Integer(3), message3.getHeaders().getSequenceNumber());
assertEquals(new Integer(3), message3.getHeaders().getSequenceSize());
}
@Test
public void testErrorChannel(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

View File

@@ -21,7 +21,7 @@
<router id="spelRouter" input-channel="expressionRouter" expression="payload.name"
default-output-channel="defaultChannelForExpression"
ignore-channel-name-resolution-failures="true">
resolution-required="false">
<mapping value="foo" channel="fooChannelForExpression"/>
<mapping value="bar" channel="barChannelForExpression"/>
</router>
@@ -40,7 +40,7 @@
<router input-channel="pojoRouter" ref="testBean"
default-output-channel="defaultChannelForPojo"
ignore-channel-name-resolution-failures="true">
resolution-required="false">
<mapping value="foo" channel="fooChannelForPojo"/>
<mapping value="bar" channel="barChannelForPojo"/>
</router>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -69,7 +69,6 @@ public class RouterWithMappingTests {
@Autowired
private PollableChannel defaultChannelForPojo;
@Test
public void expressionRouter() {
Message<?> message1 = MessageBuilder.withPayload(new TestBean("foo")).build();
@@ -115,7 +114,6 @@ public class RouterWithMappingTests {
assertNull(barChannelForPojo.receive(0));
}
private static class TestBean {
private final String name;

View File

@@ -73,9 +73,9 @@
<router id="resolutionRequiredRouter" ref="pojo" method="route" input-channel="inputForRouterRequiringResolution" resolution-required="true" />
<channel id="ignoreChannelNameResolutionFailuresInput"/>
<channel id="resolutionRequiredIsTrueInput"/>
<beans:bean id="payloadAsChannelNameRouter" class="org.springframework.integration.router.config.RouterParserTests$ReturnStringPassedInAsChannelNameRouter" />
<router id="ignoreChannelNameResolutionFailuresRouter" ref="payloadAsChannelNameRouter" input-channel="ignoreChannelNameResolutionFailuresInput" ignore-channel-name-resolution-failures="true" />
<router id="resolutionRequiredFalseRouter" ref="payloadAsChannelNameRouter" input-channel="resolutionRequiredIsTrueInput" resolution-required="false" />
<channel id="timeoutRouterChannel"/>
<router id="routerWithTimeout" ref="payloadAsChannelNameRouter" timeout="1234" input-channel="timeoutRouterChannel"/>