Spring boot XXL job task scheduling

I. Introduction to XXL job

Take a paragraph
XXL-JOB is a lightweight distributed task scheduling platform. Its core design goal is to develop quickly, learn simply, lightweight and easy to expand. Open source code and access to a number of companies online product line, out of the box.
maven dependence

        <!-- xxl-job-core -->

Link to original document XXL job using documents

II. Build a task scheduling center platform

1. Download the source code of the XXL job project on github Download address
2. Run sql script file to create project database table
3. Modify the settings of the application.properties part of the dispatching center, and pay attention to the user name and password of the database.

### web
#Dispatching center platform port

### resources

### freemarker

### mybatis

### xxl-job, datasource

spring.datasource.tomcat.validation-query=SELECT 1

### xxl-job email

### xxl-job, access token

### xxl-job, i18n (default empty as chinese, "en" as english)

4. Execute entry class startup project

5. Browser access project

Default account admin password 123456
Sign in

In this interface, you can see the number of tasks and actuators

Task: equivalent to

@Component  //Or @ WebListener
public class BrithdayListener implements ServletContextListener {

    private MailService mailService;

    public void contextInitialized(ServletContextEvent sce) {
        Timer timer = new Timer();
        // Timer task delay interval
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                // You can query the database to see who's birthday today. You can check once a day, traverse the collection, and send mail to them.
                mailService.send("1947065958@qq.com","Birthday wishes","Test, happy birthday");

new TimerTask() in Timer

Executor: the project where our task is located

III. actuator

1. The source code downloaded on github contains various executor instance projects.

I use the springboot version
Because I changed the port of the dispatch center project

The access path of the dispatch center should be written here
xxl.job.executor.appname is the name of the executor
Scheduling task: inherit the IJobHandler interface and implement the execute method:
This is clearly written in the sample project

2. Start sample project
Execute sample project entry class

Just start normally

IV. dispatching center management actuator and dispatching task

1. Enter the dispatching center platform to add actuators

AppName sample project name
The registration mode is manual and automatic. You need to enter the access path of the actuator project by yourself.
Under the distributed project, the path of module cluster can be added to schedule the executor by polling
Save it

2. New tasks

New task in task management interface

cron expression to specify the execution time. Here it is set to save every hour.

Click start to start the task automatically once an hour
Click execute to execute the task immediately to view the dispatch log successfully

View execution log

Execute method is executed
The XXL job has been successfully set up and is easy to use.

Five, prospect

I think the use of XXL job is very convenient, and the platform construction is also very simple. It's easy to start with documents. If you want to write a job, you only need to inherit the IJobHandler to implement the execute method. The visual operation of the scheduling center is very convenient. Sending mail simply calls the previously sent mail code in the excute method, and sends out the business requirement to write in any scenario. You can deploy the same module code on different servers, just register the server address with the dispatch center.

Tags: Spring FreeMarker Tomcat Database

Posted on Fri, 25 Oct 2019 09:14:19 -0400 by crondeau