SpringBoot配置动态Scheduled定时任务


目的

在SpringBoot项目整合完Scheduled之后,配置动态定时任务


一、示例

package com.xxx.xxx;

import com.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 同步第三方系统数据 task
 */
@Component
@Configuration //读取配置
@EnableScheduling // 2.开启定时任务
public class xxxTask {

    //日志
    private static final Logger logger = LoggerFactory.getLogger(xxxTask.class);

    @Autowired
    private xxxService xxxService;

    @Scheduled(cron = "${myTest.scheduledTime}") // 每30分钟执行一次 0 */30 * * * ?
    public void init(){
        logger.info("开始同步数据", DateUtil.nowDate());
        try {
            xxxService.synchronizationList();   //同步第三方系统列表数据(可以换成自己需要定时执行的业务代码)
        }catch (Exception e){
            e.printStackTrace();
            logger.error("同步数据失败" + " | msg : " + e.getMessage());
        }
    }

}

二、application.yml

myTest:
    scheduledTime: 0 */30 * * * ?
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值