简体   繁体   中英

Spring Integration DSL JDBC inbound channel adapter

I use spring integration to read data from database. Now i use polling adapter

@Bean
public MessageSource<Object> jdbcMessageSource() {
   JdbcPollingChannelAdapter a = new JdbcPollingChannelAdapter(dataSource(), "SELECT id, clientName FROM client");
   return a;
}

Flow:

@Bean
public IntegrationFlow pollingFlow() throws Exception {
    return IntegrationFlows.from(jdbcMessageSource(), 
                c -> c.poller(Pollers.fixedRate(30000).maxMessagesPerPoll(1)))
            .channel(channel1())
            .handle(handler())
            .get();
}

But i would like to schedule my flow from other system. Anyone know how to do this?

schedule my flow from other system

From your flow perspective it sounds like event driven action . For this purpose you should use JdbcOutboundGateway with the same SELECT .

And, of course, you should find the hook for that external system to trigger an event for your flow input channel. That might be any Inbound Channel Adapter or Message Driven Adapter, eg JMS, AMQP, HTTP and so. Depends what you already have in your middleware and what will be possible to expose from this your application to external systems.

I think i solve the problem with a custom trigger:

public Trigger onlyOnceTrigger() {
       return new Trigger() {
              private final AtomicBoolean invoked = new AtomicBoolean();
              @Override
              public Date nextExecutionTime(TriggerContext triggerContext) {
                    return this.invoked.getAndSet(true) ? null : new Date();
              }
       };
}

And my flow:

public IntegrationFlow pollingFlow() throws Exception {
    return IntegrationFlows.from(jdbcMessageSource(), 
                c -> c.poller(Pollers.trigger(onlyOnceTrigger()).maxMessagesPerPoll(1)))
            .channel(channel1())
            .handle(handler())
            .get();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM