Spring Batch Tutorial
Spring Batch Tutorial
Audience
This tutorial is particularly going to be useful for all those professionals who are required to
process large volumes of records involving repetitive actions such as transaction management,
job processing statistics, resource management, etc. Spring Batch is a very effective framework
for processing high-volume batch jobs.
Prerequisites
Spring Batch has been built upon Spring Framework, therefore you should have prior exposure
to the features and functions of Spring. In case you are not familiar with Spring Framework,
then you can start here.
All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt.
Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any
contents or a part of contents of this e-book in any manner without written consent of the
publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd.
provides no guarantee regarding the accuracy, timeliness or completeness of our website or its
contents including this tutorial. If you discover any errors on our website or in this tutorial,
please notify us at contact@tutorialspoint.com
i
Spring Batch
Table of Contents
About the Tutorial ..................................................................................................................................... i
Audience.................................................................................................................................................... i
Prerequisites ............................................................................................................................................. i
Step ......................................................................................................................................................... 16
Chunk...................................................................................................................................................... 16
JobRepository ....................................................................................................................................... 17
JobLauncher .......................................................................................................................................... 17
TransactionManager ............................................................................................................................. 18
DataSource ............................................................................................................................................. 18
ii
Spring Batch
ItemWriter ............................................................................................................................................... 21
Context.xml ............................................................................................................................................ 24
Tasklet.java ............................................................................................................................................ 24
App.java .................................................................................................................................................. 25
Context.xml ............................................................................................................................................ 30
CustomItemProcessor.java .................................................................................................................. 32
TutorialFieldSetMapper.java ................................................................................................................ 34
Tutorial.java ........................................................................................................................................... 35
App.java .................................................................................................................................................. 37
Context.xml ............................................................................................................................................ 43
CustomItemProcessor.java .................................................................................................................. 44
TutorialFieldSetMapper.java ................................................................................................................ 44
App.java .................................................................................................................................................. 47
iii
Spring Batch
Context.xml ............................................................................................................................................ 53
CustomItemProcessor.java .................................................................................................................. 54
TutorialRowMapper.java ....................................................................................................................... 55
Tutorial.java ........................................................................................................................................... 55
App.java .................................................................................................................................................. 57
Context.xml ............................................................................................................................................ 63
CustomItemProcessor.java .................................................................................................................. 64
TutorialRowMapper.java ....................................................................................................................... 65
Tutorial.java ........................................................................................................................................... 65
App.java .................................................................................................................................................. 67
iv
1. Spring Batch – Overview Spring Batch
Applications that deals with processing and validation of the data available in a
transactional manner.
You can also scale spring batch applications using its portioning techniques.
Flexibility: Spring Batch applications are flexible. You simply need to change an
XML file to alter the order of processing in an application.
Maintainability: Spring Batch applications are easy to maintain. A Spring Batch job
includes steps and each step can be decoupled, tested, and updated, without
effecting the other steps.
Scalability: Using the portioning techniques, you can scale the Spring Batch
applications. These techniques allow you to –
1
Spring Batch
Reliability: In case of any failure, you can restart the job from exactly where it was
stopped, by decoupling the steps.
Support for multiple file formats: Spring Batch provides support for a large set
of readers and writers such as XML, Flat file, CSV, MYSQL, Hibernate, JDBC, Mongo,
Neo4j, etc.
Multiple ways to launch a job: You can launch a Spring Batch job using web
applications, Java programs, Command Line, etc.
Tracking status and statistics during the batch execution and after completing the
batch processing.
Services such as logging, resource management, skip, and restarting the processing.
2
2. Spring Batch – Environment Spring Batch
In this chapter, we will explain how to set Spring Batch environment in Eclipse IDE. Before
proceeding with the installation, ensure that you have installed Eclipse in your system. If
not, download and install Eclipse in your system.
Step 1: Install Eclipse and open a New Project as shown in the following screenshot.
3
Spring Batch
4
Spring Batch
Step 3: Right-click on the project and convert it into a Maven project as shown below. Once
you convert it into Maven project, it will give you a Pom.xml where you need to mention
the required dependencies. Thereafter, the jar files of those will be automatically
downloaded into your project.
Step4: Now, in the pom.xml of the project, copy and paste the following content
(dependencies for spring batch application) and refresh the project.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint</groupId>
<artifactId>SpringBatchSample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringBatchExample</name>
5
Spring Batch
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.8</jdk.version>
<spring.version>4.3.8.RELEASE</spring.version>
<spring.batch.version>3.0.7.RELEASE</spring.batch.version>
<mysql.driver.version>5.1.25</mysql.driver.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
6
Spring Batch
<version>${mysql.driver.version}</version>
</dependency>
7
Spring Batch
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Finally, if you observe the Maven dependencies, you can observe that all the required jar
files have been downloaded.
8
3. Spring Batch ─ Architecture Spring Batch
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.
9
Spring Batch
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.
<job id="jobid">
<step id="step1" next="step2"/>
<step id="step2" next="step3"/>
<step id="step3"/>
</job>
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.
</job>
Step
A step is an independent part of a job which contains the necessary information to define
and execute the job (its part).
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:
10
Spring Batch
<job id="helloWorldJob">
<step id="step1">
<tasklet>
<chunk reader="mysqlReader" writer="fileWriter" processor="CustomitemProcessor" >
</chunk>
</tasklet>
</step>
</ job>
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.
<job-repository id="jobRepository"/>
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.
<job-repository id="jobRepository"
data-source="dataSource"
transaction-manager="transactionManager"
isolation-level-for-create="SERIALIZABLE"
table-prefix="BATCH_"
max-varchar-length="1000"/>
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.
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean
"> <property name="transactionManager" ref="transactionManager"/>
</bean>
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.
11
Spring Batch
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
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.
12
4. Spring Batch ─ Application Spring Batch
Almost all the examples in this tutorial contain the following files:
Configuration File
The configuration file (XML) contains the following:
In our examples, for better understanding, we have divided this in to two files the job.xml
file (defines job, step, reader and writer) and context.xml file (job launcher, job repository,
transaction manager and data source).
Mapper Class
The Mapper class, depending upon the reader, implements interfaces such as row mapper,
field set mapper, etc. It contains the code to get the data from the reader and to set it to
a Java class with setter and getter methods (Java Bean).
Tasklet/processor
The Tasklet/processor class contains the processing code of the Spring Batch application. A
processor is a class which accepts an object that contains the data read, processes it, and
returns the processed data (in the form object).
13
Spring Batch
Launcher class
This class (App.java) contains the code to launch the Spring Batch application.
14
5. Spring Batch ─ Configuration Spring Batch
While writing a Spring Batch application, we will configure the job, step, JobLauncher,
JobRepository, Transaction Manager, readers, and writers using the XML tags provided in
the Spring Batch namespace. Therefore, you need to include this namespace in your XML
file as shown below.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
In the following sections, we will discuss the various tags, their attributes and examples,
available in the Spring Batch namespace.
Job
This tag is used to define/configure the job of the SpringBatch. It contains a set of steps
and it can be launched using the JobLauncher.
Attribute Description
This is the attribute which is used to specify whether the job is restartable
restartable or not. This attribute is optional.
15
Spring Batch
Step
This tag is used to define/configure the steps of a SpringBatch job. It has the following three
attributes:
Attribute Description
It is used to specify the name of the parent bean from which the
parent
configuration should inherit.
<job id="jobid">
<step id="step1" next="step2"/>
<step id="step2" next="step3"/>
<step id="step3"/>
</job>
Chunk
This tag is used to define/configure a chunk of a tasklet. It has the following four attributes:
Attribute Description
It represents the name of the item reader bean. It accepts the value
reader
of the type org.springframework.batch.item.ItemReader.
It represents the name of the item reader bean. It accepts the value
writer
of the type org.springframework.batch.item.ItemWriter.
It represents the name of the item reader bean. It accepts the value
processor
of the type org.springframework.batch.item.ItemProcessor.
16
Spring Batch
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="xmlItemReader" writer="mysqlItemWriter"
processor="itemProcessor" commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
JobRepository
The JobRepository Bean is used to configure the JobRepository using a relational database. This bean
is associated with the class of type org.springframework.batch.core.repository.JobRepository.
Attribute Description
dataSource It is used to specify the bean name which defines the datasource.
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="mysql" />
</bean>
JobLauncher
The JobLauncher bean is used to configure the JobLauncher. It is associated with the class
org.springframework.batch.core.launch.support.SimpleJobLauncher (in our programs).
This bean has one property named jobrepository, and it is used to specify the name of the
bean which defines the jobrepository.
17
Spring Batch
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
TransactionManager
The TransactionManager bean is used to configure the TransactionManager using a
relational database. This bean is associated with the class of type
org.springframework.transaction.platform.TransactionManager.
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
DataSource
The datasource bean is used to configure the Datasource. This bean is associated with the
class of type org.springframework.jdbc.datasource.DriverManagerDataSource.
Attribute Description
driverClassName This specifies the class name of the driver used to connect with the database.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/details" />
<property name="username" value="myuser" />
<property name="password" value="password" />
</bean>
18
6. Spring Batch ─ Readers, Writers & Processors
Spring Batch
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. If the application reads n records the code in the processor will
be executed on each record.
A chunk is a child element of the tasklet. It is used to perform read, write, and processing
operations. We can configure reader, writer, and processors using this element, within a
step as shown below.
<batch:job id="helloWorldJob">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="cvsFileItemReader" writer="xmlItemWriter"
processor="itemProcessor" commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
Spring Batch provides readers and writers to read and write data form various file
systems/databases such as MongoDB, Neo4j, MySQL, XML, flatfile, CSV, etc.
To include a reader in your application, you need to define a bean for that reader, provide
values to all the required properties within the bean, and pass the id of such bean as a
value to the attribute of the chunk element reader (same for writer).
ItemReader
It is the entity of a step (of a batch process) which reads data. An ItemReader reads one
item a time. Spring Batch provides an Interface ItemReader. All the readers implement
this interface.
Following are some of the predefined ItemReader classes provided by Spring Batch to read
from various sources.
Reader Purpose
19
Spring Batch
<bean id="mysqlItemWriter"
class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" value="file:xml/outputs/userss.xml" />
<property name="marshaller" ref="reportMarshaller" />
<property name="rootTagName" value="Tutorial" />
</bean>
<bean id="reportMarshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>Tutorial</value>
</list>
</property>
</bean>
As observed, while configuring, we need to specify the respective class name of the required
reader and we need to provide values to all the required properties.
20
Spring Batch
ItemWriter
It is the element of the step of a batch process which writes data. An ItemWriter writes one
item a time. Spring Batch provides an Interface ItemWriter. All the writers implement this
interface.
Following are some of the predefined ItemWriter classes provided by Spring Batch to read
from various sources.
Writer Purpose
In same way, we need to configure the ItemWriters by creating the beans. Following is an
example of JdbcCursorItemReader which writes data to an MySQL database.
<bean id="dbItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="select * from tutorialsdata" />
<property name="rowMapper">
<bean class="TutorialRowMapper" />
</property>
</bean>
Item Processor
ItemProcessor: An ItemProcessor is used to process the data. When the given item is not
valid it returns null, else it processes the given item and returns the processed result. The
interface ItemProcessor<I,O> represents the processor.
Tasklet class: When no reader and writer are given, a Tasklet acts as a processor for
SpringBatch. It processes only single task.
21
Spring Batch
In a batch process, if "n" records or data elements are read, then for each record, it will
read the data, process it, and writes the data in the writer. To process the data, it relays on
the processor passed.
For example, let’s suppose you have written code to load a particular PDF document, create
a new page, write the data item on to the PDF in a tabular format. If you execute this
application, it reads all the data items from the XML document, stores them in the MySQL
database, and prints them in the given PDF document in individual pages.
Example
Following is a sample ItemProcessor class.
import org.springframework.batch.item.ItemProcessor;
@Override
public Tutorial process(Tutorial item) throws Exception {
System.out.println("Processing..." + item);
return item;
}
}
22
7. Spring Batch ─ Basic Application
Spring Batch
This chapter shows you the basic Spring Batch application. It will simply execute a tasklet
to displays a message.
Configuration file: This is an XML file where we define the Job and the steps of the
job. (If the application involves readers and writers too, then the configuration of
readers and writers is also included in this file.)
Context.xml: In this file, we will define the beans like job repository, job launcher
and transaction manager.
Tasklet class: In this class, we will write the processing code job (In this case, it
displays a simple message)
Launcher class: in this class, we will launch the Batch Application by running the
Job launcher.
jobConfig.xml
Following is the configuration file of our sample Spring Batch application.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
<import resource="context.xml" />
<!-- Defining a bean -->
<bean id="tasklet" class="a_sample.MyTasklet" />
<!-- Defining a job-->
<batch:job id="helloWorldJob">
<!-- Defining a Step -->
<batch:step id="step1">
<tasklet ref="tasklet"/>
</batch:step>
</batch:job>
</beans>
23
Spring Batch
Context.xml
Following is the context.xml of our Spring Batch application.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
Tasklet.java
Following is the Tasklet class which displays a simple message.
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
24
Spring Batch
App.java
Following is the code which launces the batch process.
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// System.out.println("hello");
String[] springConfig = {"a_sample/job_hello_world.xml"};
25
Spring Batch
On executing, the above SpringBatch program will produce the following output –
26
8. Spring Batch ─ XML to MySQLSpring Batch
In this chapter, we will create a Spring Batch application which uses an XML Reader and a
MySQL Writer.
Reader: The reader we are using in the application is StaxEventItemReader to read data
from XML documents.
Following is the input XML document we are using in this application. This document holds
data records which specify details like tutorial id, tutorial author, tutorial title, submission
date, tutorial icon, and tutorial description.
<tutorial>
<tutorial_id>1002</tutorial_id>
<tutorial_author>Abdul S</tutorial_author>
<tutorial_title>Learn MySQL</tutorial_title>
<submission_date>19-04-2007</submission_date>
<tutorial_icon>https://www.tutorialspoint.com/mysql/images/mysql-mini-
logo.jpg</tutorial_icon>
<tutorial_description>MySQL is the most popular Open Source Relational SQL
database management system. MySQL is one of the best RDBMS being used for
developing web-based software applications. This tutorial will give you quick
start with MySQL and make you comfortable with MySQL
programming.</tutorial_description>
</tutorial>
<tutorial>
27
Spring Batch
<tutorial_id>1003</tutorial_id>
<tutorial_author>Krishna Kasyap</tutorial_author>
<tutorial_title>Learn JavaFX</tutorial_title>
<submission_date>06-07-2017</submission_date>
<tutorial_icon>https://www.tutorialspoint.com/javafx/images/javafx-mini-
logo.jpg</tutorial_icon>
<tutorial_description>JavaFX is a Java library used to build Rich Internet
Applications. The applications developed using JavaFX can run on various devices
such as Desktop Computers, Mobile Phones, TVs, Tablets, etc. This tutorial,
discusses all the necessary elements of JavaFX that are required to develop
effective Rich Internet Applications</tutorial_description>
</tutorial>
</tutorials>
Writer: The writer we are using in the application is JdbcBatchItemWriter to write the
data to MySQL database. Assume we have created a table in MySQL inside a database called
"details".
Processor: The processor we are using in the application is a custom processor which writes
the data of each record on the PDF document.
In batch process, if "n" records or data elements were read, then for each record, it will
read the data, process it, and write the data in the Writer. To process the data, it relays
on the processor passed. In this case, in the custom processor class, we have written code
to load a particular PDF document, create a new page, write the data item onto the PDF in
a tabular format.
Finally, if you execute this application, it reads all the data items from the XML document,
stores them in the MySQL database, and prints them in the given PDF document in individual
pages.
28
Spring Batch
jobConfig.xml
Following is the configuration file of our sample Spring Batch application. In this file, we will
define the Job and the steps. In addition to these, we also define the beans for ItemReader,
ItemProcessor, and ItemWriter. (Here, we associate them with their respective classes and
pass the values for the required properties to configure them.)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd ">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="xmlItemReader"
class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="fragmentRootElementName" value="tutorial" />
<property name="resource" value="classpath:resources/tutorial.xml" />
<property name="unmarshaller" ref="customUnMarshaller" />
</bean>
<bean id="customUnMarshaller"
class="org.springframework.oxm.xstream.XStreamMarshaller">
29
Spring Batch
<property name="aliases">
<util:map id="aliases">
<entry key="tutorial" value="Tutorial" />
</util:map>
</property>
</bean>
<bean id="mysqlItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[insert into details.tutorials (tutorial_id, tutorial_author,
tutorial_title, submission_date, tutorial_icon, tutorial_description) values
(:tutorial_id, :tutorial_author, :tutorial_title, :submission_date, :tutorial_icon, :tutorial_d
escription);]]>
</value>
</property>
<property name="itemSqlParameterSourceProvider">
<bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourc
eProvider" />
</property>
</bean>
</beans>
Context.xml
Following is the context.xml of our Spring Batch application. In this file, we will define the
beans like job repository, job launcher, and transaction manager.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
30
Spring Batch
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="mysql" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionMana
ger" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
31
Spring Batch
CustomItemProcessor.java
Following is the processor class. In this class, we write the code of processing in the
application. Here, we are loading a PDF document, creating a new page, creating a table,
and inserting the following values for each record: tutorial id, tutorial name, author, date of
submission in the table.
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.springframework.batch.item.ItemProcessor;
32
Spring Batch
contentStream.drawLine(nextx,y,nextx,y-tableHeight);
nextx += colWidth;
}
texty-=rowHeight;
textx = margin+cellMargin;
}
}
@Override
public Tutorial process(Tutorial item) throws Exception {
System.out.println("Processing..." + item);
33
Spring Batch
{"Title", item.getTutorial_title()},
{"Authour", item.getTutorial_author()},
{"Submission Date", item.getSubmission_date()}} ;
contentStream.close();
doc.save("C:/Examples/test.pdf" );
System.out.println("Hello");
return item;
}
}
TutorialFieldSetMapper.java
Following is the ReportFieldSetMapper class which sets the data to the Tutorial class.
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
34
Spring Batch
Tutorial.java
Following is the Tutorial class. It is a simple class with setter and getter methods.
@Override
public String toString() {
return " [id=" + tutorial_id + ", author=" + tutorial_author
+ ", title=" + tutorial_title + ", date=" + submission_date
+ ", icon =" +tutorial_icon +", description = "+tutorial_description+"]";
}
35
Spring Batch
36
Spring Batch
App.java
Following is the code which launces the batch process. In this class, we will launch the Batch
Application by running the JobLauncher.
37
Spring Batch
38
Spring Batch
If you verify the details.tutorial table in the database, it will show you the following output.
tutorial_
tutorial_ tutorial_ tutorial_ submission_ tutorial_
description
id author title date icon
Java is a high-level
programming language
originally developed by
https://www.
Sun Microsystems and
Learn tutorialspoint.com
1001 Sanjay 06-05-2007 released in 1995. Java
Java /java/images/java-
runs on a variety of
mini-logo.jpg
platforms. This tutorial
gives a complete
understanding of Java.
MySQL is the most popular
Open Source Relational
SQL database
management system.
https://www.
MySQL is one of the best
tutorialspoint.com
Learn RDBMS being used for
1002 Abdul S 19-04-2007 /mysql/images
MySQL developing web-based
/mysql-mini-
software applications. This
logo.jpg
tutorial will give you quick
start with MySQL and
make you comfortable with
MySQL programming
JavaFX is a Java library
used to build Rich Internet
Applications. The
applications developed
using JavaFX can run on
https://www.
various devices such as
tutorialspoint.com
Learn Krishna Desktop Computers,
1003 06-07-2017 /javafx/images/
JavaFX Kasyap Mobile Phones, TVs,
javafx-mini-
Tablets, etc. This tutorial,
logo.jpg
discusses all the necessary
elements of JavaFX that
are required to develop
effective Rich Internet
Applications
This will generate a PDF with the records on each page as shown below.
39
Spring Batch
40
9. Spring Batch ─ CSV to XML Spring Batch
In this chapter, we will create a simple Spring Batch application which uses a CSV Reader
and an XML Writer.
Reader: The reader we are using in the application is FlatFileItemReader to read data
from the CSV files.
Following is the input CSV file we are using in this application. This document holds data
records which specify details like tutorial id, tutorial author, tutorial title, submission date,
tutorial icon and tutorial description.
Writer: The Writer we are using in the application is StaxEventItemWriter to write the
data to XML file.
Processor: The Processor we are using in the application is a custom processor which just
prints the records read from the CSV file.
jobConfig.xml
Following is the configuration file of our sample Spring Batch application. In this file, we will
define the Job and the steps. In addition to these, we also define the beans for ItemReader,
ItemProcessor, and ItemWriter. (Here, we associate them with respective classes and pass
the values for the required properties to configure them.)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<batch:job id="helloWorldJob">
41
Spring Batch
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="cvsFileItemReader" writer="xmlItemWriter"
processor="itemProcessor" commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="cvsFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader">
<bean id="xmlItemWriter"
class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" value="file:xml/outputs/tutorials.xml" />
<property name="marshaller" ref="reportMarshaller" />
<property name="rootTagName" value="tutorials" />
</bean>
<bean id="reportMarshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
42
Spring Batch
<property name="classesToBeBound">
<list>
<value>Tutorial</value>
</list>
</property>
</bean>
</beans>
Context.xml
Following is the context.xml of our Spring Batch application. In this file, we will define the
beans like job repository, job launcher, and transaction manager.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
43
Spring Batch
CustomItemProcessor.java
Following is the Processor class. In this class, we write the code of processing in the
application. Here, we are printing the contents of each record.
import org.springframework.batch.item.ItemProcessor;
@Override
public Tutorial process(Tutorial item) throws Exception {
System.out.println("Processing..." + item);
return item;
}
}
TutorialFieldSetMapper.java
Following is the TutorialFieldSetMapper class which sets the data to the Tutorial class.
44
Spring Batch
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
@Override
public Tutorial mapFieldSet(FieldSet fieldSet) throws BindException {
return tutorial;
}
}
Tutorial.java class
Following is the Tutorial class. It is a simple Java class with setter and getter methods.
In this class, we are using annotations to associate the methods of this class with the tags
of the XML file.
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "tutorial")
public class Tutorial {
45
Spring Batch
@XmlAttribute(name = "tutorial_id")
public int getTutorial_id() {
return tutorial_id;
}
@XmlElement(name = "tutorial_author")
public String getTutorial_author() {
return tutorial_author;
}
@XmlElement(name = "tutorial_title")
public String getTutorial_title() {
return tutorial_title;
}
@XmlElement(name = "submission_date")
public String getSubmission_date() {
return submission_date;
}
46
Spring Batch
}
@Override
public String toString() {
return " [Tutorial id=" + tutorial_id + ", Tutorial Author=" +
tutorial_author + ", Tutorial Title=" + tutorial_title + ", Submission Date=" +
submission_date + "]";
}
}
App.java
Following is the code which launches the batch process. In this class, we will launch the
batch application by running the JobLauncher.
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
47
Spring Batch
48
Spring Batch
<tutorial tutorial_id="1002">
<submission_date>19/04/2007</submission_date>
<tutorial_author>Abdul S</tutorial_author>
<tutorial_title>Learn MySQL</tutorial_title>
</tutorial>
<tutorial tutorial_id="1003">
<submission_date>06/07/2017</submission_date>
49
Spring Batch
<tutorial_author>Krishna Kasyap</tutorial_author>
<tutorial_title>Learn JavaFX</tutorial_title>
</tutorial>
</tutorials>
50
10. Spring Batch ─ MySQL to XMLSpring Batch
In this chapter, we will create a Spring Batch application which uses a MySQL reader and
an XML Writer.
Writer: The Writer we are using in the application is StaxEventItemWriter to write the
data to the XML file.
Processor: The Processor we are using in the application is a custom processor which just
prints the records read from the CSV file.
jobConfig.xml
Following is the configuration file of our sample Spring Batch application. In this file, we will
define the Job and the Steps. In addition to these, we also define the beans for ItemReader,
ItemProcessor, and ItemWriter. (Here, we associate them with their respective classes and
pass the values for the required properties to configure them.)
51
Spring Batch
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<batch:job id="helloWorldJob">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="dbItemReader" writer="mysqlItemWriter"
processor="itemProcessor" commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="dbItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="select * from tutorials_data" />
<property name="rowMapper">
<bean class="TutorialRowMapper" />
</property>
</bean>
<bean id="mysqlItemWriter"
class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" value="file:xml/outputs/tutorials.xml" />
<property name="marshaller" ref="reportMarshaller" />
52
Spring Batch
<bean id="reportMarshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>Tutorial</value>
</list>
</property>
</bean>
</beans>
Context.xml
Following is the context.xml of our Spring Batch application. In this file, we will define the
beans like job repository, job launcher, and transaction manager.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd ">
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionMana
ger" />
<bean id="jobLauncher"
53
Spring Batch
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
CustomItemProcessor.java
Following is the Processor class. In this class, we write the code of processing in the
application. Here, we are printing the contents of each record.
import org.springframework.batch.item.ItemProcessor;
@Override
public Tutorial process(Tutorial item) throws Exception {
System.out.println("Processing..." + item);
return item;
}
}
54
Spring Batch
TutorialRowMapper.java
Following is the TutorialRowMapper class which sets the data to the Tutorial class.
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
@Override
public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException {
tutorial.setTutorial_id(rs.getInt("tutorial_id"));
tutorial.setTutorial_author(rs.getString("tutorial_author"));
tutorial.setTutorial_title(rs.getString("tutorial_title"));
tutorial.setSubmission_date(rs.getString("submission_date"));
return tutorial;
}
}
Tutorial.java
Following is the Tutorial class. It is a simple Java class with setter and getter methods.
In this class, we are using annotations to associate the methods of this class with the tags
of the XML file.
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "details")
public class Tutorial {
int tutorial_id;
String tutorial_author;
55
Spring Batch
String tutorial_title;
String submission_date;
@XmlAttribute(name = "tutorial_id")
public int getTutorial_id() {
return tutorial_id;
}
@XmlElement(name = "tutorial_author")
public String getTutorial_author() {
return tutorial_author;
}
@XmlElement(name = "tutorial_title")
public String getTutorial_title() {
return tutorial_title;
}
@XmlElement(name = "submission_date")
public String getSubmission_date() {
return submission_date;
}
56
Spring Batch
App.java
Following is the code which launces the batch process. In this class, we will launch the Batch
Application by running the JobLauncher.
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
57
Spring Batch
58
Spring Batch
59
Spring Batch
<details tutorial_id="102">
<submission_date>19-04-2007</submission_date>
<tutorial_author>Abdul S</tutorial_author>
<tutorial_title>Learn MySQL</tutorial_title>
</details>
<details tutorial_id="103">
<submission_date>06-07-2017</submission_date>
<tutorial_author>Krishna Kasyap</tutorial_author>
<tutorial_title>Learn JavaFX</tutorial_title>
</details>
</Tutorial>
60
11. Spring Batch ─ MySQL to Flat File
Spring Batch
In this chapter, we will create a Spring Batch application which uses an MySQL Reader and
a Flatfile Writer (.txt ).
Writer: The Writer we are using in the application is FlatFileItemWriter to write the data
to flatfile (.txt).
Processor: The Processor we are using in the application is a custom processor which just
prints the records read from the CSV file.
61
Spring Batch
jobConfig.xml
Following is the configuration file of our sample Spring Batch application. In this file, we will
define the Job and the Steps. In addition to these, we also define the beans for ItemReader,
ItemProcessor, and ItemWriter. (Here, we associate them with respective classes and pass
the values for the required properties to configure them.)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<batch:job id="helloWorldJob">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="mysqlItemReader"
writer="flatFileItemWriter" processor="itemProcessor" commit-interval="10">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="mysqlItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader" >
<property name="dataSource" ref="dataSource" />
<property name="sql" value="select * from details.tutorialsdata" />
<property name="rowMapper">
<bean class="TutorialRowMapper" />
</property>
</bean>
62
Spring Batch
Context.xml
Following is the context.xml of our Spring Batch application. In this file, we will define the
beans like job repository, job launcher, and transaction manager.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd ">
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="mysql" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
63
Spring Batch
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
CustomItemProcessor.java
Following is the Processor class. In this class, we write the code of processing in the
application. Here, we are printing the contents of each record.
import org.springframework.batch.item.ItemProcessor;
@Override
public Tutorial process(Tutorial item) throws Exception {
System.out.println("Processing..." + item);
return item;
}
}
64
Spring Batch
TutorialRowMapper.java
Following is the TutorialRowMapper class which sets the data to the Tutorial class.
@Override
public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException {
tutorial.setTutorial_id(rs.getInt("tutorial_id"));
tutorial.setTutorial_title(rs.getString("tutorial_title"));
tutorial.setTutorial_author(rs.getString("tutorial_author"));
tutorial.setSubmission_date(rs.getString("submission_date"));
return tutorial;
}
}
Tutorial.java
Following is the Tutorial class. It is a simple Java class with setter and getter methods.
In this class, we are using annotations to associate the methods of this class with the tags
of the XML file.
65
Spring Batch
@Override
public String toString() {
return " [id=" + tutorial_id + ", title=" + tutorial_title
+ ", author=" + tutorial_author + ", date=" + submission_date +
"]";
}
}
66
Spring Batch
App.java
Following is the code which launces the batch process. In this class, we will launch the Batch
Application by running the JobLauncher.
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
67
Spring Batch
68
Spring Batch
69