基于SpringBoot实现的定时任务Scheduled

发布时间:2021-07-05
技术:springboot-2.3.7.RELEASE + maven3.0.5 + jdk1.8

概述

在我们开发项目过程中,在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信、邮件之类的操作,也可能会定时地检查和监控一些标志、参数等。本demo主要介绍通过springboot实现scheduled定时任务

详细


一、运行效果

image.png


二、实现过程


①、pom 包配置


pom 包里面只需要引入 Spring Boot Starter 包即可



<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency></dependencies>


②、启动类启用定时


在启动类上面加上@EnableScheduling即可开启定时



@SpringBootApplication@EnableSchedulingpublic class Application {

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


③、创建定时任务实现类

定时任务1:



@Component
public class MySchedulerTask {

    private int count=0;

    @Scheduled(cron="*/2 * * * * ?")
    private void process(){
        System.out.println("this is scheduler task runing  "+(count++));
    }}



定时任务2:



@Component
public class MySchedulerTask2 {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Scheduled(fixedRate = 3000)
    public void reportCurrentTime() {
        System.out.println("现在时间:" + dateFormat.format(new Date()));
    }}


结果如下:



this is scheduler task runing  0

现在时间:2021-06-26 15:21:21

this is scheduler task runing  1

this is scheduler task runing  2

现在时间:2021-06-26 15:21:24

this is scheduler task runing  3

现在时间:2021-06-26 15:21:27

this is scheduler task runing  4

this is scheduler task runing  5

现在时间:2021-06-26 15:21:30

this is scheduler task runing  6

现在时间:2021-06-26 15:21:33

this is scheduler task runing  7

this is scheduler task runing  8

现在时间:2021-06-26 15:21:36



三、项目结构图

image.png


四、补充参数说明

@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/2 * * * * ?",一种是 fixedRate = 2000,两种都表示每隔两秒打印一下内容。

fixedRate 说明

  • @Scheduled(fixedRate = 2000) :上一次开始执行时间点之后2秒再执行

  • @Scheduled(fixedDelay = 2000) :上一次执行完毕时间点之后2秒再执行

  • @Scheduled(initialDelay=1000, fixedRate=2000) :第一次延迟1秒后执行,之后按 fixedRate 的规则每2秒执行一次

* <p>
* cron规则
* cron表达式中各时间元素使用空格进行分割,表达式有至少6个(也可能7个)分别表示如下含义:
* <p>
* 秒(0~59
* 分钟(0~59
* 小时(0~23
* 天(月)(0~31,但是你需要考虑你月的天数)
* 月(0~11
* 天(星期)(1~7 1=SUN SUNMONTUEWEDTHUFRISAT
* 7.年份(19702099
* 其中每个元素可以是一个值(6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期""星期中的日期"这两个元素互斥的,必须要对其中一个设置?.
* <p>
* 0 0 10,14,16 * * ? 每天上午10点,下午2点,4
* 0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
* 0 0 12 ? * WED 表示每个星期三中午12
* "0 0 12 * * ?" 每天中午12点触发
* "0 15 10 ? * *" 每天上午10:15触发
* "0 15 10 * * ?" 每天上午10:15触发
* "0 15 10 * * ? *" 每天上午10:15触发
* "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
* "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
* "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
* "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
* "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
* "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:102:44触发
* "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
* "0 15 10 15 * ?" 每月15日上午10:15触发
* "0 15 10 L * ?" 每月最后一日的上午10:15触发
* "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
* "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
* "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发


本实例支付的费用只是购买源码的费用,如有疑问欢迎在文末留言交流,如需作者在线代码指导、定制等,在作者开启付费服务后,可以点击“购买服务”进行实时联系,请知悉,谢谢
手机上随时阅读、收藏该文章 ?请扫下方二维码