Why do we need batch processing?
- We don't always want to get the information we need right away, the batch allows us to start collecting information on an established process before the request is processed, such as a bank statement, which we can generate on a monthly basis and open a batch process before the user queries.
- Sometimes it can make the business better, for example, when shopping online, it is not that you buy a product retailer immediately delivery, but four or five hours after the unified delivery;
- better use of resources; Let the processing capacity that should be used idle is a big waste, we can customize the processing so that one machine one by one running job can better utilize the machine's processing power;
What is batch processing?
Batch processing refers to the data processing without interaction and interference with the user, and once started, a batch process should run to the end without any interference;
Why use spring Batch?
- maintainability, spring batch allows you to rely on the dependency injection that the spring framework advocates to decouple objects, and generates a robust series of test units with the test tools provided by spring, which minimizes your maintenance risk;
- Flexibility, using spring Batch, you can share code between systems, such as you can use services that have been tested and debugged in your Web application;
- extensibility, in a batch job with no transaction management, you only need to remember some principles, and with some simple configuration, the whole batch process has the ability to rollback and commit the transaction;
- The learning curve is low, and if you have experience using spring, you can use spring batch only with a small learning curve, because spring batch is built on the spring framework, and it follows spring's conventions, like other spring-based applications , using other features of spring;
- Active community support;
Background and composition of Spring batch
The Spring batch framework is a collaborative project between Accenture and SpringSource, which uses a standard-based approach to implement common batch patterns and paradigms, and it implements features such as data validation, output formatting, and a reusable way to implement complex business rules, Ability to handle large data sets;
Spring batch consists of 3 layers:
The first layer is the application layer, which includes all the custom code, all the configuration, business logic, and services for the batch process, and note that the application layer is not above the other two layers, but rather encapsulates the other two layers (the core layer and the underlying framework layer). The reason is that most of the custom code you develop in the application layer works with the core layer;
The application layer is most often interacting with the next (core) layer, which contains all the parts that define the "batch domain", the elements of the core components include the job and step interfaces, and all the interfaces that execute the job: Joblauncher and Jobparameter;
The bottom layer is the underlying framework layer, in order to do some processing, you need to read or write data from a file, a database, or to cope with a job failure, which is considered as a common basic framework, placed in the framework of the framework of the infrastructure components.
Note: The most common misconception of spring batch is that it is a scheduler, but there is no way in spring batch to start a job at a predetermined time or triggered by an event, which is done externally.
Use spring batch to define the job
Spring batch contains a number of domain-specific concepts, including job, a job is a process composed of many steps, when a step fails, the job may be rerun or may not be rerun, a job process may also be based on a condition to run ; We can define a job in a ApplicationContext file like this:
<BeanID= "Accounttasklet"class= "Com.thoughtworks.springBatchDemo.AccountTasklet"/><JobID= "Accountjob"> <StepID= "Accountstep"> <Taskletref= "Accounttasklet"/> </Step></Job>
Spring Batch Admin Project
Spring batch provides a number of administrative tools, such as starting or stopping a Spring batch job, viewing statistics about the last job run, and spring batch includes these features in a new plugin: Spring Batch Admin Project, which provides a web-based control center that allows you to control your batch process and monitor the performance of batch processing.
Spring Batch Learning (i) Introduction