Spring Batch Study Notes

Source: Internet
Author: User

Compared with the traditional Batch Processing, although Spring Batch is quite different, in the final analysis, it is executed in sequence for some column tasks. You can use Spring Batch to read the content of a TXT file, and then put the processed data into the database, and vice versa.

The core components of Spring Batch Job processing include Job, Step, Tasklet, JobLuncher, and JobRepository. A Job is the work that needs to be completed during each batch processing. A Job can contain multiple steps, which can be executed sequentially or by configuring branching. Tasklet is a relatively independent task step, such as clearing the database and starting the Web server. JobLuncher is used to run a Job, while JobRepository is used to access the database of Spring Batch. We only need to configure JobRepository. As for database access operations, the Framework automatically completes.

Generally, batch processing is automatically started by sched, such as corn in Linux. For Spring Batch, we can use job planning tools such as Quartz and ControlM for timed start.

When starting a Job with JobLuncher, we need to provide JobParameters. JobParameters can provide some parameters to the Job, and can be used to calibrate the uniqueness of the Job. For example, for the former, you can pass the date parameter to the Job, and then use the date in the Job to query the data records of the day from the database; for the latter, if the parameters passed in when the same Job is executed twice, Spring Batch will complain that "an identical Job instance already exists" during the second execution ", then terminate the second Job execution. Therefore, when JobLuncher is used to execute the Job, we need to provide different JobParameter to ensure the uniqueness of each Job execution. Of course, JobParameters can contain multiple parameters. According to your own needs, some parameters can be consistent during Job execution before and after, but not all JobParameters can be the same. The following code demonstrates how to execute a Job:

 HashMap<String, JobParameter> parameters = new HashMap<String, JobParameter>();parameters.put("myParameter", new JobParameter("today"));JobParameters jobParameters = new JobParameters(parameters);jobLauncher.run(myJob, jobParameters);

  

For some common steps, we need to read some data before writing some data. Spring Batch provides ItemReader and ItemWriter. The configuration is as follows:

<job id="myJob" xmlns="http://www.springframework.org/schema/batch">       <step id="simpleTaskletStep" next="simplePrintStep">           <tasklet>               <chunk reader="itemReader" writer="itemWriter" commit-interval="2"/>           </tasklet>       </step></job>

 

The code above configures a Job named myJob, which contains a Step and configures an itemReader and itemWriter in the tasklet of this Step.

Both ItemReader and ItemWriter are interfaces. We need to define our own reader and writer to implement these two interfaces respectively. Of course, you can also use Spring Batch, such as JdbcCurcorItemReader.

The ItemReader and ItemWriter in a Tasklet work together. The basic principle is that the read method in ItemReader is called cyclically by the framework until the read method returns null. The object returned by read is automatically transmitted to the write method of ItemWriter. The read method of ItemReader generally returns a domain object defined by ourselves, and you will find that the write method of ItemWriter accepts a List instead of a single object, this is related to the commit-interval in the above configuration.

For example, for the above configuration, commit-interval = "2" indicates that the framework will first execute the read method of ItemReader twice, put the objects returned for the two return items into a List, and finally pass the List to ItemWriter. Then re-execute the read method twice and pass the obtained List to the write method. The read method returns null after repeated execution.

For examples of Spring Batch, refer to the author's Github.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.