- cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
- crontab 工具 https://tool.lu/crontab/
- fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
- fixedDelay: 上一次执行结束时间点后xx秒再次执行
Application
//定时任务 @EnableScheduling
示例类【Scheduling】
/**
* TODO 暂时没用的定时任务
*/
@Component
public class Scheduling {
//@Scheduled(fixedRate = 2000)
//@Scheduled(cron = )
public void sun(){
System.out.println(LocalDateTime.now()+" 滴滴答答!!!");
}
}
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class HelloSchedule {
//@Scheduled(cron = "* 0/1 * * * ?") 秒分时日月年
//@Scheduled(cron = "* * 0/1 * * ?") linux unix rsync crontab
@Scheduled(cron = "0/5 * * * * ?")
public void hello(){
System.out.println("enter hello job!");
}
}
本文作者为DBC,转载请注明。