INT-4203: Expression RH Advice Improvements

JIRA: https://jira.spring.io/browse/INT-4203

Add channel names for easier use in DSL.

Add documentation (boot) example.

Normalize Expression Setters; add Javadocs

Polishing - SPR-15091

Tiny code style polishing
This commit is contained in:
Gary Russell
2017-01-03 18:35:34 -05:00
committed by Artem Bilan
parent e2dd2c52d2
commit 77fd8a4684
7 changed files with 310 additions and 52 deletions

View File

@@ -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.
@@ -28,7 +28,6 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.util.Assert;
/**
* Used to advise {@link MessageHandler}s.
@@ -50,10 +49,14 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
private volatile MessageChannel successChannel;
private volatile String successChannelName;
private volatile Expression onFailureExpression;
private volatile MessageChannel failureChannel;
private volatile String failureChannelName;
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
private volatile boolean trapException = false;
@@ -64,34 +67,106 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
private volatile EvaluationContext evaluationContext;
public void setOnSuccessExpression(String onSuccessExpression) {
Assert.notNull(onSuccessExpression, "'onSuccessExpression' must not be null");
/**
* Set the expression to evaluate against the message after a successful
* handler invocation.
* @param onSuccessExpression the SpEL expression.
* @since 4.3.7
*/
public void setOnSuccessExpressionString(String onSuccessExpression) {
this.onSuccessExpression = new SpelExpressionParser().parseExpression(onSuccessExpression);
}
/**
* Set the expression to evaluate against the message after a successful
* handler invocation.
* @param onSuccessExpression the SpEL expression.
* @since 5.0
*/
public void setOnSuccessExpression(Expression onSuccessExpression) {
this.onSuccessExpression = onSuccessExpression;
}
/**
* Set the expression to evaluate against the message after a successful
* handler invocation.
* @param onSuccessExpression the SpEL expression.
* @deprecated in favor of {@link #setOnSuccessExpression(Expression)}
*/
@Deprecated
public void setExpressionOnSuccess(Expression onSuccessExpression) {
this.onSuccessExpression = onSuccessExpression;
}
public void setOnFailureExpression(String onFailureExpression) {
Assert.notNull(onFailureExpression, "'onFailureExpression' must not be null");
/**
* Set the expression to evaluate against the root message after a failed
* handler invocation. The exception is available as the variable {@code #exception}
* @param onFailureExpression the SpEL expression.
* @since 4.3.7
*/
public void setOnFailureExpressionString(String onFailureExpression) {
this.onFailureExpression = new SpelExpressionParser().parseExpression(onFailureExpression);
}
/**
* Set the expression to evaluate against the root message after a failed
* handler invocation. The exception is available as the variable {@code #exception}
* @param onFailureExpression the SpEL expression.
* @since 5.0
*/
public void setOnFailureExpression(Expression onFailureExpression) {
this.onFailureExpression = onFailureExpression;
}
/**
* Set the expression to evaluate against the root message after a failed
* handler invocation. The exception is available as the variable {@code #exception}
* @param onFailureExpression the SpEL expression.
* @deprecated in favor of {@link #setOnFailureExpression(Expression)}
*/
@Deprecated
public void setExpressionOnFailure(Expression onFailureExpression) {
this.onFailureExpression = onFailureExpression;
}
/**
* Set the channel to which to send the {@link AdviceMessage} after evaluating the
* success expression.
* @param successChannel the channel.
*/
public void setSuccessChannel(MessageChannel successChannel) {
Assert.notNull(successChannel, "'successChannel' must not be null");
this.successChannel = successChannel;
}
/**
* Set the channel name to which to send the {@link AdviceMessage} after evaluating
* the success expression.
* @param successChannelName the channel name.
* @since 4.3.7
*/
public void setSuccessChannelName(String successChannelName) {
this.successChannelName = successChannelName;
}
/**
* Set the channel to which to send the {@link ErrorMessage} after evaluating the
* failure expression.
* @param failureChannel the channel.
*/
public void setFailureChannel(MessageChannel failureChannel) {
Assert.notNull(failureChannel, "'failureChannel' must not be null");
this.failureChannel = failureChannel;
}
/**
* Set the channel name to which to send the {@link ErrorMessage} after evaluating the
* failure expression.
* @param failureChannelName the channel name.
* @since 4.3.7
*/
public void setFailureChannelName(String failureChannelName) {
this.failureChannelName = failureChannelName;
}
/**
* If true, any exception will be caught and null returned.
* Default false.
@@ -112,10 +187,12 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
}
/**
* If true and an onSuccess expression evaluation fails with an exception, the exception will be thrown to the
* caller. If false, the exception is caught. Default false. Ignored for onFailure expression evaluation - the
* original exception will be propagated (unless trapException is true).
* @param propagateOnSuccessEvaluationFailures The propagateOnSuccessEvaluationFailures to set.
* If true and an onSuccess expression evaluation fails with an exception, the
* exception will be thrown to the caller. If false, the exception is caught. Default
* false. Ignored for onFailure expression evaluation - the original exception will be
* propagated (unless trapException is true).
* @param propagateOnSuccessEvaluationFailures The
* propagateOnSuccessEvaluationFailures to set.
*/
public void setPropagateEvaluationFailures(boolean propagateOnSuccessEvaluationFailures) {
this.propagateOnSuccessEvaluationFailures = propagateOnSuccessEvaluationFailures;
@@ -163,6 +240,9 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
evalResult = e;
evaluationFailed = true;
}
if (this.successChannel == null && this.successChannelName != null && getChannelResolver() != null) {
this.successChannel = getChannelResolver().resolveDestination(this.successChannelName);
}
if (evalResult != null && this.successChannel != null) {
AdviceMessage<?> resultMessage = new AdviceMessage<Object>(evalResult, message);
this.messagingTemplate.send(this.successChannel, resultMessage);
@@ -181,6 +261,9 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan
evalResult = e;
logger.error("Failure expression evaluation failed for " + message + ": " + e.getMessage());
}
if (this.failureChannel == null && this.failureChannelName != null && getChannelResolver() != null) {
this.failureChannel = getChannelResolver().resolveDestination(this.failureChannelName);
}
if (evalResult != null && this.failureChannel != null) {
MessagingException messagingException = new MessageHandlingExpressionEvaluatingAdviceException(message,
"Handler Failed", this.unwrapThrowableIfNecessary(exception), evalResult);

View File

@@ -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.
@@ -53,4 +53,12 @@ public class AdviceMessage<T> extends GenericMessage<T> {
return this.inputMessage;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(super.toString());
builder.setLength(builder.length() - 1);
builder.append(", inputMessage=").append(this.inputMessage.toString()).append("]");
return builder.toString();
}
}