如何在运行时注册集成流程?

时间:2020-10-21 08:39:50

标签: spring-integration spring-integration-dsl

我正在为多种属性构建微服务。因此,每个属性都有不同的配置。为此,我已经实现了类似的东西;

    @Autowired
    IntegrationFlowContext flowContext;

    @Bean
    public void setFlowContext() {
        List<Login> loginList = DAO.getLoginList(); // a web service
        loginList.forEach(e -> {
            IntegrationFlow flow = IntegrationFlows.from(() -> e, c -> c.poller(Pollers.fixedRate(e.getPeriod(), TimeUnit.SECONDS, 5)))
                    .channel("X_CHANNEL")
                    .get();
            flowContext.registration(flow).register();
        });
    }

通过此实现,我将在应用程序启动之前获取loginList。因此,启动应用程序后,由于没有轮询器配置,因此无法从Web服务获取loginList。问题是loginList可能会更改;可以添加或删除新的登录凭据。因此,我想实现一些可以在X时段内从Web服务获取loginList的方法,然后通过loginList我需要注册为每个loginList创建的流。为了实现这一目标,我已经实现了类似的东西;

    @Bean
    public IntegrationFlow setFlowContext() {
        return IntegrationFlows
                .from(this::getSpecification, p -> p.poller(Pollers.fixedRate(X))) // the specification is constant.
                .transform(payload -> DAO.getLoginList(payload))
                .split()
                .<Login>handle((payload, header) -> {
                    IntegrationFlow flow = IntegrationFlows.from(() -> payload, c -> c.poller(Pollers.fixedRate(payload.getPeriod(), TimeUnit.SECONDS, 5)))
                    .channel("X_CHANNEL")
                    .get();
                    flowContext.registration(flow).register().start();
                    return null;
                })
                .get();
    }

基本上,我已经使用过 start()方法,但是这种方法无法发挥作用。看到这个;

flowContext.registration(flow).register().start();

最后,我已经读过Dynamic and Runtime Integration Flows,但仍然无法实现此功能。

1 个答案:

答案 0 :(得分:0)

不能在@Bean定义中使用动态流注册。

它旨在在应用程序上下文完全初始化之后的运行时使用。