Trying to do polling the database in spring integration. I have XML code but I want to convert XML config to java DSL.
XML Code:
<context:component-scan
base-package="org.springintegration.polling.dbpoller" />
<int:channel id="fromdb">
<int:queue />
</int:channel>
<int:poller default="true" fixed-rate="5000" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/springboot" />
<property name="username" value="root" />
<property name="password" value="mh" />
</bean>
<int:service-activator input-channel="fromdb"
ref="jdbcMessageHandler" />
<int-jdbc:inbound-channel-adapter
channel="fromdb" data-source="dataSource" query="SELECT * FROM Items WHERE INVENTORY_STATUS = 0"
update="UPDATE Items SET INVENTORY_STATUS = 1">
<int:poller fixed-delay="4000" />
</int-jdbc:inbound-channel-adapter>
I don't have much knowledge about java DSL. Can someone tell me how to convert it?
Thanks
There is no JDBC-specific Java DSL factories and builders, but the component behind a <int-jdbc:inbound-channel-adapter>
can be used in the IntegrationFlow
definition.
See this doc: https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#finding-class-names-for-java-and-dsl-configuration
So, a Java class for the <int-jdbc:inbound-channel-adapter>
is mentioned here:
<xsd:element name="inbound-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Defines a Polling Channel Adapter for the
'org.springframework.integration.jdbc.JdbcPollingChannelAdapter'
for polling a database.
</xsd:documentation>
</xsd:annotation>
The JdbcPollingChannelAdapter
is a MessageSource
implementation and therefore can be used in Java DSL with this factory method in the IntegrationFlows
:
/**
* Populate the provided {@link MessageSource} object to the {@link IntegrationFlowBuilder} chain.
* The {@link org.springframework.integration.dsl.IntegrationFlow} {@code startMessageSource}.
* In addition use {@link SourcePollingChannelAdapterSpec} to provide options for the underlying
* {@link org.springframework.integration.endpoint.SourcePollingChannelAdapter} endpoint.
* @param messageSource the {@link MessageSource} to populate.
* @param endpointConfigurer the {@link Consumer} to provide more options for the
* {@link org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean}.
* @return new {@link IntegrationFlowBuilder}.
* @see MessageSource
* @see SourcePollingChannelAdapterSpec
*/
public static IntegrationFlowBuilder from(MessageSource<?> messageSource,
@Nullable Consumer<SourcePollingChannelAdapterSpec> endpointConfigurer) {
The poller
could be configured using that endpointConfigurer
callback and via Pollers
factory.
The <int:service-activator>
is just handle()
in Java DSL and there is no reason to specify a channel between your JdbcPollingChannelAdapter
in the from()
factory and next .handle()
in the method chain. It just inserted there naturally in between to let us to avoid boilerplate code for direct channels.