SpringBoot启动定时任务,@Scheduled注解方式

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值