setOnFailureExpression不工作的#root和#exception

问题描述 投票:1回答:1
@Bean
public ExpressionEvaluatingRequestHandlerAdvice after() {
    logger.debug("Evaluating expression advice. ");
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setTrapException(true);
    advice.setOnFailureExpressionString("#root");
    advice.setSuccessChannel(rtwSourceDeletionChannel());
    advice.setFailureChannel(rtwFtpFailureHandleChannel());
    advice.setPropagateEvaluationFailures(true);
    return advice;
}

@Bean
public IntegrationFlow rtwFtpFailureHandlerFlow() {
    return IntegrationFlows
                .from(rtwFtpFailureHandleChannel())
                .handle( msg -> {
                    // code to delete all files except source. 
                    logger.debug("Handling Failure......");

                    List<String> transitPaths = (List<String>) msg.getHeaders().get(AdviceHandlerMessageHeaders.TRANSIT_PATHS); 
                    String sourcePath = (String) msg.getHeaders().get(AdviceHandlerMessageHeaders.SOURCE); 

                    System.out.println("payload: " + msg.getPayload());
                    })
                .get();
}

对于产生如下#root和#exception相同的结果:

payload: org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice$MessageHandlingExpressionEvaluatingAdviceException: Handler Failed; nested exception is org.springframework.messaging.MessageHandlingException: error occurred in message handler [rtwFtpOutboundHandler]; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.integration.util.PoolItemNotAvailableException: Failed to obtain pooled item, failedMessage=GenericMessage [payload=sample.pdf, headers={file_name=sample.pdf, TRANSIT_PATHS=[\\localhost\atala-capture-upload, sample.tif], SOURCE=\\localhost\atala-capture-upload, file_originalFile=\\localhost\atala-capture-upload\sample.tif, id=746728a4-9b9e-847f-6c2c-29aa8a4a5fd6, file_relativePath=sample.tif, timestamp=1549922458296}], failedMessage=GenericMessage [payload=sample.pdf, headers={file_name=sample.pdf, TRANSIT_PATHS=[\\localhost\atala-capture-upload, sample.tif], SOURCE=\\localhost\atala-capture-upload, file_originalFile=\\localhost\atala-capture-upload\sample.tif, id=746728a4-9b9e-847f-6c2c-29aa8a4a5fd6, file_relativePath=sample.tif, timestamp=1549922458296}]
spring-integration spring-integration-dsl
1个回答
2
投票

该工程按预期。

的逻辑是这样的:

try {
        evalResult = this.onFailureExpression.getValue(prepareEvaluationContextToUse(exception), message);
    }
    catch (Exception e) {
        evalResult = e;
        logger.error("Failure expression evaluation failed for " + message + ": " + e.getMessage());
    }
    DestinationResolver<MessageChannel> channelResolver = getChannelResolver();
    if (this.failureChannel == null && this.failureChannelName != null && channelResolver != null) {
        this.failureChannel = channelResolver.resolveDestination(this.failureChannelName);
    }
    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);
    }

onFailureExpression的评价结果​​被添加到MessageHandlingExpressionEvaluatingAdviceException财产。而此异常被包装到ErrorMessage发送到配置failureChannel

所以,你的代码是正确的,到目前为止,除非你缺少的事实,你在payload处理消息的rtwFtpFailureHandlerFlow正是提及MessageHandlingExpressionEvaluatingAdviceException。要访问您的表达式的结果,你需要施放payload到这个异常并调用其getEvaluationResult()。在另一方面,你甚至不需要有任何复杂的表达,因为请求消息可作为此异常的failedMessage。再加上真正的失败就是在这个异常的原因。

见文档的更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints-chapter.html#expression-advice

© www.soinside.com 2019 - 2024. All rights reserved.