The Java & Annotation configuration variant:
@Bean
public PollerMetadata poller(PlatformTransactionManager transactionManager) {
PeriodicTrigger trigger = new PeriodicTrigger(1000);
trigger.setFixedRate(true);
MatchAlwaysTransactionAttributeSource attributeSource = new MatchAlwaysTransactionAttributeSource();
attributeSource.setTransactionAttribute(new DefaultTransactionAttribute());
TransactionInterceptor interceptor = new TransactionInterceptor(transactionManager, attributeSource);
PollerMetadata poller = new PollerMetadata();
poller.setTrigger(trigger);
poller.setAdviceChain(Collections.singletonList(interceptor));
return poller;
}
@Bean
@InboundChannelAdapter(value = "target", poller = @Poller("poller"))
public MessageSource<?> counterMessageSource(DataSource dataSource) {
JdbcPollingChannelAdapter adapter =
new JdbcPollingChannelAdapter(dataSource, "select * from item where status = 2");
adapter.setUpdateSql("update item set status = 10 where id in (:id)");
return adapter;
}
Right, the transaction configuration looks ugly, but there is not yet a simple way to provide a TX Advice
to the PollerMetadata
.
You can consider to use Spring Integration Java DSL on the matter:
@Bean
public MessageSource<?> jdbcAdapter(DataSource dataSource) {
JdbcPollingChannelAdapter adapter =
new JdbcPollingChannelAdapter(dataSource, "select * from item where status = 2");
adapter.setUpdateSql("update item set status = 10 where id in (:id)");
return adapter;
}
@Bean
public IntegrationFlow jdbcFlow(MessageSource<?> jdbcAdapter) {
return IntegrationFlows
.from(jdbcAdapter, e ->
e.poller(p -> p.fixedRate(1000).transactional(transactionManager())))
.channel(c -> c.direct("target"))
.get();
}
Feel free to raise a GH-issue against DSL project to provide a DSL for the JDBC adapters.