Detailed introduction to spring batch tutorial (with source code)
Spring batch is an open source batch processing framework Perform a series of tasks In spring batch, a job consists of many steps. Each step consists of a read-process-write task or a single task.
1. "Read-process-write" processing can be understood according to the literal meaning:
For example, read data from CSV file and save it to database after processing Spring batch provides many classes to deal with this.
2. A single task, that is, processing a single task. For example, clear the resource file before or after a step is completed
3. Many steps together form a job So the relationship between them is like the following description:
A job = many steps, a step = a read-process-write or a task The same job = step1 -- > Step2 -- Step3 is composed of linked lists
Spring batch example
Consider the following example of batch processing, which seems a little fierce:
1. Step1: read the CSV file from folder a and write it to folder B after processing (read-process-write) 2 Step 2: read the CSV file from folder B, process it and store it in the database (read-process-write) 3. Step3: delete the CSV file in folder B. (using a single task) 4 Step 4: read the data from the database and generate an XML report file (read-process-write) after processing 5. Step5: read the XML report and send email to the administrator (using a single task)
Using spring batch, we can define this job as follows:
The execution of the entire job is stored in the database, so even if a step fails, it is not necessary to execute the job from scratch The following is a real introductory tutorial example
The jar package is as follows:
spring-batch-2.2. 3 or above, but I'm in 2.2 Org / springframework / batch / core / schema mysql.com was found in version 3 There is a problem with the MySQL statement to create a table in SQL, that is, the problem caused by the absence of "," (not null, and the comma is missing after the not null statement). Of course, you can modify it yourself and then execute it. After execution, there are the following tables:
xstream-1.3. Jar is required.
jettison-1.3. 3. Jar is also required, otherwise it will appear
java. Lang.noclassdeffounderror: org / codehaus / jettison / mapped / mappedxmloutputfactory error.
In addition, the spring I use is version 3.1. You can download the relevant jar packages and Apache common jar packages.
mysql-connect-java-5.1. Jar to connect to the MySQL database.
Suppose you want to read and process the following CSV file and write it into an XML file
Use flatfileitemreader to read CSV files, itemprocessor to process data, and staxeventitemwriter to write data
The definition of job is as follows (job-hello-world. XML):
Map CSV files to report objects and write XML files (via JAXB annotations)
In order to convert the date, a custom fieldsetmapper If no data needs to be converted, beanwrapperfieldsetmapper automatically maps the value by name.
Call itemProcessor to process data before writing data.
Spring configuration file and database configuration file
Run program
Operation results:
The result is output XML is in the XML directory of your project directory.
Download the entire source code after removing the jar package: Spring batch introductory tutorial Download