INT-4257 Polishing - Subclasses: buildErrorMessage
JIRA: https://jira.spring.io/browse/INT-4257
So that subclasses can also generate error message for conditions such
as message conversion errors.
Polishing - PR Comments
Fix JMS buildErrorMessage Override
Update Copyrights
(cherry picked from commit c665405)
This commit is contained in:
committed by
Artem Bilan
parent
c91b20a20b
commit
089298e6be
@@ -28,6 +28,7 @@ import org.springframework.integration.support.management.TrackableComponent;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -187,17 +188,41 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
|
||||
this.messagingTemplate.send(getOutputChannel(), message);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
MessageChannel errorChannel = getErrorChannel();
|
||||
if (errorChannel != null) {
|
||||
this.messagingTemplate.send(errorChannel, this.errorMessageStrategy.buildErrorMessage(e,
|
||||
getErrorMessageAttributes(message)));
|
||||
}
|
||||
else {
|
||||
if (!sendErrorMessageIfNecessary(message, e)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an error message based on the exception and message.
|
||||
* @param message the message.
|
||||
* @param exception the exception.
|
||||
* @return true if the error channel is available and message sent.
|
||||
* @since 4.3.10
|
||||
*/
|
||||
protected final boolean sendErrorMessageIfNecessary(Message<?> message, RuntimeException exception) {
|
||||
MessageChannel errorChannel = getErrorChannel();
|
||||
if (errorChannel != null) {
|
||||
this.messagingTemplate.send(errorChannel, buildErrorMessage(message, exception));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an error message for the exception and message using the configured
|
||||
* {@link ErrorMessageStrategy}.
|
||||
* @param message the message.
|
||||
* @param exception the exception.
|
||||
* @return the error message.
|
||||
* @since 4.3.10
|
||||
*/
|
||||
protected final ErrorMessage buildErrorMessage(Message<?> message, RuntimeException exception) {
|
||||
return this.errorMessageStrategy.buildErrorMessage(exception,
|
||||
getErrorMessageAttributes(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate an {@link AttributeAccessor} to be used when building an error message
|
||||
* with the {@link #setErrorMessageStrategy(ErrorMessageStrategy)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -480,8 +480,7 @@ public abstract class MessagingGatewaySupport extends AbstractEndpoint
|
||||
if (error != null) {
|
||||
MessageChannel errorChannel = getErrorChannel();
|
||||
if (errorChannel != null) {
|
||||
Message<?> errorMessage = this.errorMessageStrategy.buildErrorMessage(error,
|
||||
getErrorMessageAttributes(requestMessage));
|
||||
ErrorMessage errorMessage = buildErrorMessage(requestMessage, error);
|
||||
Message<?> errorFlowReply = null;
|
||||
try {
|
||||
errorFlowReply = this.messagingTemplate.sendAndReceive(errorChannel, errorMessage);
|
||||
@@ -518,6 +517,20 @@ public abstract class MessagingGatewaySupport extends AbstractEndpoint
|
||||
return reply;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an error message for the message and throwable using the configured
|
||||
* {@link ErrorMessageStrategy}.
|
||||
* @param requestMessage the requestMessage.
|
||||
* @param throwable the throwable.
|
||||
* @return the error message.
|
||||
* @since 4.3.10
|
||||
*/
|
||||
protected final ErrorMessage buildErrorMessage(Message<?> requestMessage, Throwable throwable) {
|
||||
ErrorMessage errorMessage = this.errorMessageStrategy.buildErrorMessage(throwable,
|
||||
getErrorMessageAttributes(requestMessage));
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate an {@link AttributeAccessor} to be used when building an error message
|
||||
* with the {@link #setErrorMessageStrategy(ErrorMessageStrategy)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -330,7 +330,8 @@ public class ChannelPublishingJmsMessageListener
|
||||
if (errorChannel == null) {
|
||||
throw e;
|
||||
}
|
||||
errorChannel.send(new ErrorMessage(new MessagingException("Inbound conversion failed for: " + jmsMessage, e)));
|
||||
errorChannel.send(this.gatewayDelegate.buildErrorMessage(
|
||||
new MessagingException("Inbound conversion failed for: " + jmsMessage, e)));
|
||||
errors = true;
|
||||
}
|
||||
if (!errors) {
|
||||
@@ -509,6 +510,10 @@ public class ChannelPublishingJmsMessageListener
|
||||
return super.sendAndReceiveMessage(request);
|
||||
}
|
||||
|
||||
public ErrorMessage buildErrorMessage(Throwable throwable) {
|
||||
return super.buildErrorMessage(null, throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getComponentType() {
|
||||
if (ChannelPublishingJmsMessageListener.this.expectReply) {
|
||||
|
||||
Reference in New Issue
Block a user