首页 > 解决方案 > Spring 集成 DSL 关闭策略

问题描述

目前我有这个流程

package com.example.demo.flow;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.dsl.*;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.file.dsl.Files;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.Executors;

/**
 * Created by on 03/01/2020.
 */
@Component
@Slf4j
public class TestFlow {

    @Bean
    public StandardIntegrationFlow errorChannelHandler() {

        return IntegrationFlows.from("testChannel")
                .handle(o -> {

                    log.info("Handling error....{}", o);
                }).get();
    }

    @Bean
    public IntegrationFlow testFile() {


        IntegrationFlowBuilder testChannel = IntegrationFlows.from(Files.inboundAdapter(new File("d:/input-files/")),
                e -> e.poller(Pollers.fixedDelay(5000L).maxMessagesPerPoll(5)
                        .errorChannel("testChannel")))
                .channel(MessageChannels.executor(Executors.newFixedThreadPool(5)))
                .transform(o -> {

                    throw new RuntimeException("Failing on purpose");

                }).handle(o -> {
                });

        return testChannel.get();


    }


}

我计划由于关闭而停止此流程,但可能有一些文件是流程的一部分。如果我关闭集成流程,当前正在处理的文件是否仍会完成,或者它们将被停止,可能会杀死它们的线程?

标签: javaspring-integrationspring-integration-dsl

解决方案


如果停止但不破坏,则过程中的所有内容都将得到妥善处理。只有新数据将从源或消息通道发出。当您关闭它时,这也是一种自然的优雅关闭行为ApplicationContext:它首先停止 bean,让它们完成正在进行的任何事情,然后才销毁它们。

因此,到目前为止,您应该对自己的意图感到满意。请与我们分享您的经验(如果不是这样) - 如果需要,我们将尽快进行适当的修复。关键是停止阶段的目标确实是不发出新数据,而是让现有处理优雅地完成。在文档中查看更多信息:https ://docs.spring.io/spring-integration/docs/current/reference/html/system-management.html#jmx-shutdown


推荐阅读