diff --git a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java index c467ea1b1a..06bf8203d4 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java @@ -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) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java index cd46a876c6..7d4ca97a67 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java @@ -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) diff --git a/spring-integration-jms/src/main/java/org/springframework/integration/jms/ChannelPublishingJmsMessageListener.java b/spring-integration-jms/src/main/java/org/springframework/integration/jms/ChannelPublishingJmsMessageListener.java index a8b3f70d93..18da883d6a 100644 --- a/spring-integration-jms/src/main/java/org/springframework/integration/jms/ChannelPublishingJmsMessageListener.java +++ b/spring-integration-jms/src/main/java/org/springframework/integration/jms/ChannelPublishingJmsMessageListener.java @@ -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) { @@ -513,6 +514,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) {