XXL-JOB的使用

添加依赖

       
            com.xuxueli
            xxl-job-core
            2.2.0
        

yml配置

xxl:
  job:
    admin:
      # 调度中心部署跟地址, 为空则关闭自动注册
      addresses: http://127.0.0.1:8812/medisign-job-admin
    # 执行器通讯TOKEN [选填]:非空时启用;
    accessToken:
    executor:
      # 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
      appname: base-service-job
      address:
      ip:
      # 执行器端口号,注意要配置不同执行器端口;
      port: 9999
      logpath: E:/upload/logs/xxl-job/jobhandler
      # 执行器日志文件保存天数
      logretentiondays: 30

添加配置类XxlJobConfig

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;
​
@Configuration
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
​
    @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() {
        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;
    }
}
修改定时器

@Scheduled改为@XxlJob, ()括号中的值为定时器名称,对应任务调度配置的任务,不可重复

传参改为String param

返回值改为ReturnT

    //@Scheduled(cron = "0 0/5 * * * ?")
    @XxlJob("callback")
    public ReturnT callback(String param) {
        log.error("callback----------------->>>start");
        try {
            List callbackList = callbackService.getByStatusAndCountLessThanEqual("0", 3);
            if (null != callbackList && callbackList.size() > 0) {
                for (TbCallback callback : callbackList) {
                    asyncCallBackService.callback(callback);
                }
            }
        }catch (Exception e) {
            log.error("回调失败:{}",e);
        }
        return ReturnT.SUCCESS;
    }

 

你可能感兴趣的:(XXL-JOB,分布式任务调度,XXL-JOB,分布式任务调度)