目的
在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 * * * ?