diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/handler/BridgeHandler.java b/org.springframework.integration/src/main/java/org/springframework/integration/handler/BridgeHandler.java index f2d6d12a89..1b0e01ceb0 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/handler/BridgeHandler.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/handler/BridgeHandler.java @@ -16,27 +16,30 @@ package org.springframework.integration.handler; -import org.springframework.beans.factory.InitializingBean; import org.springframework.integration.core.Message; import org.springframework.util.Assert; /** * A simple MessageHandler implementation that passes the request Message - * directly to the output channel without modifying it. The main purpose of - * this handler is to bridge a PollableChannel to a SubscribableChannel or + * directly to the output channel without modifying it. The main purpose of this + * handler is to bridge a PollableChannel to a SubscribableChannel or * vice-versa. + *

+ * The BridgeHandler can be used as a stopper at the end of an assembly line of + * channels. In this setup the output channel doesn't have to be set, but if the + * output channel is omitted the REPLY_CHANNEL MUST be set on the + * message. * * @author Mark Fisher + * @author Iwein Fuld */ -public class BridgeHandler extends AbstractReplyProducingMessageHandler implements InitializingBean { - - public void afterPropertiesSet() { - this.verifyOutputChannel(); - } +public class BridgeHandler extends AbstractReplyProducingMessageHandler { @Override protected void handleRequestMessage(Message requestMessage, ReplyMessageHolder replyMessageHolder) { - this.verifyOutputChannel(); + if (requestMessage.getHeaders().getReplyChannel() == null) { + this.verifyOutputChannel(); + } replyMessageHolder.set(requestMessage); } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java index cb5908bb9a..e70bdc0165 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/handler/BridgeHandlerTests.java @@ -16,25 +16,30 @@ package org.springframework.integration.handler; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import org.junit.Test; - +import org.springframework.integration.channel.PollableChannel; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.core.Message; +import org.springframework.integration.message.MessageBuilder; import org.springframework.integration.message.MessageHandlingException; import org.springframework.integration.message.StringMessage; /** * @author Mark Fisher + * @author Iwein Fuld */ public class BridgeHandlerTests { + private BridgeHandler handler= new BridgeHandler(); + @Test public void simpleBridge() { QueueChannel outputChannel = new QueueChannel(); - BridgeHandler handler = new BridgeHandler(); handler.setOutputChannel(outputChannel); Message request = new StringMessage("test"); handler.handleMessage(request); @@ -43,17 +48,19 @@ public class BridgeHandlerTests { assertEquals(request, reply); } - @Test(expected = IllegalStateException.class) - public void missingOutputChannelVerifiedUponInitialization() { - BridgeHandler handler = new BridgeHandler(); - handler.afterPropertiesSet(); - } - @Test(expected = MessageHandlingException.class) public void missingOutputChannelVerifiedAtRuntime() { - BridgeHandler handler = new BridgeHandler(); Message request = new StringMessage("test"); handler.handleMessage(request); } + + @SuppressWarnings("unchecked") + @Test(timeout=1000) + public void missingOutputChannelAllowedForReplyChannelMessages() throws Exception { + PollableChannel replyChannel = new QueueChannel(); + Message request = MessageBuilder.withPayload("tst").setReplyChannel(replyChannel ).build(); + handler.handleMessage(request ); + assertThat(replyChannel.receive(), is(request)); + } }