【问题标题】:Is it possible to configure poller for each entity from a data source?是否可以为数据源中的每个实体配置轮询器?
【发布时间】:2021-01-15 05:52:54
【问题描述】:

我正在通过 spring 集成开发一个多属性微服务。我从 LOGIN 表等数据库中获取每个属性的登录凭据。 LOGIN 表有这些字段; LOGIN.username、LOGIN.pass 和 LOGIN.period(轮询周期)。如果我想使用基于 LOGIN.period 字段的不同轮询器配置的微服务,我该怎么做?

    @Bean
    public IntegrationFlow start() {
        return IntegrationFlows
                .from(() -> DAO.getLoginList()) // from a web service.
                .split() // splits the each login credentials for each property.
                .channel("X_CHANNEL") // subscribes to a channel todo business logic.
                .get();
    }

是否可以根据数据库中的 LOGIN.period 值实现一个组件以在不同的轮询器配置中进行工作流程?

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    请说明您如何从数据库中获取该信息。

    但是,如果您的观点是您可能在 DB 中有多个记录,并且您希望为所有这些记录设置多个轮询器,那么您需要查看动态流注册:https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/dsl.html#java-dsl-runtime-flows

    因此,您从数据库中读取数据,在循环中为每条记录创建IntegrationFlow,并根据记录中的数据配置它们的轮询器。

    【讨论】:

    • 我正在通过 Web 服务从 LOGIN 表中获取登录凭据。
    • 好的,我看到了.from(() -> DAO.getLoginList()) // from a web service.。所以,听起来你仍然有一些动态,因此运行中的IntegrationFlowContextIntegrationFlow 实例是适合你的方法。您只是无法在应用程序启动和调整轮询 bean 时获得该信息。对于您的用例,当您从该 DAO 调用中获得拆分结果时,确实需要在运行时执行此操作。
    【解决方案2】:

    根据 Artem Bilan 的回答,我已经实现了 IntegrationFlowContext 和 IntegrationFlow 实例;

        @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();
            });
        }
    
    

    【讨论】:

    猜你喜欢
    • 2011-03-17
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多