1 需求
在项目开发中需要定时通过接口推送数据到其他平台,推送数据接口有调用量限制,当天次数消费完就不能调用了,第二天才能调用,需要采用定时器在第二天凌晨开启调用。
2 配置定时任务
import java.util.Date;
import lombok.extern.log4j.Log4j2;
import cn.hutool.core.date.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
@Log4j2
@Configuration
@EnableScheduling
public class HistoryStudentScheduleTask {
@Autowired
private HistoryStudentService historyStudentService;
/**
* 每晚1点执行任务
* 0 0 1 * * ?
*/
@Scheduled(cron = "0 0 1 * * ?")
private void execute() {
log.info("【定时推送数据开始执行】:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
historyStudentService.multiThreadPush();
log.info("【定时推送数据结束执行】:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
}
3 在线Cron表达式生成器
https://cron.qqe2.com/