curious_brain
curious_brain

Reputation: 403

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

I have a requirement wherein I have to FTP read XML files from a remote server and dump them in a local directory. After that I have to fire a SQL query against the database using values from the incoming payload and then - based on the outcome of the query - decide if I want to continue the flow or not.

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

At this stage I want to continue the flow if the query returns a non-zero output. Else the flow for this message should end. Also, if the flow should continue, the output to next channel should be the same Spring Integration Message which came on 'jdbcChannel'. Please advice.

What I can definitely do is write a <int:filter> which references a bean returning true or false. But I am just trying to avoid having to write this Java code!

Upvotes: 2

Views: 1033

Answers (1)

Gary Russell
Gary Russell

Reputation: 174534

Save off the original payload in a header and restore it later...

<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']" />

...

Notice you need to use a gateway, not a channel adapter (JDBC).

You can put all this in a <chain/> if you wish.

Upvotes: 2

Related Questions