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
This commit is contained in:
committed by
Artem Bilan
parent
1dc3726e7d
commit
c66540593c
@@ -28,6 +28,7 @@ import org.springframework.integration.support.management.TrackableComponent;
|
|||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
import org.springframework.messaging.MessageChannel;
|
import org.springframework.messaging.MessageChannel;
|
||||||
import org.springframework.messaging.MessagingException;
|
import org.springframework.messaging.MessagingException;
|
||||||
|
import org.springframework.messaging.support.ErrorMessage;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@@ -187,17 +188,41 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
|
|||||||
this.messagingTemplate.send(getOutputChannel(), message);
|
this.messagingTemplate.send(getOutputChannel(), message);
|
||||||
}
|
}
|
||||||
catch (RuntimeException e) {
|
catch (RuntimeException e) {
|
||||||
MessageChannel errorChannel = getErrorChannel();
|
if (!sendErrorMessageIfNecessary(message, e)) {
|
||||||
if (errorChannel != null) {
|
|
||||||
this.messagingTemplate.send(errorChannel, this.errorMessageStrategy.buildErrorMessage(e,
|
|
||||||
getErrorMessageAttributes(message)));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw 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
|
* Populate an {@link AttributeAccessor} to be used when building an error message
|
||||||
* with the {@link #setErrorMessageStrategy(ErrorMessageStrategy)
|
* 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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) {
|
if (error != null) {
|
||||||
MessageChannel errorChannel = getErrorChannel();
|
MessageChannel errorChannel = getErrorChannel();
|
||||||
if (errorChannel != null) {
|
if (errorChannel != null) {
|
||||||
Message<?> errorMessage = this.errorMessageStrategy.buildErrorMessage(error,
|
ErrorMessage errorMessage = buildErrorMessage(requestMessage, error);
|
||||||
getErrorMessageAttributes(requestMessage));
|
|
||||||
Message<?> errorFlowReply = null;
|
Message<?> errorFlowReply = null;
|
||||||
try {
|
try {
|
||||||
errorFlowReply = this.messagingTemplate.sendAndReceive(errorChannel, errorMessage);
|
errorFlowReply = this.messagingTemplate.sendAndReceive(errorChannel, errorMessage);
|
||||||
@@ -518,6 +517,20 @@ public abstract class MessagingGatewaySupport extends AbstractEndpoint
|
|||||||
return reply;
|
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
|
* Populate an {@link AttributeAccessor} to be used when building an error message
|
||||||
* with the {@link #setErrorMessageStrategy(ErrorMessageStrategy)
|
* 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -330,7 +330,8 @@ public class ChannelPublishingJmsMessageListener
|
|||||||
if (errorChannel == null) {
|
if (errorChannel == null) {
|
||||||
throw e;
|
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;
|
errors = true;
|
||||||
}
|
}
|
||||||
if (!errors) {
|
if (!errors) {
|
||||||
@@ -513,6 +514,10 @@ public class ChannelPublishingJmsMessageListener
|
|||||||
return super.sendAndReceiveMessage(request);
|
return super.sendAndReceiveMessage(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ErrorMessage buildErrorMessage(Throwable throwable) {
|
||||||
|
return super.buildErrorMessage(null, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getComponentType() {
|
public String getComponentType() {
|
||||||
if (ChannelPublishingJmsMessageListener.this.expectReply) {
|
if (ChannelPublishingJmsMessageListener.this.expectReply) {
|
||||||
|
|||||||
Reference in New Issue
Block a user