任务调度,XXL-JOB实战

1.引入依赖包

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

2.引入配置文件

@Slf4j
@Configuration
@EnableConfigurationProperties(XxlJobProps.class)
public class XxlJobConfig {

    @Autowired
    private XxlJobProps conf;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(conf.getAdmin().getAddresses());
        xxlJobSpringExecutor.setAppname(conf.getExecutor().getAppName());
        xxlJobSpringExecutor.setIp(conf.getExecutor().getIp());
        xxlJobSpringExecutor.setPort(conf.getExecutor().getPort());
        xxlJobSpringExecutor.setAccessToken(conf.getAccessToken());
        xxlJobSpringExecutor.setLogPath(conf.getExecutor().getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(conf.getExecutor().getLogRetentionDays());
        return xxlJobSpringExecutor;
    }
}

 XxlJobProps 属性

@Data
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobProps {
    /**
     * 调度中心配置
     */
    private XxlJobAdminProps admin;

    /**
     * 执行器配置
     */
    private XxlJobExecutorProps executor;

    /**
     * 与调度中心交互的accessToken
     */
    private String accessToken;

    @Data
    public static class XxlJobAdminProps {
        /**
         * 调度中心地址
         */
        private String addresses;
    }

    @Data
    public static class XxlJobExecutorProps {
        /**
         * 执行器名称
         */
        private String appName;

        /**
         * 执行器 IP
         */
        private String ip;

        /**
         * 执行器端口
         */
        private int port;

        /**
         * 执行器日志
         */
        private String logPath;

        /**
         * 执行器日志保留天数,-1
         */
        private int logRetentionDays;
    }
}

 xxl -job 地址配置

 

xxl:
  job:
    admin:
      addresses: http://10.1.1.217:8080/xxl-job-admin
    executor:
      appname: carpool
      port: 9999
      logpath: /logs/xxl
      logretentiondays: 5  

 创建任务

@Component
@Slf4j
public class UserJobHandler {

    /**
     * 用户解冻任务,每天凌晨1点跑一次
     *
     * @param param
     * @return
     */
    @XxlJob("thawUserJobHandler")
    public ReturnT<String> thawUserJobHandler(String param) {
        log.info("begin --> thawUserJobHandler");
        //
        log.info("end --> thawUserJobHandler");
        return ReturnT.SUCCESS;
    } 

}

登录xxl admin 创建thawUserJobHandler

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值