xxljob定时任务

1. XXL-Job简介

XXL-Job是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。它主要由两部分组成:

  • 调度中心(Scheduler):负责任务的调度管理。
  • 执行器(Executor):负责具体任务的执行。

2. 原理

XXL-Job采用客户端/服务端(C/S)架构模式。调度中心作为服务端负责任务的调度,而执行器作为客户端负责接收调度指令并执行任务。二者通过HTTP协议进行通信。

2.1 调度中心

  • 任务管理:在调度中心可以创建、删除、暂停或恢复任务。
  • 监控任务状态:调度中心可以实时监控任务的执行状态和结果。
  • 日志记录:所有任务执行的日志都会被记录下来供查询。

2.2 执行器

  • 注册:执行器需要主动向调度中心注册自己,注册信息包括执行器名称、IP地址等。
  • 任务执行:接收到调度中心下发的任务后,执行器会根据任务定义执行具体的业务逻辑。
  • 回调:任务执行完毕后,执行器会向调度中心汇报执行结果。

3. 重要角色及作用

  • 调度中心:负责任务的调度和管理。
  • 执行器:负责执行具体的任务逻辑。
  • 用户界面:用户通过前端界面来管理任务,查看任务状态和日志等。

4. 基本使用

4.1 安装与部署xxl-job的调度器

  1. 下载源码包:可以从GitHub上下载XXL-Job的最新版本。
    https://github.com/xuxueli/xxl-job/releases

  2. 部署调度中心:解压文件,修改配置文件,启动调度中心。

3.将sql语句初始化
在mysql中执行doc>>db>>tables_xxl_job.sql

4.2执行器

1.在springboot中引入xxl-job的依赖

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

2.配置文件中添加xxljob信息

点击查看代码
xxl:
  job:
    admin:
      address: http://127.0.0.1:8081/xxl-job-admin
    executor:
      appname: xxl-job-executor-sample
      ip: 127.0.0.1
      port: 9999
    accessToken: token123
3.创建xxljob的配置文件
点击查看代码
package com.weisi.smartTest.config;


import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@Configuration
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.address}")
    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() {
        logger.info(">>>>>>>>>>> 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;
    }

    /**
     * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
     *
     *      1、引入依赖:
     *          <dependency>
     *             <groupId>org.springframework.cloud</groupId>
     *             <artifactId>spring-cloud-commons</artifactId>
     *             <version>${version}</version>
     *         </dependency>
     *
     *      2、配置文件,或者容器启动变量
     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
     *
     *      3、获取IP
     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
     */


}
4.编写执行器方法
点击查看代码

package com.weisi.smartTest.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weisi.smartTest.common.R;
import com.weisi.smartTest.entity.Variable;
import com.weisi.smartTest.service.VariableService;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class TestController {



    /**
     * 1、简单任务示例(Bean模式)
     */
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        System.out.println("XXL-JOB, Hello World.");
    }
}

4.3 调度器上创建执行器和任务

4.4 启动项目


posted @ 2024-08-02 09:27  测试微思录-静水流深  阅读(33)  评论(0)    收藏  举报