Quick revision
The short answer
We build an IntegrationFlow that starts from an SFTP inbound streaming adapter with a poller, splits each file into lines with a streaming FileSplitter, filters invalid lines to an error channel through the filter's discard channel, transforms valid lines into records and publishes them with a Kafka outbound adapter in synchronous mode. For reliability we check that a persistent metadata store prevents reprocessing after a restart, that a file is marked done only after every record is published, that large files are streamed rather than loaded into memory, and that failures reach an error channel with logging, alerting and a quarantine location.
What to remember
In this answer, we built a Java DSL flow that streams CSV files from SFTP, splits them lazily into lines, filters invalid lines to a discard channel, parses the rest and publishes them to Kafka synchronously. For reliability we used a persistent metadata store so restarts do not reprocess files, a pseudo transaction so a file is moved to processed only after every record is published, streaming for large files, and an error channel with metrics, alerts and a quarantine directory.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We build an IntegrationFlow that starts from an SFTP inbound streaming adapter with a poller, splits each file into lines with a streaming FileSplitter, filters invalid lines to an error channel through the filter's discard channel, transforms valid lines into records and publishes them with a Kafka outbound adapter in synchronous mode. For reliability we check that a persistent metadata store prevents reprocessing after a restart, that a file is marked done only after every record is published, that large files are streamed rather than loaded into memory, and that failures reach an error channel with logging, alerting and a quarantine location.
Why this question is asked
File-based partner integrations are old technology, but they still carry orders, payments and stock levels in many companies, and they fail in ways that are hard to notice: a file processed twice, a file half processed, a file never processed. Interviewers ask this design question to see whether we can build the happy path quickly and then reason about what happens on a crash. In this answer, we will recap the pieces we need, build the flow with the Java DSL, and then go through each reliability concern: duplicates after restart, marking a file done, large files, and error handling.
1A Quick Recap of the Building Blocks
An IntegrationFlow is a chain of Spring Integration endpoints defined in Java code. Our flow needs an inbound channel adapter that polls the remote SFTP directory and produces a message per file, a splitter that turns one file message into one message per line, a filter that passes valid lines and sends invalid ones elsewhere, a transformer that parses a line into a record, and an outbound channel adapter that publishes each record to Kafka.
Two more pieces matter for reliability. A file list filter decides which remote files the adapter should pick up; a persistent one remembers files it has seen in a metadata store, a small key-value store such as a database table or Redis. And a poller can run each poll in a transaction, even a pseudo transaction with no database, so that we can act when a whole file succeeds or fails.
- Poll SFTP every 30 s*.csv, not seen before
- Stream the fileno local copy, no full load
- Split into linesiterator mode, one line at a time
- Line valid?YesParse into OrderRecordNoSend to invalidLines channel
- Publish to Kafkasync send, key = record id
- Whole file done?YesMove to /processedNoRoll back: retry or quarantine
6 steps in order. Play them, or step through them one at a time.
2The Flow in the Java DSL
Here is the flow. The exact class names follow Spring Integration 6 with its MINA SSHD-based SFTP support; check them against your version.
Notice that every channel between the steps is a DirectChannel, the DSL's default. That means the poller thread runs the whole file, line by line, through to Kafka before the poll ends, which is what lets one transaction cover one file. The invalidLines channel has its own small flow that writes each rejected line, with the file name and line number from the headers, to a reject file or table for the partner team.
3No Reprocessing After a Restart
The persistent accept-once filter records each file name and modification time in the metadata store as it accepts the file. After a restart, the adapter lists the directory again, the filter sees the file in the store and skips it. With an in-memory filter instead, every file still on the server would be processed again after each restart.
The store must be shared and durable: a JdbcMetadataStore on the service's database, or Redis, so that it survives restarts and is the same for every instance. If two instances poll the same directory, the shared store also stops both from taking the same file, because the filter's check-and-add is atomic in a ConcurrentMetadataStore.
4Marking a File Done Only After Every Record Is Published
There is a catch with the accept-once filter: it marks the file as seen when it is listed, before we process it. If the application crashes after publishing half the lines, the file is already in the store and would never be picked up again, silently losing the second half.
The pseudo transaction fixes this. Because the whole file runs in the poller's thread, the transaction commits only when the last line has been published, and the Kafka adapter in sync mode throws if any send fails, which rolls the transaction back. A transaction synchronization factory then acts on the outcome: after commit, move the remote file to /processed; after rollback, remove the file from the metadata store so the next poll retries it, or after a few failures move it to /quarantine. A retry may republish lines that already reached Kafka, so delivery is at-least-once, and the Kafka key, the record id, lets consumers detect duplicates.
But there is one more case: if the JVM itself dies, no rollback runs, and the file stays in the store while still sitting in the inbound directory. Because finished files are always moved away, a file that is in the store and still in the directory is unfinished, so a small check at startup, or an expiry on store entries older than an hour, makes it eligible again.
5Large Files and Error Handling
Partner files can be hundreds of megabytes. The streaming inbound adapter hands the flow an InputStream on the remote file instead of downloading it, and the FileSplitter in iterator mode reads one line at a time, so memory use stays flat whatever the file size. The splitter also closes the remote session when the stream is consumed. A synchronous Kafka send per line is slower than batching; if throughput matters, we can send asynchronously and wait for all futures before the transaction commits.
Every failure must be visible. The poller's errorChannel, partnerErrors, receives an ErrorMessage for any failure in the file's processing. Its flow logs the file name and cause, increments a Micrometer counter that drives an alert, and after repeated failures moves the file to the quarantine directory with a note for the partner team. Invalid lines go to their own channel and are counted too, because a sudden jump from 0.1 percent to 30 percent invalid lines usually means the partner changed the format.
6Follow-up questions
Why not download the file with a synchronizing adapter instead of streaming?
The synchronizing adapter copies each file to a local directory first, which is simple and lets us reprocess locally, but it needs disk space and leaves local files to clean up. Streaming avoids the copy. Both work; streaming suits large files and stateless containers.
What if the same file name is uploaded again with new content?
SftpPersistentAcceptOnceFileListFilter stores the modification time with the name, so a file with the same name but a newer timestamp is accepted again. If partners reuse names with the same timestamp, we include a checksum or move processed files away, which the done-handling already does.
How do we stop two instances from processing the same file?
The shared ConcurrentMetadataStore makes acceptance atomic, so only one instance accepts each file. Moving the file to /processed afterwards also removes it from later listings. Some teams instead run the poller on one leader instance using Spring Integration's leader election support.
7Conclusion
In this answer, we built a Java DSL flow that streams CSV files from SFTP, splits them lazily into lines, filters invalid lines to a discard channel, parses the rest and publishes them to Kafka synchronously. For reliability we used a persistent metadata store so restarts do not reprocess files, a pseudo transaction so a file is moved to processed only after every record is published, streaming for large files, and an error channel with metrics, alerts and a quarantine directory.
Open with
I'd build it as one IntegrationFlow. It starts from the SFTP inbound streaming adapter on the partner directory, polling every thirty seconds, one file per poll, with a chained file filter: a pattern filter for star dot csv and an SftpPersistentAcceptOnceFileListFilter backed by a JDBC or Redis metadata store.
Then, if they want more
Then a FileSplitter in iterator mode, so lines are read lazily, with the first line taken as a header. A filter checks each line and sends invalid ones to a discard channel that records them with the file name and line number.
A transformer parses valid lines into records, and a Kafka outbound adapter publishes them with the record id as key and sync set to true, so a failed send throws. For reliability, the persistent filter stops reprocessing after restarts and is shared between instances. But it marks the file when it's listed, so a crash midway would lose the rest.
So I'd run each poll in a pseudo transaction, which works because the whole flow is on direct channels in the poller thread. On commit, the file moves to processed; on rollback, it's removed from the store and retried, and after several failures quarantined. That makes it at-least-once, so consumers dedupe on the key.
Streaming keeps memory flat for large files, and the poller's error channel logs, counts and alerts.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Cloud Stream reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design a Spring Integration flow with the Java DSL that polls an SFTP directory for CSV...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud-stream/fedfb2ec33/