如何使用 jdbc-outbound-channel-adapter 过滤 Spring 集成流?

How to filter Spring Integration flow using jdbc-outbound-channel-adapter?

我有一个要求,其中我必须 FTP 从远程服务器读取 XML 文件并将它们转储到本地目录中。之后,我必须使用来自传入负载的值对数据库发起 SQL 查询,然后 - 根据查询结果 - 决定是否要继续流程。

<int-ftp:inbound-channel-adapter id="ftpInbound1"
    channel="inboundFTPFileChannel" session-factory="ftpSessionFactory"
            local-filter="compositeFilter" filename-pattern="*.xml"
            preserve-timestamp="true" charset="UTF-8" remote-directory="${remote.request.cdr.circle.dir}"
            remote-file-separator="/" local-directory="${local.request.dir}">
            <int:poller fixed-rate="${ftp.file.poller.interval}"
                time-unit="SECONDS" max-messages-per-poll="-1" />
        </int-ftp:inbound-channel-adapter>

<int:filter input-channel="inboundFTPFileChannel"
        output-channel="jdbcChannel" discard-channel="discardChannel"
        expression="payload.isFile()" />

<int-jdbc:outbound-channel-adapter
        channel="jdbcChannel" query="SELECT COUNT(1) FROM some_table WHERE some_col = some_value"
        data-source="myDataSource" />

在这个阶段,如果查询 returns 非零输出,我想继续流程。否则此消息的流程应该结束。此外,如果流程应该继续,下一个通道的输出应该与 'jdbcChannel' 上出现的 Spring 集成消息相同。请指教

我绝对可以做的是编写一个 <int:filter> 来引用返回 true 或 false 的 bean。但我只是想避免必须编写此 Java 代码!

将原始负载保存在 header 中,稍后再恢复...

<int:header-enricher ...>
    <int:header name="savedPayload" expression="payload" />
</int:header-enricher>

<int:jdbc-outbound-gateway... />

<int:filter ... expression="payload > 0" />

<int:transformer ... expression="headers['savedPayload']" />

...

请注意,您需要使用网关,而不是通道适配器 (JDBC)。

如果您愿意,可以将所有这些都放在一个 <chain/> 中。