Error handling Spring集成-如何将错误处理委托给单独的线程

Error handling Spring集成-如何将错误处理委托给单独的线程,error-handling,spring-integration,Error Handling,Spring Integration,我的Spring集成流程如下所示 从目录中读取文件 将名为errorChannel:“exceptionChannel”的标头添加到消息中 执行业务逻辑 将输出打印到“成功目录” 如果步骤3中出现错误,异常将发送到exceptionChannel,并写入“失败目录”。 我希望将错误流委派给单独的线程。 我所拥有的: 如果有5个文件,而第三个文件有错误 ->2个文件被写入成功目录 ->1文件被写入失败的目录 ->错误文件出现后,流停止 我需要什么: 如果有5个文件,而第三个文件有错误 ->前2个文

我的Spring集成流程如下所示

  • 从目录中读取文件
  • 将名为errorChannel:“exceptionChannel”的标头添加到消息中
  • 执行业务逻辑
  • 将输出打印到“成功目录”
  • 如果步骤3中出现错误,异常将发送到exceptionChannel,并写入“失败目录”。 我希望将错误流委派给单独的线程。

    我所拥有的: 如果有5个文件,而第三个文件有错误

    ->2个文件被写入成功目录

    ->1文件被写入失败的目录

    ->错误文件出现后,流停止

    我需要什么: 如果有5个文件,而第三个文件有错误

    ->前2个文件必须写入成功目录

    ->第三个文件必须写入失败的目录

    ->最后2个文件必须写入成功目录

    成功流程代码:

    @Bean(name="exceptionChannel")
    MessageChannel exceptionChannel() {
        return MessageChannels.direct()
                .get();
    }
    
    @Bean
    public IntegrationFlow migrateInputToOutput() {
        return IntegrationFlows.from(Files.inboundAdapter(new File(INPUT_DIR))),
                .enrichHeaders(h -> h.header("errorChannel", "exceptionChannel", true))
                .handle(someBusinessLogic) // ANY EXCEPTION HERE IS SENT TO exceptionChannel
                .handle(Files.outboundAdapter(new File(OUTPUT_SUCCESS_DIR))
                .get();
    }
    
    @Bean
    public IntegrationFlow handleErrorInMigration() {
        return IntegrationFlows.from("exceptionChannel"),
                .handle(errorLogicToPrintException)
                .handle(Files.outboundAdapter(new File(OUTPUT_ERROR_DIR))
                .get();
    }
    
    处理错误的代码:

    @Bean(name="exceptionChannel")
    MessageChannel exceptionChannel() {
        return MessageChannels.direct()
                .get();
    }
    
    @Bean
    public IntegrationFlow migrateInputToOutput() {
        return IntegrationFlows.from(Files.inboundAdapter(new File(INPUT_DIR))),
                .enrichHeaders(h -> h.header("errorChannel", "exceptionChannel", true))
                .handle(someBusinessLogic) // ANY EXCEPTION HERE IS SENT TO exceptionChannel
                .handle(Files.outboundAdapter(new File(OUTPUT_SUCCESS_DIR))
                .get();
    }
    
    @Bean
    public IntegrationFlow handleErrorInMigration() {
        return IntegrationFlows.from("exceptionChannel"),
                .handle(errorLogicToPrintException)
                .handle(Files.outboundAdapter(new File(OUTPUT_ERROR_DIR))
                .get();
    }
    

    Files.inboundAdapter()
    是可轮询的源代码,因此在代码中配置了类似于
    Poller
    的东西。那个有一个
    errorChannel()
    选项。这样做确实更好。如果您的轮询器是全局轮询器,并且您不想对其进行修改,则最好为此端点精确配置轮询器:

    IntegrationFlows.from(Files.inboundAdapter(new File(INPUT_DIR)), 
            e -> e.poller(p -> p.fixedDelay().errorChannel(exceptionChannel())))
    
    这样就不会影响所有其他轮询端点

    将错误处理移到不同线程时,需要考虑将< <代码>异常通道< /代码>作为<代码> Exchange通道>代码>:< /P>
    @Bean(name="exceptionChannel")
    MessageChannel exceptionChannel() {
        return MessageChannels.executor(myExecutor)
                .get();
    }
    

    @ArtemBilan-你能帮忙吗?@GaryRussell-你能帮忙吗?MessageChannel.executor为我工作。接受答案。