From 9c55cdf5df6d1058fb68a43616d7dff2f9a624ca Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 27 Aug 2010 21:20:10 +0000 Subject: [PATCH] INT-1180 Added a 'requiresReply' header. The default value is FALSE, but Gateway request Messages now have this value set to true whenever the invoked method has a non-void return. This will trigger a MessageHandlingException instead of a timeout downstream if no reply is produced by an instance of AbstractReplyProducingMessageHandler. --- .../integration/MessageHeaders.java | 7 +++ .../integration/core/MessageBuilder.java | 4 ++ .../AbstractReplyProducingMessageHandler.java | 4 ++ .../handler/ArgumentArrayMessageMapper.java | 3 + .../GatewayRequiresReplyTests-context.xml | 17 ++++++ .../gateway/GatewayRequiresReplyTests.java | 60 +++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayRequiresReplyTests-context.xml create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayRequiresReplyTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/MessageHeaders.java b/spring-integration-core/src/main/java/org/springframework/integration/MessageHeaders.java index 7089a92e0a..293194872e 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/MessageHeaders.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/MessageHeaders.java @@ -58,6 +58,8 @@ public final class MessageHeaders implements Map, Serializable { public static final String CORRELATION_ID = PREFIX + "correlationId"; + public static final String REQUIRES_REPLY = PREFIX + "requiresReply"; + public static final String REPLY_CHANNEL = PREFIX + "replyChannel"; public static final String ERROR_CHANNEL = PREFIX + "errorChannel"; @@ -98,6 +100,11 @@ public final class MessageHeaders implements Map, Serializable { return this.get(REPLY_CHANNEL); } + public boolean getRequiresReply() { + Boolean headerValue = this.get(REQUIRES_REPLY, Boolean.class); + return (headerValue != null && headerValue); + } + public Object getErrorChannel() { return this.get(ERROR_CHANNEL); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/core/MessageBuilder.java b/spring-integration-core/src/main/java/org/springframework/integration/core/MessageBuilder.java index ce16d38c82..ebfe4aeae6 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/core/MessageBuilder.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/core/MessageBuilder.java @@ -167,6 +167,10 @@ public final class MessageBuilder { return this.setHeader(MessageHeaders.CORRELATION_ID, correlationId); } + public MessageBuilder setRequiresReply(Boolean requiresReply) { + return this.setHeader(MessageHeaders.REQUIRES_REPLY, requiresReply); + } + public MessageBuilder setReplyChannel(MessageChannel replyChannel) { return this.setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReplyProducingMessageHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReplyProducingMessageHandler.java index 4ca6ead28e..94620cc0af 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReplyProducingMessageHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReplyProducingMessageHandler.java @@ -109,6 +109,10 @@ public abstract class AbstractReplyProducingMessageHandler extends AbstractMessa throw new MessageHandlingException(message, "handler '" + this + "' requires a reply, but no reply was received"); } + if (message != null && message.getHeaders().getRequiresReply()) { + throw new MessageHandlingException(message, + "A reply Message is required by this request Message, but none was received."); + } if (logger.isDebugEnabled()) { logger.debug("handler '" + this + "' produced no reply for request Message: " + message); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/ArgumentArrayMessageMapper.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/ArgumentArrayMessageMapper.java index a3790daae6..20f18ea55e 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/ArgumentArrayMessageMapper.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/ArgumentArrayMessageMapper.java @@ -236,6 +236,9 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayRequiresReplyTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayRequiresReplyTests.java new file mode 100644 index 0000000000..81283e6cb3 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayRequiresReplyTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-2010 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.gateway; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.integration.MessageHandlingException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + * @since 2.0 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class GatewayRequiresReplyTests { + + @Autowired + private ApplicationContext applicationContext; + + + @Test + public void replyReceived() { + TestService gateway = (TestService) applicationContext.getBean("gateway"); + String result = gateway.test("foo"); + assertEquals("bar", result); + } + + @Test(expected = MessageHandlingException.class) + public void noReplyReceived() { + TestService gateway = (TestService) applicationContext.getBean("gateway"); + gateway.test("bad"); + } + + + public static interface TestService { + public String test(String s); + } + +}