Spring Batch Documentation
repository·main·Indexed 25 days ago
https://github.com/spring-projects/spring-batchA lightweight, comprehensive batch framework for developing robust enterprise batch applications. It leverages the Spring Framework's POJO-based development approach to provide access to advanced enterprise services. Key features include fault-tolerant steps with retry and skip logic, chunk scanning behavior, and integration with Micrometer, Prometheus, and Grafana for metrics. The framework supports various adapters like ItemReaderAdapter and Tasklet adapters, as well as AMQP-based remote chunking.
What's inside Spring Batch
- The Delimited File Import Job sample demonstrates a standard Spring Batch workflow: reading data from a delimited file (such as a CSV), processing that data, and writing the results to a different file.
Integrate Spring Batch with Spring Integration
mainSpring Batch Integration allows you to combine the batch processing capabilities of Spring Batch with the messaging and integration patterns of Spring Integration. This is useful for:
- Automating operations: Using messages to trigger job execution.
- Separation of concerns: Sending messages when a job completes or fails to trigger operational workflows (e.g., notifications, monitoring) independent of the application logic.
- Embedding messaging in jobs: Using messaging channels to read or write items during processing.
- Distributed workloads: Implementing remote partitioning and remote chunking to distribute work across multiple workers.
Understand Flat File types in Spring Batch
mainSpring Batch supports two primary types of flat file structures for bulk data interchange:
- Delimited files: Fields are separated by a specific delimiter (e.g., a comma in a CSV file).
- Fixed length files: Each field occupies a predefined, set number of characters.
When working with flat files, you must define the structure (the schema) ahead of time, as unlike XML, there is no universal standard like XSD for flat files.
Use AmqpItemReader and AmqpItemWriter for remote chunking
mainThis sample demonstrates how to use Spring Batch to write to anAmqpItemWriter. The implementation leveragesAmqpTemplateand is modeled after theJmsItemReader/ Writer patterns used for remote chunking. It includes anAmqpItemReaderandAmqpItemWriter(contributed by Chris Schaefer) to facilitate message-based batch processing via AMQP.Understand Spring Batch core concepts
mainTo build effective batch applications, familiarize yourself with these fundamental Spring Batch concepts:
- Step: The main unit of work. It initializes business logic and controls the transaction environment.
- Tasklet: A component implemented by a developer to process the specific business logic for a
Step. - Item: The smallest unit of complete data for processing (e.g., a file line, a database row, or an XML element).
- Logical Unit of Work (LUW): A single iteration of work performed by a job as it iterates through an input source.
- Commit Interval: The number of LUWs processed within a single transaction.
- Partitioning: A technique for splitting a job into multiple threads, where each thread processes a subset of the data. This can occur within a single JVM or across multiple JVMs in a clustered environment.
Understand the Football Statistics Loading Job Sample
mainThe Football Job is a sample batch process designed to load American Football statistics from CSV files into a database and then generate a summary report.
Job Workflow:
- playerLoad: Reads
player.csvand inserts player data into thePLAYERStable. - gameLoad: Reads
games.csvand inserts game performance statistics into theGAMEStable. - playerSummarization: Executes a SQL query to join
GAMESandPLAYERStables, then writes the summarized results into thePLAYER_SUMMARYtable.
This sample demonstrates handling multiple input types (flat files and databases) and performing data summarization, a common enterprise batch scenario.
- playerLoad: Reads
Understand Retry mechanism in Spring Batch v6.0+
mainSpring Batch uses the core retry feature provided by Spring Framework 7.0 to handle transient errors (e.g., network glitches during web service calls or
DeadlockLoserDataAccessExceptionduring database updates).Important Change: As of version 6.0, Spring Batch no longer uses the standalone
Spring Retrylibrary for automating retry operations within the framework; it is now integrated with the Spring Framework 7.0 resilience features.XML Input and Output with Spring OXM
mainThis sample demonstrates how to perform XML input and output operations using streaming and Spring OXM (Object/XML Mapping) marshallers and unmarshallers.
Specifically, it shows a job that copies
CustomerCreditdata from one XML file to another. The implementation uses XStream for object-to-XML conversion due to its simple configuration for basic use cases. For alternative OXM options, refer to the Spring OXM documentation.Common ItemReader implementations
mainSpring Batch provides several standard implementations of
ItemReaderfor different data sources:- Flat File: Reads lines from files where data is defined by fixed positions or delimited by special characters (e.g., commas).
- XML: Processes XML data independently of specific parsing technologies and allows for validation against an XSD schema.
- Database: Accesses database resources to return resultsets. These implementations typically use a
RowMapperto map rows to objects, track the current row for restartability, and provide transaction enhancements.
Understand Local Partitioning with PartitionHandler
mainThe Local Partitioning Sample demonstrates multi-threaded step execution using the
PartitionHandlerService Provider Interface (SPI). It specifically uses aTaskExecutorPartitionHandlerto distribute work across multiple threads, where each thread handles oneStepexecution.Key implementation details to note:
- Work Division: The work is divided using either a
MultiResourcePartitioner(XML version) or aColumnRangePartitioner(Java version). - Thread Safety: Readers and writers within the partitioned
Stepmust be step-scoped to ensure their state is not shared across different execution threads.
- Work Division: The work is divided using either a
Core interfaces for batch processing: ItemReader, ItemProcessor, and ItemWriter
mainSpring Batch simplifies bulk data processing into a three-step pattern: reading data, transforming it, and writing the result. To implement this pattern, you use three key interfaces:
ItemReader: Responsible for reading large amounts of data.ItemProcessor: Responsible for performing calculations or transformations on the data read.ItemWriter: Responsible for writing the processed results out.
Understand the concept of a Step in Spring Batch
mainA
Stepis a domain object that encapsulates an independent, sequential phase of a batch job. It contains all the information necessary to define and control the actual batch processing.Steps vary in complexity depending on the developer's requirements:
- Simple Steps: May involve basic tasks like loading data from a file into a database with minimal custom code.
- Complex Steps: May involve intricate business rules applied during the processing phase.