spring boot 定时任务(springboot内置)

  作者:记性不好的阁主

转载自:https://blog.csdn.net/dkbnull/article/details/87659898


github地址:https://github.com/1051513344/SpringBootDemo.git


1、导入web依赖


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


2、在项目启动类上增加注解@EnableScheduling,表示开启定时任务


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* Spring Boot 启动类
*
* @author dukunbiao(null) 2018-08-18
* https://github.com/dkbnull/SpringBootDemo
*/
@SpringBootApplication
@EnableScheduling
public class SpringBootDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}



3、创建定时任务类





4、编写定时任务类


一、串行定时任务


import cn.wbnull.springbootdemo.util.DateUtils;
import cn.wbnull.springbootdemo.util.LoggerUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* 定时任务
*
* @author dukunbiao(null) 2019-02-18
* https://github.com/dkbnull/SpringBootDemo
*/
@Component
public class ScheduledTask {

@Scheduled(cron = "0/10 * * * * ?") //10秒执行一次
public void scheduledTaskByCorn() {
LoggerUtils.info("定时任务开始 ByCorn" + DateUtils.dateFormat());
scheduledTask();
LoggerUtils.info("定时任务结束 ByCorn" + DateUtils.dateFormat());
}

// @Scheduled(fixedRate = 10000) //10秒执行一次
// public void scheduledTaskByFixedRate() {
// LoggerUtils.info("定时任务开始 ByFixedRate" + DateUtils.dateFormat());
// scheduledTask();
// LoggerUtils.info("定时任务结束 ByFixedRate" + DateUtils.dateFormat());
// }

// @Scheduled(fixedDelay = 10000) //10秒执行一次
// public void scheduledTaskByFixedDelay() {
// LoggerUtils.info("定时任务开始 ByFixedDelay" + DateUtils.dateFormat());
// scheduledTask();
// LoggerUtils.info("定时任务结束 ByFixedDelay" + DateUtils.dateFormat());
// }

// @Scheduled(cron = "${demo.corn}")
// public void scheduledTaskByConfig() {
// LoggerUtils.info("定时任务 ByConfig" + DateUtils.dateFormat());
// }

//模拟业务代码执行时间 (使用时去掉)
private void scheduledTask() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


说明


默认@Scheduled不是并行的,所有定义的定时任务呀都是串行执行。


二、并行定时任务


须加上@EnableAsync注解 并且在需要并行执行定时任务的方法前加@Async注解


import cn.wbnull.springbootdemo.util.DateUtils;
import cn.wbnull.springbootdemo.util.LoggerUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* 定时任务
*
* @author dukunbiao(null) 2019-02-18
* https://github.com/dkbnull/SpringBootDemo
*/
@Component
@EnableAsync
public class ScheduledTaskV3 {

@Scheduled(cron = "0/7 * * * * ?")
@Async
public void scheduledTaskV1() {
LoggerUtils.info("定时任务V3,定时任务1开始:" + DateUtils.dateFormat());
scheduledTask();
LoggerUtils.info("定时任务V3,定时任务1结束:" + DateUtils.dateFormat());
}

@Scheduled(cron = "0/10 * * * * ?")
@Async
public void scheduledTaskV2() {
LoggerUtils.info("定时任务V3,定时任务2开始:" + DateUtils.dateFormat());
scheduledTask();
LoggerUtils.info("定时任务V3,定时任务2结束:" + DateUtils.dateFormat());
}

@Scheduled(cron = "0/22 * * * * ?")
@Async
public void scheduledTaskV3() {
LoggerUtils.info("定时任务V3,定时任务3开始:" + DateUtils.dateFormat());
scheduledTask();
LoggerUtils.info("定时任务V3,定时任务3结束:" + DateUtils.dateFormat());
}

private void scheduledTask() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}



cron表达式示例:

0 * * * * ? 每分钟触发

0 0 * * * ? 每小时整触发

0 0 4 * * ? 每天凌晨4点触发

0 15 10 * * ? 每天早上10:15触发

*/5 * * * * ? 每隔5秒触发

0 */5 * * * ? 每隔5分钟触发

0 0 4 1 * ? 每月1号凌晨4点触发

0 0 4 L * ? 每月最后一天凌晨3点触发

0 0 3 ? * L 每周星期六凌晨3点触发

0 11,22,33 * * * ? 每小时11分、22分、33分触发

相关推荐

评论 抢沙发

表情

分类选择