Spring Batch Hello World Example. 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 (csv, xml or database), “process” it and “write” it to other resources (csv, xml and database). For example, a step may read data from a CSV file, process it and write it into the database. Spring Batch provides many made Classes to read/write CSV, XML and database. For “single” operation task (tasklet), it means doing single task only, like clean up the resources after or before a step is started or completed. And the steps can be chained together to run as a job. Job = Many Steps.
What is PL SQL? PL SQL is a procedural language which has interactive SQL, as well as procedural programming language constructs like conditional branching. SQL Workbench/J can also be used from batch files to execute SQL scripts. This can be used to e.g. automatically extract data from a database or run other SQL.
Step = 1 READ- PROCESS- WRITE or 1 Tasklet. Job = {Step 1 - > Step 2 - > Step 3} (Chained together). Spring Batch Examples. Consider following batch jobs : Step 1 – Read CSV files from folder A, process, write it to folder B.
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.
READ- PROCESS- WRITE”Step 2 – Read CSV files from folder B, process, write it to the database. READ- PROCESS- WRITE”Step 3 – Delete the CSB files from folder B.
Tasklet”Step 4 – Read data from a database, process and generate statistic report in XML format, write it to folder C. READ- PROCESS- WRITE”Step 5 – Read the report and send it to manager email. Tasklet”In Spring Batch, we can declare like the following . Job" xmlns="http: //www. Item. Reader" writer="cvs. Item. Writer". processor="item.
Processer" commit- interval="1" />. Item. Reader" writer="database. Item. Writer". processor="item. Processer" commit- interval="1" />. Deleting. Tasklet" />. Item. Reader" writer="xml.
Item. Writer". processor="item. Processer" commit- interval="1" />. Email. Tasklet" />.
The entire jobs and steps execution are stored in database, which make the failed step is able to restart at where it was failed, no need start over the entire job. Tutorial. In this Spring Batch tutorial, we will show you how to create a job, read a CSV file, process it, write the output to an XML file. Tools and libraries used. Maven 3. Eclipse 4.
JDK 1. 6. Spring Core 3. RELEASESpring OXM 3. RELEASESpring JDBC 3. RELEASESpring Batch 2. RELEASE2. Project Directory. Review final project directory, a standard Maven project. Project Dependencies.
They must have dependencies are just Spring Core, Spring Batch and JDK 1. Read comments for self- explanatory.
POM/4. 0. 0". xmlns: xsi="http: //www. XMLSchema- instance". Location="http: //maven. POM/4. 0. 0. http: //maven. Version> 4. 0.
Version>. < group. Id> com. mkyong< /group.
Id>. < artifact. Id> Spring. Batch. Example< /artifact. Id>. < packaging> jar< /packaging>. SNAPSHOT< /version>.
Spring. Batch. Example< /name>. RELEASE< /spring. RELEASE< /spring.
Spring Core - ->. Id> org. springframework< /group. Id>. < artifact. Id> spring- core< /artifact. Id>. < version> ${spring. Spring jdbc, for database - ->. Id> org. springframework< /group.
Id>. < artifact. Id> spring- jdbc< /artifact. Id>. < version> ${spring. Spring XML to/back object - ->. Id> org. springframework< /group.
Id>. < artifact. Id> spring- oxm< /artifact. Id>. < version> ${spring. My. SQL database driver - ->.
Id> mysql< /group. Id>. < artifact. Id> mysql- connector- java< /artifact. Id>. < version> ${mysql. Spring Batch dependencies - ->.
Id> org. springframework. Id>. < artifact. Id> spring- batch- core< /artifact. Id>. < version> ${spring. Id> org. springframework. Id>. < artifact. Id> spring- batch- infrastructure< /artifact.
Id>. < version> ${spring. Spring Batch unit test - ->. Id> org. springframework. Id>. < artifact.
Id> spring- batch- test< /artifact. Id>. < version> ${spring. Junit - ->. < dependency>. Id> junit< /group. Id>. < artifact. Id> junit< /artifact.
Id>. < version> ${junit. Name> spring- batch< /final. Name>. < plugins>. Id> org. apache. Id>. < artifact. Id> maven- eclipse- plugin< /artifact.
Id>. < version> 2. Sources> true< /download. Sources>. < download. Javadocs> false< /download.
Javadocs>. < /configuration>. Id> org. apache. Id>. < artifact.
Id> maven- compiler- plugin< /artifact. Id>. < version> 2. Spring Batch Jobs.
A CSV file. report. A Spring batch job, to read above csv file with Flat.
File. Item. Reader, process the data with item. Processor and write it to an XML filewith Stax. Event. Item. Writer. XMLSchema- instance". Location="http: //www. Report" scope="prototype" />.
Processor" class="com. Custom. Item. Processor" />.
World. Job">. < batch: step id="step. File. Item. Reader" writer="xml. Item. Writer". processor="item.
Processor" commit- interval="1. File. Item. Reader" class="org. Flat. File. Item.
Reader">. < property name="resource" value="classpath: cvs/input/report. Mapper">. < bean class="org. Default. Line. Mapper">. Tokenizer">. class="org. Delimited. Line. Tokenizer">. Name,date" />. < /bean>.
Set. Mapper">. < bean class="com. Report. Field. Set. Mapper" />. < !- - if no data type conversion, use Bean.
Wrapper. Field. Set. Mapper to map by name. Bean. Wrapper. Field. Set. Mapper">. < property name="prototype. Bean. Name" value="report" />.
Item. Writer" class="org. Stax. Event. Item. Writer">. < property name="resource" value="file: xml/outputs/report.
Marshaller" />. Tag. Name" value="report" />. Marshaller" class="org. Jaxb. 2Marshaller">. To. Be. Bound">. Report< /value>.
Map CSV value to Report object and write it to XML file (via jaxb annotations). Report. java. package com. Big. Decimal. import java. Date. import javax. Xml. Attribute. import javax. Xml. Element. import javax. Xml. Root. Element.
Xml. Root. Element(name = "record"). Report {. private int id. Big. Decimal sales.
String staff. Name. Date date. @Xml. Attribute(name = "id"). Id() {. public void set. Id(int id) {. this. Xml. Element(name = "sales").
Big. Decimal get. Sales() {. return sales. Sales(Big. Decimal sales) {. Xml. Element(name = "qty"). Qty() {. public void set. Qty(int qty) {. this.
Xml. Element(name = "staff. Name"). public String get. Staff. Name() {. return staff. Name. public void set. Staff. Name(String staff. Name) {. this. staff. Name = staff. Name.
Date get. Date() {. Date(Date date) {. String to. String() {.
Report [id=" + id + ", sales=" + sales. Name=" + staff. Name + "]". To convert a Date, you need a custom Field.
Set. Mapper. If no data type conversion, just use Bean. Wrapper. Field. Set. Mapper to map the values by name automatically. Report. Field. Set. Mapper. java. package com. Parse. Exception. Simple. Date. Format.
Field. Set. Mapper. Field. Set. import org. Bind. Exception. import com.
Report. public class Report. Field. Set. Mapper implements Field.
Set. Mapper< Report> {. Simple. Date. Format date. Format = new Simple. Date. Format("dd/MM/yyyy"). Report map. Field. Set(Field. Set field.
Set) throws Bind. Exception {. Report report = new Report(). Id(field. Set. read. Int(0)). report. set.
Sales(field. Set. Big. Decimal(1)). Qty(field. Set. read. Int(2)). report. set. Staff. Name(field. Set. read. String(3)).
MM- dd. //field. Set. Date(4). String date = field. Set. read. String(4). Date(date. Format. Parse. Exception e) {. Stack. Trace(). return report.
A item. Processor will be fired before item. Writer. Custom. Item. Processor. java. package com. Item. Processor. import com. Report. public class Custom. Item. Processor implements Item.
Processor< Report, Report> {. Report process(Report item) throws Exception {. System. out. println("Processing.." + item). Spring context and database configuration. XMLSchema- instance".
Location=". http: //www. Repository". class="org. Map. Job. Repository. Factory. Bean">. Manager" ref="transaction. Manager" />. < !- - stored job- meta in database - ->. Repository". class="org.
Job. Repository. Factory. Bean">. < property name="data. Source" ref="data. Source" />. < property name="transaction.
Manager" ref="transaction. Manager" />. < property name="database. Type" value="mysql" />. Manager". class="org. Resourceless. Transaction.
Manager" />. < bean id="job. Launcher". class="org. Simple. Job. Launcher">. Repository" ref="job.
Repository" />. XMLSchema- instance". Location="http: //www.
My. SQL database - ->. Source". class="org.