spring integration expressionevaluatingrequesthandleradvice无法计算failureexpression

k10s72fa  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(367)

昨天我问了一个问题之后,我发现了一些问题 ExpressionEvaluatingRequestHandlerAdvice 因为它不像我预期的那样工作。
我尝试了doc的例子,我的期望是得到两个 AdviceMessage :一个说“good was successful”,另一个说“bad was bad,with reason…”但是当我运行代码时,我得到一条advicemessage和一条errormessage。

AdviceMessage [payload=good was successful, headers={id=2eb612ea-d3cd-f853-3295-236505a130f3, timestamp=1611307975199}, inputMessage=GenericMessage [payload=good, headers={id=725e89ea-be09-7534-1b29-5c7412a99a79, timestamp=1611307975196}]]
ErrorMessage [payload=org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice$MessageHandlingExpressionEvaluatingAdviceException: Handler Failed; nested exception is java.lang.IllegalStateException: Could not invoke the method 'public java.lang.Object com.example.demo.EerhaApplication$$Lambda$299/0x00000008401d7040.handle(java.lang.Object,org.springframework.messaging.MessageHeaders)', failedMessage=GenericMessage [payload=bad, headers={id=7639425f-7676-d521-5994-4c74e5b09f4a, timestamp=1611307975200}], headers={id=aaa07286-4bc3-71e5-09fb-6b55a46e0ecf, timestamp=1611307975201}]

显然,总是抛出一个异常来计算failureexpression,但却永远得不到期望的结果。
我使用的是springboot2.4.2,但我也尝试过旧版本,总是有相同的行为。
我做错什么了?

laik7k3q

laik7k3q1#

我们需要看看你的代码 ExpressionEvaluatingRequestHandlerAdvice ,但无论如何 AdviceMessage 仅在成功评估 onSuccessExpression :

if (evalResult != null && this.successChannel != null) {
    AdviceMessage<?> resultMessage = new AdviceMessage<>(evalResult, message);
    this.messagingTemplate.send(this.successChannel, resultMessage);
}

当你 MessageHandler 失败(即使 onSuccessExpression 失败),我们转到这个分支:

catch (RuntimeException e) {
        Exception actualException = unwrapExceptionIfNecessary(e);
        if (this.onFailureExpression != null) {
            Object evalResult = evaluateFailureExpression(message, actualException);
            if (this.returnFailureExpressionResult) {
                return evalResult;
            }
        }

我们派了一个 ErrorMessage :

if (evalResult != null && this.failureChannel != null) {
        MessagingException messagingException =
                new MessageHandlingExpressionEvaluatingAdviceException(message, "Handler Failed",
                        unwrapThrowableIfNecessary(exception), evalResult);
        ErrorMessage errorMessage = new ErrorMessage(messagingException);
        this.messagingTemplate.send(this.failureChannel, errorMessage);
    }

所以,你表现出来的是一种预期的行为。
因此,我们需要看到您的配置,并了解在这两种情况下您的期望是什么。

tcbh2hod

tcbh2hod2#

为了得到字符串“bad was bad,with reason…”打印这是我的解决方案:

@Bean
public IntegrationFlow failure() {
  return f -> f.handle(m -> {
    System.out.println(((ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException)m.getPayload()).getEvaluationResult());
  });
}

相关问题