Batch Jobs with Spring Batch

Title: Batch Jobs with Spring Batch Labels: Java, Java-Data -->

Two Black Fridays ago, a retailer I know ran its nightly revenue reconciliation as a hand-rolled Java loop: read every order from a CSV export, recompute totals, write a summary row. At 3:12 AM the loop died on a single corrupt row — a customer field containing a raw newline that broke the naive line split. The on-call engineer re-ran the job. It started from row one again, re-processed the 40,000 rows it had already written, and double-counted the evening's revenue in the report. Finance spotted it at 9 AM; the "fix" was a day of manual spreadsheet surgery and a very awkward email.

The loop had three missing pieces, and they are exactly the three things Spring Batch gives you: a restart point (resume where you died, not from row one), transaction boundaries (a failed chunk leaves no half-written output), and observability (which rows were read, written, committed, skipped — recorded in tables you can query). This post builds the nightly order reconciliation for our checkout service with Spring Batch, runs it for real, kills it mid-way, and restarts it.

Why hand-rolled batch loops fail

Here is the shape of the loop that caused the incident above — an honest sketch of what teams write before they meet a batch framework:

// The anti-pattern: one giant implicit transaction (or none), no restart point.
List<String> lines = Files.readAllLines(export);   // 40k rows in memory, hope for the best
for (String line : lines) {
    Order o = parse(line);                          // one bad row throws here...
    ReconciliationResult r = reconcile(o);
    report.append(r.toLine());                       // ...after 39,999 appends already happened
}
// Die at row 39,999? Re-run starts at row 0. Written rows get written twice.

Three defects, all production-shaped:

  • No restart point. The loop's only state is "how far the for-loop got," which dies with the JVM. Every re-run redoes completed work — and if the writer isn't idempotent, completed work gets duplicated.
  • No transaction boundaries. Either each row commits alone (slow, and a crash mid-row leaves a torn write) or nothing commits until the end (a crash loses everything). There is no middle ground without explicit chunking.
  • No observability. When it dies at 3 AM, the only evidence is a stack trace. How many rows were processed? Which chunk failed? Was the report half-written? Nobody knows without re-running.

Spring Batch's answer is a small, opinionated model: a Job is the whole nightly run; a Step is one phase of it; a chunk-oriented step reads, processes, and writes items in chunks — each chunk its own transaction; and a JobRepository persists every step's progress so a failed job can resume exactly where it stopped.

The Job / Step / Chunk model

For our nightly order reconciliation: one job, one step. The step reads the day's orders from a CSV export, validates each order's total, and appends one line per order to a reconciliation report. The chunk size is 3 — three items per transaction — so the mechanics are visible on a 12-row demo file:

Job nightlyOrderReconciliation Step reconcileOrders chunk-oriented, chunk size = 3 ItemReader FlatFileItemReader reads 3 orders from the CSV export ItemProcessor validate totals OK or MISMATCH per order; may throw ItemWriter report file appends 3 lines commit — one transaction covers the whole chunk of 3 read.count is checkpointed next chunk — until input exhausted JobRepository (H2 database) BATCH_JOB_INSTANCE / BATCH_JOB_EXECUTION / BATCH_STEP_EXECUTION — records status, read count, write count, commit count and skip count per chunk. A restart resumes from the last committed chunk, not from row one.

Decision rule: keep the reader dumb, the processor pure, and the writer boring. The reader moves bytes into objects. The processor holds the business rule (here: does the exported total match what we expected?) and nothing else — no I/O, no database calls. The writer persists results and nothing else. When a step misbehaves at 3 AM, this split tells you which third to blame in under a minute.

The worked job: nightly order reconciliation

Everything below is real code in package com.javamakeuse.batch, compiled with javac on Java 21 against Spring Batch 5.2.6 + Spring Framework 6.2.19 + H2 2.5.252 (jars pulled from Maven Central with curl — coordinates at the end of the post). The input is a 12-row orders CSV; row 7 is deliberately poisoned with an unparseable customer field so we can watch the job fail and restart.

The domain objects are plain beans — FlatFileItemReader populates them via setters:

package com.javamakeuse.batch;

import java.math.BigDecimal;

public class Order {
    private String orderId;
    private String customer;
    private BigDecimal amount;
    private BigDecimal expectedTotal;

    public Order() { }
    public String getOrderId() { return orderId; }
    public void setOrderId(String orderId) { this.orderId = orderId; }
    public String getCustomer() { return customer; }
    public void setCustomer(String customer) { this.customer = customer; }
    public BigDecimal getAmount() { return amount; }
    public void setAmount(BigDecimal amount) { this.amount = amount; }
    public BigDecimal getExpectedTotal() { return expectedTotal; }
    public void setExpectedTotal = expectedTotal; }
}

The processor's output type — one reconciled line carrying what the export said versus what we expected:

package com.javamakeuse.batch;

import java.math.BigDecimal;

/** One reconciled line: what the export said vs what we expected. */
public class ReconciliationResult {

    private final String orderId;
    private final String customer;
    private final BigDecimal amount;
    private final BigDecimal expectedTotal;
    private final BigDecimal difference;
    private final String status; // OK or MISMATCH

    public ReconciliationResult(String orderId, String customer, BigDecimal amount,
                                BigDecimal expectedTotal, BigDecimal difference, String status) {
        this.orderId = orderId;
        this.customer = customer;
        this.amount = amount;
        this.expectedTotal = expectedTotal;
        this.difference = difference;
        this.status = status;
    }

    public String toReportLine() {
        return String.format("%-9s %-26s amount=%8s expected=%8s diff=%8s status=%s",
                orderId, customer, amount, expectedTotal, difference, status);
    }
}

The processor is where the reconciliation rule lives — and where the poison row detonates:

package com.javamakeuse.batch;

import java.math.BigDecimal;
import org.springframework.batch.item.ItemProcessor;

public class ReconciliationProcessor implements ItemProcessor<Order, ReconciliationResult> {

    @Override
    public ReconciliationResult process(Order order) {
        if ("POISON".equals(order.getCustomer())) {
            throw new IllegalStateException(
                    "corrupt feed row: order " + order.getOrderId()
                    + " has an unparseable customer field");
        }
        BigDecimal diff = order.getAmount().subtract(order.getExpectedTotal());
        String status = diff.compareTo(BigDecimal.ZERO) == 0 ? "OK" : "MISMATCH";
        return new ReconciliationResult(order.getOrderId(), order.getCustomer(),
                order.getAmount(), order.getExpectedTotal(), diff, status);
    }
}

The writer appends report lines. One honest caveat, stated in the code's own comment: a file is not transactional, so this writer is append-only and the launcher truncates the report once per job instance — never per restart. Committed chunks are never re-read on restart, so appending stays correct:

package com.javamakeuse.batch;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;

public class ReportItemWriter implements ItemWriter<ReconciliationResult> {

    private final Path reportFile = Path.of("/tmp/batch-demo/recon-report.txt");

    @Override
    public void write(Chunk<? extends ReconciliationResult> chunk) {
        try {
            StringBuilder sb = new StringBuilder();
            for (ReconciliationResult r : chunk) {
                sb.append(r.toReportLine()).append(System.lineSeparator());
            }
            Files.writeString(reportFile, sb.toString(), StandardCharsets.UTF_8,
                    StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        } catch (IOException e) {
            throw new UncheckedIOException("cannot write reconciliation report", e);
        }
    }
}

Decision rule: if your writer's target isn't transactional (a file, an email, a webhook), make the write idempotent or append-only-per-committed-chunk, and never truncate-on-restart. Otherwise a restart re-writes what the first attempt already committed — the double-counting bug from the opening story, wearing a framework costume.

Wiring: reader, step, job, and the JobRepository

All of these beans live in one @Configuration class, ReconBatchConfig. Its shell, imports, and the launcher bean the runner looks up:

package com.javamakeuse.batch;

import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.TaskExecutorJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@EnableBatchProcessing // registers the step/job scopes; we wire the rest ourselves
public class ReconBatchConfig {

    // ... the @Bean methods below live here ...

    @Bean
    public JobLauncher jobLauncher(JobRepository jobRepository) throws Exception {
        TaskExecutorJobLauncher launcher = new TaskExecutorJobLauncher();
        launcher.setJobRepository(jobRepository);
        launcher.afterPropertiesSet();
        return launcher;
    }

    @Bean
    public ItemProcessor reconciliationProcessor() {
        return new ReconciliationProcessor();
    }

    @Bean
    public ItemWriter reportWriter() {
        return new ReportItemWriter();
    }
}

The reader is @StepScoped so each step execution gets its own instance, with the input file injected from job parameters (more on parameters below):

@Bean
@StepScope
public FlatFileItemReader<Order> orderReader(
        @Value("#{jobParameters['inputFile']}") Resource inputFile) {
    BeanWrapperFieldSetMapper<Order> mapper = new BeanWrapperFieldSetMapper<>();
    mapper.setTargetType(Order.class);
    return new FlatFileItemReaderBuilder<Order>()
            .name("orderReader")
            .resource(inputFile)
            .linesToSkip(1) // header row
            .delimited()
            .names("orderId", "customer", "amount", "expectedTotal")
            .fieldSetMapper(mapper)
            .build();
}

The step is one line of intent — chunk of 3, this reader, this processor, this writer — and the job is one step. The chunk size is the transaction boundary: each committed chunk is one database transaction for the step's own bookkeeping, and the read.count checkpoint is saved with it:

@Bean
public Step reconcileStep(JobRepository jobRepository,
                          PlatformTransactionManager transactionManager,
                          FlatFileItemReader<Order> orderReader,
                          ItemProcessor<Order, ReconciliationResult> reconciliationProcessor,
                          ItemWriter<ReconciliationResult> reportWriter) {
    return new StepBuilder("reconcileOrders", jobRepository)
            .<Order, ReconciliationResult>chunk(3, transactionManager)
            .reader(orderReader)
            .processor(reconciliationProcessor)
            .writer(reportWriter)
            .build();
}

@Bean
public Job nightlyOrderReconciliation(JobRepository jobRepository,
                                      @Qualifier("reconcileStep") Step reconcileStep) {
    return new JobBuilder("nightlyOrderReconciliation", jobRepository)
            .start(reconcileStep)
            .build();
}

The JobRepository is the piece most tutorials gloss over and the piece that makes restart possible. It needs a real DataSource — restart state must survive the JVM dying, so an in-memory database is a lie here. (The DataSource mechanics — pools, why the repository needs its own transaction manager — are this track's Spring companion: "JDBC First: Connections, Pools & Transactions",.) Ours is H2 on a file, with Spring Batch's own schema-h2.sql creating the BATCH_* tables on first launch:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:file:/tmp/batch-demo/db/recon");
    ds.setUsername("sa");
    ds.setPassword("");
    return ds;
}

@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}

@Bean
public JobRepository jobRepository(DataSource dataSource,
                                   PlatformTransactionManager transactionManager) throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource);
    factory.setTransactionManager(transactionManager);
    factory.afterPropertiesSet();
    return factory.getObject();
}

Decision rule: the JobRepository always gets a durable DataSource — a file or server database, never an in-memory one in production. Restartability that evaporates with the process is not restartability. (Chunk transactions themselves are the same PlatformTransactionManager story as "Transactions: @Transactional, Isolation & Propagation", — each chunk commits or rolls back as one unit.)

The runner that launches the job from a main() — it boots the Spring context, creates the batch metadata tables on first run, and prints the BATCH_* metadata after every execution:

package com.javamakeuse.batch;

import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ScriptUtils;

/**
 * Launches the reconciliation job. Usage:
 *   java ... ReconJobRunner &lt;jobBeanName&gt; &lt;inputFile&gt; &lt;runId&gt; &lt;truncateReport&gt;
 *
 * jobBeanName: nightlyOrderReconciliation | nightlyOrderReconciliationSkip
 * Re-running with the SAME runId resumes the same job instance (restart);
 * a different runId starts a brand-new instance.
 */
public class ReconJobRunner {

    private static final Path REPORT = Path.of("/tmp/batch-demo/recon-report.txt");

    public static void main(String[] args) throws Exception {
        String jobName = args[0];
        String inputFile = args[1];
        String runId = args[2];
        boolean truncateReport = Boolean.parseBoolean(args[3]);

        try (AnnotationConfigApplicationContext ctx =
                     new AnnotationConfigApplicationContext(ReconBatchConfig.class)) {

            DataSource dataSource = ctx.getBean(DataSource.class);
            createBatchSchemaIfMissing(dataSource);

            if (truncateReport) {
                Files.deleteIfExists(REPORT);
            }

            JobLauncher launcher = ctx.getBean(JobLauncher.class);
            Job job = ctx.getBean(jobName, Job.class);

            JobParameters params = new JobParametersBuilder()
                    .addString("inputFile", inputFile)
                    .addString("runId", runId, true) // identifying: same runId => same job instance
                    .toJobParameters();

            System.out.println(">>> launching job '" + jobName + "' with runId=" + runId);
            JobExecution execution = launcher.run(job, params);
            System.out.println(">>> job finished with status: " + execution.getStatus());

            printJobMetadata(dataSource, jobName);
        }
    }

    /** Spring Batch does not create its own tables — run schema-h2.sql once. */
    private static void createBatchSchemaIfMissing(DataSource dataSource) throws Exception {
        JdbcTemplate jdbc = new JdbcTemplate(dataSource);
        Integer tables = jdbc.queryForObject(
                "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'BATCH_JOB_INSTANCE'",
                Integer.class);
        if (tables != null && tables > 0) {
            return;
        }
        System.out.println(">>> creating Spring Batch metadata tables (first run only)");
        try (Connection con = dataSource.getConnection()) {
            ScriptUtils.executeSqlScript(con,
                    new ClassPathResource("org/springframework/batch/core/schema-h2.sql"));
        }
    }

    /** The JobRepository IS the observability: instances, executions, step counters. */
    private static void printJobMetadata(DataSource dataSource, String jobName) {
        JdbcTemplate jdbc = new JdbcTemplate(dataSource);
        System.out.println("--- BATCH_JOB_INSTANCE / BATCH_JOB_EXECUTION for " + jobName + " ---");
        List<Map<String, Object>> rows = jdbc.queryForList(
                "SELECT i.JOB_INSTANCE_ID AS instance, e.JOB_EXECUTION_ID AS execution,"
                        + " e.STATUS AS status, e.EXIT_CODE AS exit_code,"
                        + " s.STEP_NAME AS step, s.READ_COUNT AS read, s.WRITE_COUNT AS written,"
                        + " s.COMMIT_COUNT AS commits, s.PROCESS_SKIP_COUNT AS skipped"
                        + " FROM BATCH_JOB_INSTANCE i"
                        + " JOIN BATCH_JOB_EXECUTION e ON e.JOB_INSTANCE_ID = i.JOB_INSTANCE_ID"
                        + " LEFT JOIN BATCH_STEP_EXECUTION s ON s.JOB_EXECUTION_ID = e.JOB_EXECUTION_ID"
                        + " WHERE i.JOB_NAME = ? ORDER BY instance, execution",
                jobName);
        for (Map<String, Object> row : rows) {
            System.out.println("instance=" + row.get("INSTANCE")
                    + " execution=" + row.get("EXECUTION")
                    + " status=" + row.get("STATUS")
                    + " exit=" + row.get("EXIT_CODE")
                    + " step=" + row.get("STEP")
                    + " read=" + row.get("READ")
                    + " written=" + row.get("WRITTEN")
                    + " commits=" + row.get("COMMITS")
                    + " skipped=" + row.get("SKIPPED"));
        }
    }
}

Run 1: the poison row kills the job

The launcher builds job parameters, runs the job, and then queries the BATCH_* tables — the JobRepository is the observability:

JobParameters params = new JobParametersBuilder()
        .addString("inputFile", "file:/tmp/batch-demo/orders-poison.csv")
        .addString("runId", "run-2026-10-05", true) // identifying: same values => same job instance
        .toJobParameters();
JobExecution execution = launcher.run(job, params);

Genuine output of the first run (log noise trimmed, nothing else changed):

>>> launching job 'nightlyOrderReconciliation' with runId=run-2026-10-05
[main] ERROR org.springframework.batch.core.step.AbstractStep - Encountered an error executing step reconcileOrders in job nightlyOrderReconciliation
java.lang.IllegalStateException: corrupt feed row: order ORD-1007 has an unparseable customer field
[main] INFO org.springframework.batch.core.launch.support.TaskExecutorJobLauncher - Job: [SimpleJob: [name=nightlyOrderReconciliation]] completed with the following parameters: [...] and the following status: [FAILED] in 2s920ms
>>> job finished with status: FAILED
--- BATCH_JOB_INSTANCE / BATCH_JOB_EXECUTION for nightlyOrderReconciliation ---
instance=1 execution=1 status=FAILED exit=FAILED step=reconcileOrders read=9 written=6 commits=2 skipped=0

Read that metadata line like an incident report: read=9 (two full chunks plus the three reads of the doomed third chunk — the reader pulls the whole chunk before processing starts), written=6, commits=2. Two chunks committed; the third died on ORD-1007. The report file holds exactly 6 lines — the two committed chunks, including a MISMATCH on ORD-1003 (210.00 vs 200.00) that the reconciliation correctly flagged:

ORD-1001  alice@example.com   amount=  120.00 expected=  120.00 diff=    0.00 status=OK
ORD-1002  bob@example.com     amount=   89.50 expected=   89.50 diff=    0.00 status=OK
ORD-1003  carol@example.com   amount=  210.00 expected=  200.00 diff=   10.00 status=MISMATCH
ORD-1004  dave@example.com    amount=   45.99 expected=   45.99 diff=    0.00 status=OK
ORD-1005  eve@example.com     amount=  300.00 expected=  300.00 diff=    0.00 status=OK
ORD-1006  frank@example.com   amount=   77.25 expected=   77.25 diff=    0.00 status=OK

Run 2: restart resumes from the last committed chunk

Operations fixes the one bad row in the same file (POISON becomes a valid customer) and relaunches with the identical parameters. Same identifying parameters means the same job instance — and a FAILED instance is restartable, so Spring Batch picks up the saved read.count of 6 and the reader skips straight to row 7. Genuine output:

>>> launching job 'nightlyOrderReconciliation' with runId=run-2026-10-05
[main] INFO org.springframework.batch.core.launch.support.TaskExecutorJobLauncher - Job: [SimpleJob: [name=nightlyOrderReconciliation]] completed with the following parameters: [...] and the following status: [COMPLETED] in 3s550ms
>>> job finished with status: COMPLETED
--- BATCH_JOB_INSTANCE / BATCH_JOB_EXECUTION for nightlyOrderReconciliation ---
instance=1 execution=1 status=FAILED exit=FAILED step=reconcileOrders read=9 written=6 commits=2 skipped=0
instance=1 execution=2 status=COMPLETED exit=COMPLETED step=reconcileOrders read=6 written=6 commits=3 skipped=0

The money line is instance=1 execution=2: the same job instance, a second execution, and it read only 6 rows — the ones after the checkpoint. The report now holds exactly 12 lines with zero duplicates (verified with sort | uniq -d), the resumed run having appended rows 7–12 after rows 1–6, including the two remaining mismatches (ORD-1009: 10.00 vs 12.00; ORD-1012: 88.88 vs 80.00).

Decision rule: restart is a property of the job instance, not of your code. Relaunch with the same identifying parameters and Batch resumes; change any identifying parameter and you get a brand-new instance that starts from row one. Name your parameters so this is obvious — runId, businessDate — and treat "same parameters, new instance" as a bug.

Two caveats worth knowing before you trust this in production. First, on restart the reader re-opens the same file and fast-forwards to the saved read count — so the rows before the checkpoint must not have shifted. If the feed file is regenerated with rows in a different order between attempts, the checkpoint points at the wrong row. Second, this demo has one step; with ten steps, a failure in step 3 leaves steps 1–2 COMPLETED and the restart re-runs step 3 onward — each step's checkpoint is independent, which is exactly the semantics you want.

Job parameters and job instances, precisely

The table above shows the whole model. A job instance is the logical run — "the reconciliation for 2026-10-05" — identified by the job name plus its identifying job parameters. A job execution is one physical attempt at that instance. The rules that matter:

  • The same identifying parameters twice = the same instance. If its last execution failed, the new execution resumes. If its last execution completed, launching again throws JobInstanceAlreadyCompleteException — Batch refuses to silently re-run a finished reconciliation, which is a guardrail against the double-counting bug from the opening story.
  • Non-identifying parameters (the third argument to addString is false) are carried along for the run but don't affect identity — use them for things like an operator's name or a debug flag.
  • @StepScope + #{jobParameters['inputFile']} is how a step learns which file tonight's run should read. Late binding like this is why the same job definition can reconcile any night's export.

Skip and retry: when failure is data, not disaster

Restarting is for failures you fix and re-run. But some failures shouldn't kill the night at all. A single corrupt row in a 40,000-row feed is bad data — skip it, count it, and reconcile the other 39,999. A deadlocked database write is transient — retry it a few times before giving up. Spring Batch separates the two with a fault-tolerant step:

@Bean
public Step reconcileStepWithSkip(JobRepository jobRepository,
                                  PlatformTransactionManager transactionManager,
                                  FlatFileItemReader<Order> orderReader,
                                  ItemProcessor<Order, ReconciliationResult> reconciliationProcessor,
                                  ItemWriter<ReconciliationResult> reportWriter) {
    return new StepBuilder("reconcileOrdersSkip", jobRepository)
            .<Order, ReconciliationResult>chunk(3, transactionManager)
            .reader(orderReader)
            .processor(reconciliationProcessor)
            .writer(reportWriter)
            .faultTolerant()
            .skip(IllegalStateException.class) // corrupt rows: skip, don't die
            .skipLimit(3)                      // ...but more than 3 means the feed is broken: fail
            .build();
}

@Bean
public Job nightlyOrderReconciliationSkip(JobRepository jobRepository,
                                          @Qualifier("reconcileStepWithSkip") Step reconcileStepWithSkip) {
    return new JobBuilder("nightlyOrderReconciliationSkip", jobRepository)
            .start(reconcileStepWithSkip)
            .build();
}

The retry twin, for transient failures (this variant was compiled against the same jars; the skip job below is the one that ran):

.faultTolerant()
.retry(IllegalStateException.class)   // transient failure: try the chunk again
.retryLimit(3)                        // up to 3 attempts, then the step fails
.skip(IllegalArgumentException.class) // bad data: skip it instead
.skipLimit(10)

Running the skip variant against the original poisoned file — one pass, no restart needed — genuine output:

>>> launching job 'nightlyOrderReconciliationSkip' with runId=skip-demo-2026-10-05
[main] INFO org.springframework.batch.core.launch.support.TaskExecutorJobLauncher - Job: [SimpleJob: [name=nightlyOrderReconciliationSkip]] completed with the following parameters: [...] and the following status: [COMPLETED] in 2s159ms
>>> job finished with status: COMPLETED
--- BATCH_JOB_INSTANCE / BATCH_JOB_EXECUTION for nightlyOrderReconciliationSkip ---
instance=2 execution=3 status=COMPLETED exit=COMPLETED step=reconcileOrdersSkip read=12 written=11 commits=5 skipped=1

skipped=1, written=11, COMPLETED: the poison row was quarantined and counted, the other eleven reconciled. The report has 11 lines — every row except ORD-1007.

Decision rule: skip is for bad data (the row is wrong; trying again won't help), retry is for transient failures (the row is fine; the world glitched — a deadlock, a socket timeout, a briefly unavailable downstream). Skip with a limit, always: an unbounded skip policy turns a corrupted feed into a silently empty reconciliation. And never retry a non-idempotent write — retrying a chunk that already appended to a file is how you get the double-counting bug back.

What I ran, exactly

Java 21 (javac 21.0.12.1), no Maven CLI and no Spring Boot — plain javac/java against jars downloaded from Maven Central with curl. Mutually compatible versions, taken from Spring Batch 5.2.6's own POM: org.springframework.batch:spring-batch-core:5.2.6, spring-batch-infrastructure:5.2.6, Spring Framework 6.2.19 (spring-core, spring-beans, spring-context, spring-tx, spring-jdbc, spring-aop, spring-expression, spring-jcl), spring-retry:2.0.13, micrometer-core/micrometer-observation/micrometer-commons:1.14.14, com.h2database:h2:2.5.252, slf4j-api/slf4j-simple:2.0.16. (Dependency coordinates and version alignment are covered in this track's companion "Maven vs Gradle: Builds Demystified",.) Every class and method used above was verified to exist in those jars; all three runs' outputs are verbatim from the runs, with only repetitive log lines trimmed.

What's next

Batch is for work with an end: the file is finite, the night ends, the report gets written. But the checkout service also produces a stream that never ends — orders, payments, inventory events flowing all day. The next post in this track, "Streaming Basics: Kafka Streams Concepts for Java Devs", moves from the nightly file to the endless stream: what changes when there is no "last row," and which of tonight's ideas — checkpoints, idempotent writes, exactly-once thinking — survive the trip.

Field check before you move on: take the reconciliation job and poison two rows in different chunks. Run it strict: confirm it fails on the first poison row with only earlier chunks committed. Fix only the first poison row, restart with the same parameters, and confirm it resumes past the checkpoint and then fails on the second poison row — the JobRepository should show one instance with three executions (FAILED → FAILED → COMPLETED after you fix the second row). Then add a skip policy and watch all of it complete in a single execution. If you can predict the read/written/commits/skipped numbers before each run, you understand chunk processing.

Continue: Java Learning Roadmap 2026

Comments

Popular posts from this blog

JSP Servlet Interview Questions For Freshers Series 1

What are the core classes and interfaces of hibernate 4

Java Banking Finance Services and Insurance (BFSI) domain interview questions