Table of contents
2. How to generate timed tasks (timed tasks that come with springboot)
1. Definition
Timed task: execute a task regularly at a specific point in time
Distributed task scheduling xxl-job: scheduled tasks in a distributed task system, xxljob is an open source project
2. How to generate timed tasks (timed tasks that come with springboot)
①. Note: All timing task methods are void
②, cron in @Scheduled (cron=" ") only supports six digits
But this method cannot be used in the case of a cluster, otherwise there will be multiple machines executing this code, so there is xxljob
3. Configure xxljob
①. Download xxljob project address: GitHub - xuxueli/xxl-job: A distributed task scheduling framework. (Distributed task scheduling platform XXL-JOB)
②. Configure the port number and database (the port number is set by yourself, the database is set to the location where you put the xxljob database, and the sql file project has it)
③, create a folder to put the configuration file, start, project jar package
Modify the address at the beginning to the address where the jar package is located
④. Click start to start the project (a black box appears and disappears to indicate that the project started successfully)
⑤. Browser localhost:8089 to open the page (8089 is the port number configured by yourself) Username: admin Password: 123456
4. Project integration xxljob
①. Introduce pom dependencies
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
②, Copy the official configuration file into it
③. Copy the configuration to the configuration of the project to be integrated
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin ### xxl-job, access token xxl.job.accessToken=default_token ### xxl-job executor appname xxl.job.executor.appname=xxl-job-executor-sample ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null xxl.job.executor.address= ### xxl-job executor server-info xxl.job.executor.ip= xxl.job.executor.port=9999 ### xxl-job executor log-path xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job executor log-retention-days xxl.job.executor.logretentiondays=30
④, the parameter appname in the third line corresponds to this
One machine corresponds to one appname
⑤, start the project
⑥, Create a new class and hand it over to spring to manage @Component
⑦. Add management method
a. The parameter aaaa in @XxlJob("aaaa") is filled in the JobHandler
b. Routing strategy: generally the first or polling
c. Write a timed task with xxljob
The four functions are:
Output log (the output log is printed at the scheduling log of the task scheduling center)
Get passed parameters
How many machines are there
Which machine is it now (3 and 4 are used when executing this part of code for multiple machines)