前言
定时任务,程序员的常用手段
1.代码如下
import cn.cncommdata.schedule.service.IXFScheduleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 新建定时任务
*/
@Component
@EnableScheduling
public class SchedulingJob {
/**
* 导入业务
*/
@Resource
private IXFScheduleService xfScheduleService;
/**
* 定时(每隔5秒)执行task
*/
// @Scheduled(cron = "0 0/5 * * * ?") // 每五分钟执行一次
@Scheduled(cron = "${FIVE_SECONDS}") // 每五秒执行一次
public void scheduler() {
xfScheduleService.task();
}
}
2.yml中灵活配置时间
推荐使用
FIVE_SECONDS: 0/5 * * * * ?
FIVE_MINUTES: 0 0/5 * * * ?