如何在spring集成中使用javadsl将db-poller xml配置转换为java配置



尝试在spring集成中轮询数据库。我有XML代码,但我想将XML配置转换为javaDSL。

XML代码:

<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>

我对javaDSL没有太多的了解。有人能告诉我如何转换吗?

感谢

没有JDBC特定的Java DSL工厂和构建器,但<int-jdbc:inbound-channel-adapter>背后的组件可以在IntegrationFlow定义中使用。请参阅此文档:https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#finding-java和dsl配置的类名

因此,这里提到了<int-jdbc:inbound-channel-adapter>的Java类:

<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>

JdbcPollingChannelAdapterMessageSource实现,因此可以在Java DSL中使用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) {

可以使用endpointConfigurer回调并通过Pollers工厂来配置poller

<int:service-activator>在Java DSL中只是handle(),没有理由在from()工厂中的JdbcPollingChannelAdapter和方法链中的下一个.handle()之间指定通道。它只是自然地插入其中,让我们避免直接通道的样板代码。

最新更新