From 089298e6bebcbfe2c369ec68fa83673ebf0a53fa Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 24 Apr 2017 11:20:08 -0400 Subject: [PATCH] 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) --- .../endpoint/MessageProducerSupport.java | 37 ++++++++++++++++--- .../gateway/MessagingGatewaySupport.java | 19 ++++++++-- .../ChannelPublishingJmsMessageListener.java | 9 ++++- 3 files changed, 54 insertions(+), 11 deletions(-) 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 1a3ab775cf..34efb38d36 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 5bbb17cacc..2bd30b11f3 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) { @@ -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) {