xxl-Job配置的方法

XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。

一、pom中加依赖

有的时候会提示版本冲突,所以排除jackson

 
            com.xuxueli
            xxl-job-core
            2.0.2
            
                
                    com.fasterxml.jackson.core
                    jackson-databind
                
             
 

二、配置yml文件

##xxl-job
xxl:
  job:
    admin:
      addresses: http://d-xxljob.dmsd.tech:8081/job-admin-dev
    executor:
      appname: xxt-job
      ip:
      port: 9999
      logpath:
      logretentiondays: -1
    accessToken:

addresses:xxl-job的地址,如果有多个地址,用逗号分隔开。

appname: xxl-job执行器的名字

ip:执行器ip,选填,为空会自动获取ip

port:执行器端口号,选填,为空默认为9999

logpath:执行器运行日志文件存储磁盘路径,选填,为空使用默认路径

logretentiondays: 执行器日志保存天数,选填,值大于3时生效

三、xxl-job配置类

package com.dmsd.rank.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.ComponentScan;
import org.springframework.context.annotation.Configuration;


@Configuration
@ComponentScan(basePackages = "com.dmsd.rank.controller.dynamiccrawl")
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;
    @Value("${xxl.job.executor.appname}")
    private String appName;
    @Value("${xxl.job.executor.ip}")
    private String ip;
    @Value("${xxl.job.executor.port}")
    private int port;
    @Value("${xxl.job.accessToken}")
    private String accessToken;
    @Value("${xxl.job.executor.logpath}")
    private String logPath;
    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppName(appName);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSpringExecutor;
    }


}

注:basePackages 为使用xxl-job的类文件夹

四、设置xxl-job的功能代码

package com.dmsd.rank.controller.dynamiccrawl;

import com.dmsd.rank.utils.WeekUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

import static com.xxl.job.core.biz.model.ReturnT.SUCCESS;


@Component
@JobHandler(value = "dynamic-rank-crawl")
public class TestCrawl extends IJobHandler {
    @Override
    public ReturnT execute(String s) throws Exception {
        XxlJobLogger.log("XXL-JOB, Hello World.");

        for (int i = 0; i < 5; i++) {
            XxlJobLogger.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        return SUCCESS;
    }
}

五、执行器管理

xxl-Job配置的方法_第1张图片

点击新增执行器后

xxl-Job配置的方法_第2张图片

六、任务管理

点击新增任务后

xxl-Job配置的方法_第3张图片

 

七、启动项目

1、启动项目后端

2、点击启动

3、查看日志

你可能感兴趣的:(Spring)