5.cron表达式
常用: 秒、分、时、日、月、年
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0 12 * * ? 每天中午12点触发
0 0/5 0 * * ? 每5分钟执行一次
具体更多可以参考: https://www.cnblogs.com/linjiqin/archive/2013/07/08/3178452.html
//串行方式执行定时任务
@Component
public class OpenService {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
//5秒执行一次
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("当前时间:" + dateFormat.format(new Date()));
}
//1分钟执行一次
@Scheduled(cron = "0 0/1 * * * ?")
public void pushDataScheduled(){
System.out.println("当前时间pushDataScheduled:" + dateFormat.format(new Date()));
}
}
//启动类上加@EnableScheduling注解;
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
定时任务(Spring 调度任务@scheduled学习总结)
@Component
public class ScheduledTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("当前时间:" + dateFormat.format(new Date()));
}
}
官网Api:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled;(英文版)
1.initialDelay :初次执行任务之前需要等待的时间
@Scheduled(initialDelay =5000) public void doSomething() { }
2.fixedDelay:每次执行任务之后间隔多久再次执行该任务。
@Scheduled(fixedDelay=5000) public void doSomething() { // something that should execute periodically }
3.fixedRate:执行频率,每隔多少时间就启动任务,不管该任务是否启动完成
@Scheduled(fixedRate=5000) public void doSomething() { }
4.cron=“”设置时分秒等具体的定时,网上很很多相关列子。
例如:转:http://blog.csdn.net/kkdelta/article/details/7238581
@Scheduled(cron="*/5 * * * * MON-FRI") public void doSomething() { // something that should execute on weekdays only }
Spring使用之:Quartz定时任务为什么会被阻塞:http://fhqllt.iteye.com/blog/434943