In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Summary of Spring-batch Framework Learning (1)
I. get to know the Spring-batch framework for the first time:
1. Core nouns explain:
Job: the core concept of the Spring-batch framework, which contains all the operations of batch processing
Step: each Job consists of one or more Step, and each Step involves itemReader,itemProcessor,ItemWriter. As the name implies, these three interfaces are responsible for data sources, business logic, and data output after processing.
JobRepository: when defining Job, you need to specify a JobRepository to store the state information of Job during operation. The reason for storing state information is: if Job fails, Spring supports rerunning from the failed place instead of starting from scratch.
JobLauncher: it is easy to understand that launchuer is used to execute Job. If it is not set, the system will configure the default Launcher for Job by default.
two。 To illustrate the relationship between nouns:
Classification of 3.Spring-Batch schema components:
Application (application layer): contains all batch jobs and custom code written by developers using Spring-batch
Batch Core (core layer): contains the core classes necessary to load and control batch jobs, which contains the implementation of Job,Step,JobLauncher
Infrastructure (infrastructure layer): the infrastructure layer contains Reader (ItemReader), Writer (ItemWriter), and Services can be used by the application layer and the core layer.
4. Diagram of the relationship between each layer:
Two. Create a simple Spring-batch project
1. Create a spring-batch project and import it into IDE
two。 Create a simple Job
(1) create a Configuration package
(2) create a Configuration class: add @ Configuration;@EnableBatchProcessing
(3) dependency @ Autowired:JobBuilderFactory;StepbuilderFactory used by injection
(4) Class display:
Package com.dhcc.batch.batchDemo.config;import org.springframework.batch.core.Job;import org.springframework.batch.core.Step;import org.springframework.batch.core.StepContribution;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.batch.core.scope.context.ChunkContext;import org.springframework.batch.core.step.tasklet.Tasklet Import org.springframework.batch.repeat.RepeatStatus;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;@Configuration @ EnableBatchProcessingpublic class JobConfiguration {@ Autowired private JobBuilderFactory jobBuilderFactory; @ Autowired private StepBuilderFactory stepBuilderFactory; @ Primary @ Bean public Job helloWord () {return jobBuilderFactory.get ("helloWordJob") .start (step1 ()) .build () } JobLauncher @ Bean public Step step1 () {return stepBuilderFactory.get ("step1") .tasklet (new Tasklet () {@ Override public RepeatStatus execute (StepContribution contribution, ChunkContext chunkContext) throws Exception {System.out.println ("Hello Spring Batch...."); return RepeatStatus.FINISHED;}}) .build ();}}
(5) Open BatchDemoApplication to start the server exception message:
Solution: database support is required to analyze the spring-batch runtime, and we do not connect to any databases, so we take the in-memory database H2 in spring as an example to configure it as follows:
Com.h3database h3
After the addition is complete, let's continue to start the project and view the console:
The project runs successfully, we see the console output information, we successfully create the Job and step, and print out the hello word Batch...
III. Construction of development environment
Premise: create the database we need in the MySQL database. The database I created is called springbatch.
1. Let's take MySQL database as an example, first add some dependencies of Jdbc and mysql database (remember to comment out H2 in-memory database dependencies)
Org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime
two。 Add configuration information under application.properties
Spring.datasource.url=jdbc:mysql://localhost:3306/springbatch?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=falsespring.datasource.username=rootspring.datasource.password=qitao1996spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sqlspring.batch.initialize-schema=always
After the configuration is complete, we start our project again and observe the console:
You can see that the project runs successfully, and now we look at our springbatch database in the database and we find that the database that didn't have any tables before running now has the following tables:
4. Summarize the important concepts in SpringBatch (learn more about one after two or three studies)
1.Job: is an interface that defines how a job is executed; JobInstance is a run of Job, which we can understand as an instance of Job; JobExceution is every execution of JobInstace, it records the status, attempts to execute according to the status, fails to execute, and continues to execute next time
2.JobInstance: it is an execution of job. A JobInstance can be executed repeatedly. If the last execution fails, the last failed job will be re-executed the next time. Each execution is a JobExceution.
3.JobParameters: can be used as a parameter to start Job, and can be used to identify different Job. The runtime is provided to JobInstance,jonExceution to decide whether to continue execution next time based on the status and parameters.
4.JobExceution: every time we try to execute a Job, we can call it a JobExceution. The result of this execution can be either a success or a failure. For example, a JobInstance execution fails, and the next time it executes, the parameter passed in by it is the time of the last execution, and it will continue to execute, so that one JobInstance is always executed, resulting in two JobExceution.
Legend:
5.Step: mainly divided into two parts
(1) Tasklet: the interface contains a unique method execute ()
(2) Chunk-based: process the following modules in Step one by one:
ItemReader: data input input: for an Step, read one entry at a time
ItemProcessor: data processing processing
ItemWriter: data output output: for a Step, output one item at a time according to the setting
6.StepExecution: for each attempt to execute a Step, a StepExection is created when a Step actually starts execution (as shown in the following illustration)
7.ExecutionContext: execution context, which represents a collection of key-value key-value pairs, which can be persisted by the Spring framework. Developers can store persistent state. Each JobExecution and each StepExecution execution corresponds to an execution context (ExecutionContext). For StepExecution, the execution context is saved every time a point is submitted, while for Job, it is saved between each StepExecution execution. For example, when we switch from Step1 to Step2, we save it.
8.JobRepository: this article will not be repeated.
9.JobLauncher: API, used to start and load Job, start according to the passed parameters, and return the case where Job is executed at once, as shown in the following API method
Public JobExecution run (Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException
Complete pom.xml is attached below
4.0.0 com.dhcc.batch batchDemo 0.0.1-SNAPSHOT jar batchDemo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-batch Org.springframework.boot spring-boot-starter-test test org.springframework.batch spring-batch-test test org.springframework.boot spring-boot-starter-jdbc mysql Mysql-connector-java runtime org.springframework.boot spring-boot-maven-plugin
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.