- Spring Batch Flat File Item Writer Examples
- Spring Batch Flat File Item Writer Example
- Relational File
Item Reader - component that reads data from a specified data source. Common data sources include flat files, XML files, database tables, JMS etc. Item Writer - component that writes data to a target data source in chunks. Common data sources are the same as described in the item reader above. Spring Batch's built-in reader, org.springframework.batch.item.file.FlatFileItemReader, parses a file into individual lines. It requires a resource that references the flat file, the number of lines to skip at the beginning of the file (typically just the file headers), and a line. Spring Batch is a framework for batch processing – execution of a series of jobs. In Spring Batch, A job consists of many steps and each step consists of a READ-PROCESS-WRITE task or single operation task (tasklet). For “READ-PROCESS-WRITE” process, it means “read” data from the resources. Create a callback for writing header and footer information to a flat file or xml file. In the case of a flat file, we just need the string that should be written out. An Item Reader reads data into the spring batch application from a particular source, whereas an Item Writer writes data from Spring Batch application to a particular destination. An Item processor is a class which contains the processing code which processes the data read in to the spring batch.
- Spring Batch Tutorial
- Spring Batch Useful Resources
- Selected Reading
Following is the diagrammatic representation of the architecture of Spring Batch. As depicted in the figure, the architecture contains three main components namely, Application, Batch Core, and Batch Infrastructure.
Application − This component contains all the jobs and the code we write using the Spring Batch framework.
Batch Core − This component contains all the API classes that are needed to control and launch a Batch Job.
Batch Infrastructure − This component contains the readers, writers, and services used by both application and Batch core components.
Components of Spring Batch
The following illustration shows the different components of Spring Batch and how they are connected with each other.
Job
In a Spring Batch application, a job is the batch process that is to be executed. It runs from start to finish without interruption. This job is further divided into steps (or a job contains steps).
We will configure a job in Spring Batch using an XML file or a Java class. Following is the XML configuration of a Job in Spring Batch.
A Batch job is configured within the tags <job></job>. It has an attribute named id. Within these tags, we define the definition and ordering of the steps.
Restartable − In general, when a job is running and we try to start it again that is considered as restart and it will be started again. To avoid this, you need to set the restartable value to false as shown below.
Spring Batch Flat File Item Writer Examples
Step
A step is an independent part of a job which contains the necessary information to define and execute the job (its part).
As specified in the diagram, each step is composed of an ItemReader, ItemProcessor (optional) and an ItemWriter. A job may contain one or more steps.
Readers, Writers, and Processors
An item reader reads data into a Spring Batch application from a particular source, whereas an item writer writes data from the Spring Batch application to a particular destination.
An Item processor is a class which contains the processing code which processes the data read into the spring batch. If the application reads 'n' records, then the code in the processor will be executed on each record.
When no reader and writer are given, a tasklet acts as a processor for SpringBatch. It processes only a single task. For example, if we are writing a job with a simple step in it where we read data from MySQL database and process it and write it to a file (flat), then our step uses −
A reader which reads from MySQL database.
A writer which writes to a flat file.
A custom processor which processes the data as per our wish.
Spring Batch provides a long list of readers and writers. Using these predefined classes, we can define beans for them. We will discuss readers and writers in greater detail in the coming chapters.
JobRepository
A Job repository in Spring Batch provides Create, Retrieve, Update, and Delete (CRUD) operations for the JobLauncher, Job, and Step implementations. We will define a job repository in an XML file as shown below.
In addition to id, there are some more options (optional) available. Following is the configuration of job repository with all the options and their default values.
In-Memory Repository − In case you don’t want to persist the domain objects of the Spring Batch in the database, you can configure the in-memory version of the jobRepository as shown below.
JobLauncher
JobLauncher is an interface which launces the Spring Batch job with the given set of parameters. SampleJoblauncher is the class which implements the JobLauncher interface. Following is the configuration of the JobLauncher.
JobInstance
A JobIinstance represents the logical run of a job; it is created when we run a job. Each job instance is differentiated by the name of the job and the parameters passed to it while running.
If a JobInstance execution fails, the same JobInstance can be executed again. Hence, each JobInstance can have multiple job executions.
Spring Batch Flat File Item Writer Example
JobExecution and StepExecution
Relational File
JobExecution and StepExecution are the representation of the execution of a job/step. They contain the run information of the job/step such as start time (of job/step), end time (of job/step).