一、xxl-job introduction
XXL-JOB is a distrbute task schdule platform,Its core design goals are rapid development, simple learning, lightweight, and easy expansion。The source code is now open and connected to the online product lines of many companies, ready to use out of the box.
二、environment setup
use docker-compose to build test environment,specific conguration as follows:
docker-compose-xxl-job.yml
# reference docs: https://www.xuxueli.com/xxl-job
version: "3"
networks:
xxljob:
driver: bridge
services:
xxl-job-admin:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/xxl-job-admin:2.3.0 # inital images`xuxueli/xxl-job-admin:2.3.0`
container_name: xxl-job-admin
environment:
# TODO :/xxl-job/xxl-job-admin/src/main/resources/application.properties
PARAMS: "--spring.datasource.url=jdbc:mysql://10.11.68.77:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
--spring.datasource.username=root
--spring.datasource.password=root
--server.servlet.context-path=/xxl-job-admin
--spring.mail.host=smtp.qq.com
--spring.mail.port=25
--spring.mail.username=xxx@qq.com
--spring.mail.from=xxx@qq.com
--spring.mail.password=xxx
--xxl.job.accessToken="
ports:
- "8080:8080"
depends_on:
- mysql
networks:
- xxljob
mysql:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7
container_name: mysql_3306
restart: unless-stopped
volumes:
- "./mysql/my.cnf:/etc/mysql/my.cnf"
- "./mysql/init-file.sql:/etc/mysql/init-file.sql"
- "./mysql/data:/var/lib/mysql"
# - "./mysql/conf.d:/etc/mysql/conf.d"
- "./mysql/log/mysql/error.log:/var/log/mysql/error.log"
- "./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d"
environment:
TZ: Asia/Shanghai
LANG: en_US.UTF-8
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: xxl_job
ports:
- "3306:3306"
networks:
- xxljob
start test environment
docker-compose -f docker-compose-xxl-job.yml -p xxl-job up -d
access url:http://ip:9003/xxl-job-admin
default login username/password:admin/123456
三、Code Project
Goals of this experiment: Write a task class JOB and implement cron scheduling on the console
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xxl-job</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</project>
xxl-job configuration
package com.et.xxljob.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> start xxl-job config init");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
jobhandler
package com.et.xxljob.handler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class DemoJob {
@XxlJob("job1")
public void job1() {
log.info("do job1");
}
}
application.yaml
server:
port: 8088
logging:
level:
com.ramble: debug
xxl:
job:
admin:
addresses: http://127.0.0.1:8080/xxl-job-admin
accessToken:
executor:
appname: xxljob-demo-service
address: ""
ip: ""
port: 0
logpath: ./logs/xxl-job/jobhandler
logretentiondays: 30
DemoApplication.java
package com.et.xxljob;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Code Repository
四、Test
- Start the executor project, go to the dispatch center -> executor management, create a new executor, the AppName must be consistent with the name configured in the executor configuration file, select automatic registration.
- In the Scheduling Center -> Task Management, create a new task, select the new executor we just created, and focus on the configuration of JobHandler. The name must be consistent with the name in the @XxlJob annotation on the corresponding task method to find the corresponding task to execute.
- After the creation is completed, execute it once in the operation and check the corresponding log output to see that the configuration is successful.
2024-02-28 10:43:49.595 INFO 26368 --- [ Thread-8] com.xxl.job.core.thread.JobThread : >>>>>>>>>>> xxl-job JobThread stoped, hashCode:Thread[Thread-8,10,main]
2024-02-28 10:43:50.016 INFO 26368 --- [ Thread-9] com.et.xxljob.handler.DemoJob : do job1