簡體   English   中英

異步流中出站網關上的錯誤處理

[英]Error handling on an outbound gateway in async flow

我有這樣的集成流程:

@Bean
public IntegrationFlow inboundRequestFlow()
{
    return IntegrationFlows.from( inboundRequestGateway() )
        .log( ... )
        .filter(  requestValidator,
            spec -> spec.discardChannel( invalidRequestChannel() ) )
        .bridge( spec -> spec.requiresReply( false ) )
        .channel( asyncFlowChannel )
        .wireTap(
            //sends a NO_CONTENT reply if the request is ok
            flow -> flow.enrichHeaders( spec -> spec.header( HttpHeaders.STATUS_CODE, HttpStatus.NO_CONTENT ).defaultOverwrite( true ) )
                .transform( payload -> "" )
                .channel( inboundGatewayReplyChannel() )
        ).get();

}

它在http網關上接收一個請求,對其進行驗證,如果一切正常,則將該請求發送到“ asyncFlowChannel”,並使用204答復入站網關。

“ asyncFlowChannel”是在Executor通道上運行的另一個IntegrationFlow的起點:

@Bean
public IntegrationFlow outboundFlow()
{
    return IntegrationFlows.from( asyncFlowChannel)
        .log( ... )
        .transform( ... )
        .transform( ... )
        .split(... )            
        .resequence( ... )
        .enrichHeaders( ... )
        .log( ... )
        .transform( ... )
        .handle( this.outboundSOAPGateway() )
        .log( .. )
        .handle( ... )
        .bridge( spec -> spec.requiresReply( false ) )
        .channel( anotherAsyncFlowChannel )
        .get();
}

如果我的outboundGateway發生異常(由於與網絡相關的IO錯誤或錯誤響應),我想記錄該錯誤並采取適當的措施。 但是我無法在outboundSOAPGateway上設置錯誤通道,而在開始流上的inboundRequestGateway已經收到了它的回復。

我得到的錯誤的唯一線索是此日志:

10:19:53.002 WARN [outbound-flow-0] org.springframework.messaging.core.GenericMessagingTemplate $ TemporaryReplyChannel-收到回復消息,但接收線程已經收到回復:ErrorMessage [payload = ...,headers =.。 ]

我的問題是:在啟動流的inboundGateway已經收到其答復的異步流中,在出站網關上處理錯誤的正確方法是什么?

任何MessageHandler端點都可以隨AbstractRequestHandlerAdvice 其中之一是ExpressionEvaluatingRequestHandlerAdvice ,您可以在其中捕獲異常並將其發送到failureChannelhttps : failureChannel 。 html#expression-advice

為此,可以為.handle( this.outboundSOAPGateway() )提供第二個參數,例如:

.handle((GenericHandler<?>) (p, h) -> {
                    throw new RuntimeException("intentional");
                }, e -> e.advice(retryAdvice()))

在這種情況下,我使用

@Bean
public RequestHandlerRetryAdvice retryAdvice() {
    RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
    requestHandlerRetryAdvice.setRecoveryCallback(new ErrorMessageSendingRecoverer(recoveryChannel()));
    return requestHandlerRetryAdvice;
}

但是對ExpressionEvaluatingRequestHandlerAdvice

順便說一句, retryAdvice()可能也會為您解決問題。 請參見其ErrorMessageSendingRecoverer

代替.channel( asyncFlowChannel ) ,使用

.gateway(asyncFlowChannel, e -> e.errorChannel(...))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM