如何使用自定义错误通道和WebFlux.inboundGateway答复错误响应?

时间:2020-06-29 19:49:50

标签: spring-integration spring-integration-dsl

在下面的两个链接之后,我创建了一个IntegrationFlow,它会在发生错误时调用自定义errorFlow。但是我看到的行为是应用程序从不回复客户端,只是挂起。如何从errorFlow回复请求?作为参考,我托管了示例on github

<?php
// app/Console/Commands/FooCommand.php
public function handle()
{
  $file = $this->argument('file');
  
  if (! file_exists($file)) {
    $this->line('Error! File does not exist!');
    exit;
  }
}

// tests/Feature/FooCommandTest.php
public function testFoo() {
  $this->artisan('foo', ['file' => 'foo.txt'])->expectsOutput('Something');
}

请求...

    @Bean
    public IntegrationFlow mainFlow() {
        return IntegrationFlows.from(WebFlux.inboundGateway(URI)
                .errorChannel(customErrorChannel()))
                .channel(MessageChannels.flux()) //Work around: https://github.com/spring-projects/spring-integration/issues/3276
                .transform(p -> {
                    throw new RuntimeException("Error!");
                    //return "Ok Response"; //If we comment the throw and uncomment this, then the the code replies to the request ok.
                })
                .get();
    }

    @Bean
    public PublishSubscribeChannel customErrorChannel() {
        return MessageChannels.publishSubscribe().get();
    }

    @Bean
    public IntegrationFlow errorFlow() {
        return IntegrationFlows.from("customErrorChannel")
                .transform(p -> {
                    return "Error Response";
                })
                .get();
    }

Request screenshot

1 个答案:

答案 0 :(得分:1)

结果证明,该逻辑仍然存在问题。

我们需要对其进行更多调查并找出解决办法。

与此同时,您可以对ExpressionEvaluatingRequestHandlerAdvice使用故障变压器,并在errorFlow()中以类似方式处理错误。将returnFailureExpressionResult视为true。而且您的onFailureExpression应该是对该customErrorChannel的网关调用。或者,您可以使用该表达式中的MessagingTemplate.sendAndReceive() API代替网关。

在文档中查看更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#message-handler-advice-chain