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).

This commit is contained in:
Mark Fisher
2008-10-16 16:39:19 +00:00
parent a8b2918680
commit f750be097f

View File

@@ -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 <code>true</code>.
*/
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() {