Quick revision
The short answer
Model the job as a flow: download, then validate, and from the validate step use transitions such as on("INVALID").to(notifyStep) and on("*").to(loadStep), then load and publish the summary. The validate step reports the business outcome as a custom ExitStatus set by a StepExecutionListener, or a JobExecutionDecider makes the choice, and every path ends explicitly with end(), fail() or stopAndRestart() so the job status says what happened. Each step must be restartable and idempotent, and steps share only small values through ExecutionContext promotion.
What to remember
In this answer, we designed a partner-file flow with download, validate, load and publish steps and a notify path for bad files. Transitions match on exit statuses, with the most specific pattern winning, so on("INVALID") routes to notifying the partner while on("*") continues to load. The validate step reports business outcomes through a StepExecutionListener, or a JobExecutionDecider makes the choice, and every path ends explicitly with end, fail or stopAndRestart. Steps write to instance-specific locations so they can be repeated, and they share only small values through ExecutionContext promotion.
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.
Model the job as a flow: download, then validate, and from the validate step use transitions such as on("INVALID").to(notifyStep) and on("*").to(loadStep), then load and publish the summary. The validate step reports the business outcome as a custom ExitStatus set by a StepExecutionListener, or a JobExecutionDecider makes the choice, and every path ends explicitly with end(), fail() or stopAndRestart() so the job status says what happened. Each step must be restartable and idempotent, and steps share only small values through ExecutionContext promotion.
Why this question is asked
Real batch jobs are rarely a straight line. A partner file can be missing, malformed or full of bad rows, and the business wants a different outcome for each: load and report when the file is good, tell the partner and stop when it is not. If we express that with exceptions alone, the job just shows FAILED and nobody knows whether it was a database outage or a bad file. Interviewers ask this to see whether we can design conditional flows that make the outcome visible and keep the job safe to restart. In this answer, we will recap step transitions, design the flow, produce business exit statuses, decide how each path ends, and make the steps restartable and able to pass small values along.
1A Quick Recap of Exit Statuses and Transitions
Every step execution ends with two values. BatchStatus is the framework's state, such as COMPLETED or FAILED, and Spring Batch uses it for restart rules. ExitStatus is a code describing how the step ended. By default it mirrors the batch status, but we can set our own code, such as INVALID or NO_FILE. Transitions in a job flow match on the exit status.
A transition is written as on(pattern).to(nextStep). The pattern can be an exact code, or use * to match any characters and ? to match one. When several patterns match, Spring Batch picks the most specific one, so on("INVALID") wins over on("*") whatever order we write them in. A path can also end the job with end(), fail(), or stopAndRestart(step).
2The Flow We Want
The partner file goes through five steps. downloadStep fetches the file from the partner's SFTP server. validateStep checks the header, the record count against the trailer, and each row's format. If the file is good, loadStep writes rows to the database and publishSummaryStep sends a summary. If the file is bad, notifyPartnerStep emails or calls the partner's API with the reasons, and the job stops.
- downloadStepfetch to /data/partner/acme/2026-11-02/
- validateStepheader, trailer count, row formats
- Exit status INVALID?YesnotifyPartnerStep, then end REJECTEDNoloadStep
- loadStepchunk-oriented, 1,000 rows per commit
- publishSummarySteprow counts from the job context
- Job COMPLETED
6 steps in order. Play them, or step through them one at a time.
Notice that a technical failure, such as the SFTP server timing out, is a different path from a business rejection. A timeout should fail the job so it can be restarted when the server is back; a bad file is a valid outcome that should be recorded as such.
3Writing the Flow in Java
Here is the job definition. We use from() to make each transition's source step explicit, which avoids surprises about which step a later on() applies to.
The last line of the flow ends the job with batch status COMPLETED and exit status REJECTED. That choice says the job did its work correctly: it found a bad file and told the partner. Operators see REJECTED on their dashboard and know it is not an outage. Because the instance is completed, a corrected file must be launched as a new instance, so the identifying parameters should include something that identifies the delivery, such as the file name or a delivery id, as well as the date.
4Producing a Business Exit Status
validateStep must turn its finding into the INVALID code. The cleanest way for a chunk step is a StepExecutionListener whose afterStep method looks at what the step counted and returns the exit status. For a tasklet, we can set it directly on the step contribution.
The alternative is a JobExecutionDecider. A decider is not a step; it is a small piece of code placed in the flow between steps that returns a FlowExecutionStatus based on the job or step execution. It is useful when the decision depends on information from several steps, or when we do not want the validate step's own status to carry routing logic. Its downside is that it leaves no StepExecution of its own, so the decision is less visible in the repository.
5Choosing How Each Path Ends
The three endings mean different things to operators and to restart. end() completes the job, and the instance can no longer be restarted. fail() marks it FAILED, and a restart resumes at the step where it ended. stopAndRestart(step) marks the job STOPPED and records that a restart should begin at the given step.
| Ending | Batch status | Restart | Use for |
|---|---|---|---|
end() or end("REJECTED") | COMPLETED | no, new instance needed | valid business outcome |
fail() | FAILED | yes, from the failing step | technical errors |
stopAndRestart(downloadStep) | STOPPED | yes, from downloadStep | wait for a corrected file |
For the rejected file, stopAndRestart(downloadStep) is a reasonable alternative to end("REJECTED"): the partner sends a corrected file, the operator restarts the same instance, and it downloads again. Which one we choose depends on whether the business sees a corrected file as the same delivery or a new one. The important thing is to choose explicitly rather than letting an exception decide.
6Restartable Steps and Passing Small Values
Each step should be safe to run again. downloadStep writes to a location specific to the instance, such as /data/partner/acme/2026-11-02/, and overwrites the file if it exists, so a repeated download does no harm. loadStep uses a restartable reader and an upsert, or deletes the instance's rows before loading, so a re-run does not create duplicates.
Steps often need small facts from earlier steps, such as the number of valid rows for the summary. Each step has its own ExecutionContext, so we promote selected keys to the job's ExecutionContext with an ExecutionContextPromotionListener when the step ends. Later steps read them from the job context. We keep this to small values; large data belongs in a table or file, because the context is serialised and stored in the repository at each commit.
7Follow-up questions
When is a JobExecutionDecider better than a step that sets an exit status?
A decider is better when the routing decision is not the natural result of one step's work: for example when it combines counts from two steps, checks the day of the week, or reads a feature flag. It keeps steps focused on their work and the routing in one visible place. A step that sets its own exit status is better when the outcome is exactly what the step discovered, as with validation, because the status is then stored in that step's execution and is easy to see in the repository and the logs.
What happens if no transition matches an exit status?
The job fails with an error saying that no transition matched. That is why we finish each step's transitions with a catch-all such as on("*"), or rely on the specific patterns covering every outcome the step can produce.
Should validation be a separate pass or part of the load?
For a partner file where a single bad row means rejecting the whole file, a separate pass is clearer: we never load half a file. If bad rows can be skipped and reported while good ones load, validation can live in the load step's processor with a skip policy, and the exit status can still report how many rows were skipped.
8Conclusion
In this answer, we designed a partner-file flow with download, validate, load and publish steps and a notify path for bad files. Transitions match on exit statuses, with the most specific pattern winning, so on("INVALID") routes to notifying the partner while on("*") continues to load. The validate step reports business outcomes through a StepExecutionListener, or a JobExecutionDecider makes the choice, and every path ends explicitly with end, fail or stopAndRestart. Steps write to instance-specific locations so they can be repeated, and they share only small values through ExecutionContext promotion.
Open with
I would build it as a flow job: download, validate, and then branch. The validate step ends with a business exit status, VALID or INVALID, which I set in a StepExecutionListener's afterStep based on what the step counted, while keeping FAILED for real technical errors.
Then, if they want more
In the job definition I use transitions from the validate step: on INVALID go to notifyPartnerStep, on FAILED fail the job, and on anything else go to loadStep and then publishSummaryStep. The most specific pattern wins, so the order of the lines does not matter. I would end every path explicitly.
After notifying the partner I would end with a custom status such as REJECTED, which is completed rather than failed, so operators can tell a bad file from an outage; or, if a corrected file should be the same instance, stopAndRestart at the download step. Technical failures use fail so the job can be restarted.
If the decision depended on several steps or a flag, I would use a JobExecutionDecider instead. Each step must be restartable and idempotent, so the download goes to a location specific to the partner and date and overwrites, and the load uses upserts.
Small values like row counts move between steps by promoting them to the job ExecutionContext, never large data.
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 Batch 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 "You need a batch flow that downloads a partner file, validates it, loads it, and then...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/12c32ad590/