IN PROGRESS - issue INT-633: BridgeHandler should not require an output channel

http://jira.springframework.org/browse/INT-633

Relaxed checking for output channel to allow REPLY_CHANNEL header usage
This commit is contained in:
Iwein Fuld
2009-06-07 12:26:52 +00:00
parent b49fc7c11e
commit 7e4e31afdf
2 changed files with 28 additions and 18 deletions

View File

@@ -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.
* <p/>
* 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 <tt>REPLY_CHANNEL</tt> 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);
}

View File

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