【问题标题】:How to convert db poller xml configurations to java configurations using java dsl in spring integration如何在spring集成中使用java dsl将db poller xml配置转换为java配置
【发布时间】:2020-07-08 21:03:28
【问题描述】:

尝试在 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 了解不多。谁能告诉我如何转换它?

谢谢

【问题讨论】:

    标签: java spring-integration spring-integration-dsl


    【解决方案1】:

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

    所以,这里提到了&lt;int-jdbc:inbound-channel-adapter&gt; 的 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) {
    

    可以使用endpointConfigurer 回调和Pollers 工厂配置poller

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

    【讨论】:

    • 假设我定义了 processMessages() 用于从数据库接收行。如何从处理函数 .handle(processMessages()) 中获取行?
    • JdbcPollingChannelAdapter 从 DB 中返回要生成到通道中的那些行。因此,您的processMessages() 应该接受这些行作为参数List&lt;?&gt;(或查看JdbcPollingChannelAdapter 上的rowMapper 选项以从该行构建一些特定于域的POJO)。然后你可以使用你的方法handle(this, "processMessages")
    • 感谢它工作并能够接收 List> 中的行集。此外,在接收到有效负载中的行后,如果行集为空,我如何确认行集?如果我使用 isEmpty 方法,即 if(payload.isEmpty()){} 那么这个 if 语句在这种情况下永远不会执行。
    • 当数据库没有返回数据时,通道适配器不会产生消息。因此,如果您在服务方法中获取列表,那么它肯定有一些数据。
    • 如果列表为空,是否有办法在控制台中记录/打印消息?
    猜你喜欢
    • 2015-05-30
    • 2011-01-15
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2015-11-08
    • 2020-12-19
    • 2018-07-19
    • 1970-01-01
    相关资源
    最近更新 更多