From f750be097fd0f58bfd43e907080146808b828c9f Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 16 Oct 2008 16:39:19 +0000 Subject: [PATCH] AbstractMessagingGateway now automatically throws the exception payload of a received ErrorMessage in send-and-receive operations is the 'shouldThrowErrors' value is set to true. It is true by default (related to INT-301). --- .../gateway/AbstractMessagingGateway.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java index 272bee7935..c8b6885389 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java @@ -23,12 +23,14 @@ import org.springframework.integration.channel.PollableChannel; import org.springframework.integration.channel.SubscribableChannel; import org.springframework.integration.core.Message; import org.springframework.integration.core.MessageChannel; +import org.springframework.integration.core.MessagingException; import org.springframework.integration.endpoint.AbstractReplyProducingMessageConsumer; import org.springframework.integration.endpoint.MessageEndpoint; import org.springframework.integration.endpoint.MessagingGateway; import org.springframework.integration.endpoint.PollingConsumerEndpoint; import org.springframework.integration.endpoint.ReplyMessageHolder; import org.springframework.integration.endpoint.SubscribingConsumerEndpoint; +import org.springframework.integration.message.ErrorMessage; import org.springframework.integration.message.MessageConsumer; import org.springframework.integration.message.MessageDeliveryException; import org.springframework.util.Assert; @@ -49,6 +51,8 @@ public abstract class AbstractMessagingGateway implements MessagingGateway, Mess private final MessageChannelTemplate channelTemplate = new MessageChannelTemplate(); + private volatile boolean shouldThrowErrors = true; + private volatile MessageEndpoint replyMessageCorrelator; private volatile MessageBus messageBus; @@ -95,6 +99,16 @@ public abstract class AbstractMessagingGateway implements MessagingGateway, Mess this.channelTemplate.setReceiveTimeout(replyTimeout); } + /** + * Specify whether the Throwable payload of a received {@link ErrorMessage} + * should be extracted and thrown from a send-and-receive operation. + * Otherwise, the ErrorMessage would be returned just like any other + * reply Message. The default is true. + */ + public void setShouldThrowErrors(boolean shouldThrowErrors) { + this.shouldThrowErrors = shouldThrowErrors; + } + public void setMessageBus(MessageBus messageBus) { this.messageBus = messageBus; } @@ -142,7 +156,15 @@ public abstract class AbstractMessagingGateway implements MessagingGateway, Mess if (this.replyChannel != null && this.replyMessageCorrelator == null) { this.registerReplyMessageCorrelator(); } - return this.channelTemplate.sendAndReceive(message, this.requestChannel); + Message reply = this.channelTemplate.sendAndReceive(message, this.requestChannel); + if (reply != null && this.shouldThrowErrors && reply instanceof ErrorMessage) { + Throwable error = ((ErrorMessage) reply).getPayload(); + if (error instanceof RuntimeException) { + throw (RuntimeException) error; + } + throw new MessagingException("gateway received checked Exception", error); + } + return reply; } private void registerReplyMessageCorrelator() {