[SpringBoot Advanced] SpringBoot integrates XXL-JOB distributed task scheduling platform

Introduction

XXL-JOB is a distributed task scheduling platform. Its core design goals are rapid development, easy learning, lightweight, and easy expansion. The source code is now open and connected to the online product lines of many companies, out of the box.

Project address: https://gitee.com/xuxueli0323/xxl-job

download source code

Source address: https://gitee.com/xuxueli0323/xxl-job

insert image description here

git clone to local, use idea to open

insert image description here

Or use idea to pull the project directlyinsert image description here

Deploy task scheduling platform

Execute SQL script

insert image description here

Execute SQL scripts to generate related tables

insert image description here

Deployment task platform

A simple test can directly start the mian use of the xxl-job-admin project

1. Modify the data source username/password

insert image description here

2. Maven install/package is packaged into the local libraryinsert image description here

3. In the cmd window, java -jar xxl-job-admin-2.4.0-SNAPSHOT.jar starts the project

insert image description here

4. Task scheduling platform address: http://localhost:8080/xxl-job-admin/ User name: admin Password: 123456

insert image description here

easy to use

Initialize the test project

Create a new springboot project and copy the following files to your own project

insert image description here

pom dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- xxl-job-core -->
    <dependency>
        <groupId>com.xuxueli</groupId>
        <artifactId>xxl-job-core</artifactId>
        <version>2.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

SampleXxlJob

/**
 * XxlJob开发示例(Bean模式)
 *
 * 开发步骤:
 *      1、任务开发:在Spring Bean实例中,开发Job方法;
 *      2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
 *      3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志;
 *      4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果;
 *
 */
@Component
public class SampleXxlJob {
    
    
    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
    /**
     * 1、简单任务示例(Bean模式)
     */
   @XxlJob("lywJobHandler")
    public ReturnT<String> demoJobHandler() throws Exception {
    
    
        // 打印任务日志
        XxlJobHelper.log("开始测试xxl-job demo1");

        // 获取任务参数
        String jobParam = XxlJobHelper.getJobParam();
        ObjectMapper objectMapper = new ObjectMapper();
        Map map = objectMapper.readValue(jobParam, Map.class);

        XxlJobHelper.log("map: "+ map);
        logger.info("jobParam: {}", map);
        for (int i = 0; i < 5; i++) {
    
    
            XxlJobHelper.log("开始生成:" + i);
           // TimeUnit.SECONDS.sleep(2);
        }
        // default success
        return ReturnT.FAIL;
    }
    @XxlJob("dayJobHandler")
    public void dayJobHandler() throws Exception {
    
    
        XxlJobHelper.log("开始测试xxl-job demo2");

        for (int i = 0; i < 5; i++) {
    
    
            XxlJobHelper.log("开始生成:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        // default success
    }

    /**
     * 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
     */
    @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
    public void demoJobHandler2() throws Exception {
    
    
        XxlJobHelper.log("XXL-JOB, Hello World.");
    }
    public void init(){
    
    
        logger.info("init");
    }
    public void destroy(){
    
    
        logger.info("destroy");
    }
}
  • XxlJobHelper.log("start testing xxl-job demo1"); print log
  • XxlJobHelper.getJobParam(); get parameters

Change setting

Modify the project configuration and start the demo projectinsert image description here

Scheduling platform configuration executor

AppName is specified in the demo projectxxl.job.executor.appname=xxl-job-executor-lyw

insert image description here

OnLine machine address: Configure the executor, start the corresponding project, and it will automatically display on-line

insert image description here

task management add task

Add a scheduled task

insert image description here

start task

insert image description here

view log

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/qq_45297578/article/details/128786200