SpringTask任务调度详解+SpringBoot整合

3 篇文章 0 订阅
2 篇文章 0 订阅

认真学完,相信你会有收获

1 定时任务的框架

  1. Quartz:整合了Spring
  2. SpringTask:轻量
  3. SpringBoot整合SpringTask:两行代码即可搞定

1.1.说说定时任务的应用场景

1. 月底扣话费
2. 会员到期
3. 生日祝福
4. qq好友生日祝福
5. 月底的自动发邮件给领导, 统计这个月的数据
6. 等等等

2.SpringBoot整合SpringTask任务调度框架的使用

2.1 注解

采用SpringBoot整合SpringTask不需要额外导包

@EnableScheduling:开启对定时任务的支持,添加在启动类上

@Scheduled(cron="*/6 * * * * ?"):执行的方法上添加,标识方法何时被执行,注意是有空格,后面详细解释

2.2 启动类启用定时任务调度

在启动类上加@EnableScheduling即可开启定时,当项目中有多个执行任务的时候,只需要开启一次

@SpringBootApplication
@EnableScheduling
public class SpringTaskDemoApplication {
	publi static void main(String[] args){
		SpringApplication.run(SpringTaskDemoApplication.class,args);
	}
}

2.3 创建定时任务实现类

2.3.1 项目创建

2.3.2 添加SpringBoot启动器

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>
    </dependencies>

2.3.2 创建启动器

2.3.3 创建两个简单的定时任务

2.3.3.1 定时任务1:

package com.manlu.test;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 漫路
 */
@Component
public class SchedulerTask01 {
    private int count = 0;

    //每嗝6秒执行一次
    @Scheduled(cron = "*/6 * * * * ?")
    private void process(){
        System.out.println("定时任务1:"+ ++count);
    }
}

2.3.3.1 定时任务2:

package com.manlu.test;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author manlu
 */
@Component
public class SchedulerTask02 {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedDelay = 6000)
    private void reportCurrentTime(){
        System.out.println("定时任务2: "+DATE_FORMAT.format(new Date()));
    }
}

2.3.4 运行结果

//记得要在启动器上加@EnableScheduling注解

2.4 @Scheduled注解中参数说明

  • @Scheduled("*/6 * * * * ?"):参数可以接收两种定时的设置
    • 一种是常用的cron=“*/6 * * * * ?”
    • 一种是fixedRate=6000,这两种都表示每隔6秒运行一次
  • @Scheduled(fixedRate = 6000):上次开始执行时间点之后6秒再执行
  • @Scheduled(fixedDelay = 6000):上次执行完毕时间点之后6秒再执行
  • @Scheduled(initialDelay = 1000, fixedRate = 6000):第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次

2.5 CronTrigger的使用

上面说的那个要加空格的参数怎么写?现在聊一聊

  • cron里面写的是有规则的表达式

2.5.1 定时任务表达式表

2.5.2 一些符号的用法

2.5.3 CronTriggerBean调度器介绍

一个cron表达式至少有6个(也可能是7个) 由空格分隔的时间元素。从左到右,这些元素的定义如下:

参数1:秒 0 - 59
参数2:分钟 0 -59
参数3:小时 0 - 23
参数4:月份中的日期 0 - 30
参数5:月份 0 - 11 或 JAN - DEC
参数6:星期中的日期 1 - 7 获取  SUN - SAT
参数7:年份 1970 - 2099

    每个元素都可以显示的规定一个值(如6),一个区间(如9-12),一个列表(如9,11,13)或一个通配符(如*)。
“月份中的日期” 和 “星期中的日期”这两个元素是互斥的,因此需要通过设置一个问号(?)来表明你不想设置的那个字段

2.5.4 表达式示例

表达式对应的意思
“0 0 10,14,16 * * ?”每天上午10点,下午2点,4点 时触发
“0 0/30 9-17 * * ?”朝九晚五工作时间内每半小时,从0分开始每隔30分钟发送一次
“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/55 14 * * ?”在每天下午2点到下午2:55期间的每5分钟触发
“0 0/55 14,18 * * ?”在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
“0 0-5 14 * * ?”在每天下午2点到下午2:05期间的每1分钟触发
“0 10,44 14 ? 3 WED”每年三月的星期三的下午2:10和2: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 ? * 6L2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6#3”每月的第三个星期五上午10:15触发
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Spring Boot任务调度,可以使用Spring框架提供的`@Scheduled`注解来实现。下面是整合任务调度的步骤: 1. 在Spring Boot项目中添加任务调度的依赖。可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 这里使用了Quartz作为任务调度的实现。 2. 创建一个任务类,该类中包含需要执行的具体任务逻辑。可以使用`@Component`注解将其声明为Spring组件,方便自动扫描和注入。 ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyTask { @Scheduled(cron = "0 0 0 * * ?") // 使用cron表达式定义任务执行时间 public void executeTask() { // 执行具体的任务逻辑 System.out.println("执行任务..."); } } ``` 在上述示例中,使用了`@Scheduled`注解来定义任务的执行时间,这里指定了每天凌晨执行一次任务。 3. 在启动类上添加`@EnableScheduling`注解,开启Spring任务调度功能。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 这样就完成了Spring Boot任务调度整合。任务将会在指定的时间自动执行。 注意:确保项目中`@EnableScheduling`注解被正确添加,并且Spring Boot的自动扫描能够找到任务类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值