@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=[\localhostatala-capture-upload, sample.tif], SOURCE=\localhostatala-capture-upload, file_originalFile=\localhostatala-capture-uploadsample.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=[\localhostatala-capture-upload, sample.tif], SOURCE=\localhostatala-capture-upload, file_originalFile=\localhostatala-capture-uploadsample.tif, id=746728a4-9b9e-847f-6c2c-29aa8a4a5fd6, file_relativePath=sample.tif, timestamp=1549922458296}]
这是意料之中的事。
逻辑是这样的:
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);
}
将CCD_ 1的评估结果作为属性添加到CCD_。并且该异常被封装到ErrorMessage
以发送到配置的failureChannel
。
因此,到目前为止,您的代码是正确的,除非您忽略了这样一个事实,即您在该rtwFtpFailureHandlerFlow
中处理的消息的payload
正是所提到的MessageHandlingExpressionEvaluatingAdviceException
。要访问表达式的结果,需要将payload
强制转换为此异常并调用其getEvaluationResult()
。另一方面,您甚至不需要任何复杂的表达式,因为请求消息可以作为该异常的onFailureExpression
0使用。另外,真正的失败在于这个异常的原因。
有关详细信息,请参阅文档:https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints-chapter.html#expression-建议