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 -->
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

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
server.port=8989
#Name
server.context-path=/xxl-job-admin

### resources
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/

### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########

### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml

### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?Unicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=30
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=30000

### xxl-job email
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

### xxl-job, access token
xxl.job.accessToken=

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

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 {

    @Autowired
    private MailService mailService;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Timer timer = new Timer();
        // Timer task delay interval
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            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");
            }
        },100000,300000);
    }
}

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