Java › Standard

Restartable chunk processing in Jakarta and Spring Batch

Compare Jakarta Batch with Spring Batch 5, trace how checkpoints support restart, and choose chunk sizes, readers, and skip or retry policies.

Restartable chunk processing in Jakarta and Spring Batch

A batch job can finish thousands of rows before the process stops. I check the chunk boundary and the saved checkpoint to understand which records will run again when the job restarts.

The examples use Java 21, Jakarta Batch 2.1, and Spring Batch 5.x.

How a chunk-oriented job runs

Reading and writing an entire data set in one transaction increases memory use, lock duration, and the work a rollback must undo. Both Jakarta Batch and Spring Batch use jobs, steps, and chunk-oriented processing. A step reads items, optionally transforms or filters them, then writes a batch and commits. A step can also run one dedicated task without the chunk pipeline.

flowchart TD
    JOB["Job"] --> STEP["Step"]
    STEP --> READ["Read items"]
    READ --> PROCESS["Process or filter"]
    PROCESS --> WRITE["Write the chunk"]
    WRITE --> COMMIT["Commit and checkpoint"]

For the settlement example, the reader supplies orders, the processor builds settlement items, and the writer persists them. A transaction generally covers the chunk’s work. Choose where to do side effects so a restarted chunk can repeat safely.

ConcernJakarta Batch 2.1Spring Batch 5.x
Job definitionJSL XML, typically under META-INF/batch-jobs/Java configuration with JobBuilder and StepBuilder
Single activityBatchletTasklet
Execution and metadataJobOperator and runtime-managed dataJobLauncher and JobRepository
Dependency injectionJakarta CDI for named artifactsSpring-managed beans
Runtime examplesJBeret and Open LibertySpring Batch

The Jakarta processor in the example returns null to filter an order from the output. Filtering an item is distinct from a failure that counts against a skip policy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// src/main/java/com/example/batch/jakarta/OrderItemProcessor.java
package com.example.batch.jakarta;

import jakarta.batch.api.chunk.ItemProcessor;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Named;

@Dependent
@Named("orderItemProcessor")
public class OrderItemProcessor implements ItemProcessor {

    @Override
    public Object processItem(Object item) {
        OrderRecord record = (OrderRecord) item;
        if (record.amount() <= 0) {
            return null; // A null result filters the item out before writing.
        }
        return new Settlement(record.orderId(), record.amount());
    }
}

The Boot starter sets up Spring Batch for the Spring example. The step processes chunks of 500 and skips a bounded number of selected input errors. Boot’s auto-configuration backs off when the application takes control with @EnableBatchProcessing. Add the annotation only when you intend to configure that infrastructure.

1
2
3
4
5
6
// build.gradle.kts
dependencies {
    implementation("org.springframework.boot:spring-boot-starter-batch")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    runtimeOnly("com.h2database:h2")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// src/main/java/com/example/batch/spring/SettlementJobConfig.java
package com.example.batch.spring;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
public class SettlementJobConfig {

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

    @Bean
    public Step settlementStep(
            JobRepository jobRepository,
            PlatformTransactionManager transactionManager,
            ItemReader<OrderEntity> orderItemReader,
            ItemProcessor<OrderEntity, SettlementEntity> settlementProcessor,
            ItemWriter<SettlementEntity> settlementWriter
    ) {
        return new StepBuilder("settlementStep", jobRepository)
                .<OrderEntity, SettlementEntity>chunk(500, transactionManager)
                .reader(orderItemReader)
                .processor(settlementProcessor)
                .writer(settlementWriter)
                .faultTolerant()
                .skip(IllegalArgumentException.class)
                .skipLimit(100)
                .build();
    }
}

Checkpoints record the last committed chunk

A Jakarta reader can expose its position through checkpointInfo(). In Spring Batch, stream-aware readers and writers can store state in an ExecutionContext. The repository records the state alongside job and step execution metadata. With the same job instance and a restartable step, the framework can resume from the last persisted checkpoint.

flowchart TD
    A["Committed chunk"] --> B["Save reader position"]
    B --> C["Job repository"]
    FAIL["Process restarts"] --> C
    C --> RESUME["Resume at stored position"]

A crash before a chunk’s commit can make that chunk run again. Coordinate writer and metadata transactions, and make external effects repeatable if they can’t join the same boundary. A job with different identifying parameters may create a new job instance and begin from scratch.

Choosing chunk size and reader strategy

A larger chunk reduces transaction overhead, but it holds resources longer and repeats more items after a rollback. A smaller chunk limits repeat work and commits more often. I vary the chunk size while observing transaction duration, memory, lock contention, and throughput. Match the reader’s page size to the expected pattern and keep the sort order stable.

When writing changes which rows match the reader’s query, offset pagination can skip rows as the result set shrinks. Use a stable keyset with a unique sort key, or evaluate a suitable cursor reader for the database and transaction setup.

JPA processes many entities through a persistence context. A batch reader and writer need predictable flush and clear behavior so memory does not grow with the input. Configure skips for known bad records. Reserve retries for transient errors and limit them. Let unexpected failures stop the step and preserve the evidence needed to repair or restart it.

Verifying a job before rollout

Test a restart by interrupting a run after at least one committed chunk, then reusing the identifying job parameters. Compare the final output with one successful run and check that repeated inputs do not create duplicate settlements. Also exercise a malformed record, a transient write failure, and one skipped item beyond the configured limit. Use Jakarta Batch where deployment depends on its portable runtime model, and Spring Batch when the job is already part of the Spring ecosystem.

References

Release for Jakarta EE 10
Copyright © 2005 - var d = new Date(); document.write(d.getFullYear()); Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.Terms of Use • Privacy • Trademark Guidelines • Thank you • Your California Privacy Rights • Cookie Settings
This post is licensed under CC BY 4.0 by the author.