如何在Spring Integration Database Poller中从多个表中获取数据

问题描述

我有一个数据库轮询器,可以从多个表中获取行。因此,我编写了两个轮询程序,以使用以下代码独立获取数据,并且工作正常,但是以下代码的问题是冗余。那么有什么更好的方法可以实现这一目标?

//Poller 1 for table one:   
 
@Bean
    public MessageSource<Object> jdbcmessageSource() throws sqlException {
        return new JdbcPollingChannelAdapter(dataSourceConfig.getDataSource(),"select * from db.table_name_one");
    }
    @Bean
    public IntegrationFlow pollingFlow() throws Exception {
        return IntegrationFlows.from(jdbcmessageSource(),c -> c.poller(Pollers.fixedrate(10000)))
                .handle(List.class,(payload,headers) -> {
                    transferExecutor.handleMessage(payload);
                    return null;
                })
                .get();
    }

//Poller 2 for table two: 
    @Bean
        public MessageSource<Object> jdbcmessageSource1() throws sqlException {
            return new JdbcPollingChannelAdapter(dataSourceConfig.getDataSource(),"select * from db.table_name_two");
        }
        @Bean
        public IntegrationFlow pollingFlow1() throws Exception {
            return IntegrationFlows.from(jdbcmessageSource1(),c -> c.poller(Pollers.fixedrate(10000)))
                    .handle(List.class,headers) -> {
                        transferExecutor.handleMessage(payload);
                        return null;
                    })
                    .get();
        }

   

解决方法

如果这些表中的数据完全不同,则您的解决方案是正确的。 由于您有不同的表,因此有不同的服务来处理它们是很合逻辑的。

想象您将为这些表执行JPA,因此您可能会有不同的实体和不同的Spring Data Repository。

但是,如果数据相似,则可以咨询DBA如何JOIN为这两个表进行选择,甚至可以在数据库中使用view来从应用程序中执行简单查询。 / p>

Spring Integration绝对无法为您做任何事情。这不是为与RDBMS交互进行一些繁重的工作而承担的框架责任。