首页 > 解决方案 > 如何在spring集成中使用java dsl将db poller xml配置转换为java配置

问题描述

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

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>

我对java DSL了解不多。有人可以告诉我如何转换吗?

谢谢

标签: javaspring-integrationspring-integration-dsl

解决方案


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

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

JdbcPollingChannelAdapter是一个MessageSource实现,因此可以在 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) {

poller可以使用该回调endpointConfigurer和通过Pollers工厂进行配置。

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


推荐阅读