天天看點

XXL-JOB的使用

添加依賴

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

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<String>

//@Scheduled(cron = "0 0/5 * * * ?")
    @XxlJob("callback")
    public ReturnT<String> callback(String param) {
        log.error("callback----------------->>>start");
        try {
            List<TbCallback> 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;
    }
           

繼續閱讀