当前位置: 首页 > news >正文

SpringBoot项目使用Schedule注释创建定时任务

文章目录

  • 知识讲解
    • 相关注释(主要两个,@EnableScheduling和@Scheduled)
    • scheduled的cron语法
  • 代码
    • 项目目录结构
    • 启动类(Application)
  • 定时任务类(Task)
    • 配置类(application.properties)
    • pom依赖
  • 展望(Quartz)

知识讲解

相关注释(主要两个,@EnableScheduling和@Scheduled)

@EnableScheduling注解: 在配置类上使用,开启计划任务的支持(类上)。
@Scheduled注解: 来声明这是一个任务,包括 cron,fixDelay,fixRate 等类型(方法上,需先开启计划任务的支持)。
注:
可以为@Scheduled注解的方法再用@PostConstruct注释(该注解的作用时刚启动SpringBoot项目时就执行该方法)

scheduled的cron语法

经典案例:
“1 * * * * ?” 每一分钟触发一次任务
“30 10 * * * ?” 每小时的10分30秒触发任务
“30 10 1 * * ?” 每天1点10分30秒触发任务
“30 10 1 20 * ?” 每月20号1点10分30秒触发任务
“30 10 1 20 10 ? *” 每年10月20号1点10分30秒触发任务
“30 10 1 20 10 ? 2011” 2011年10月20号1点10分30秒触发任务
“30 10 1 ? 10 * 2011” 2011年10月每天1点10分30秒触发任务
“30 10 1 ? 10 SUN 2011” 2011年10月每周日1点10分30秒触发任务
“15,30,45 * * * * ?” 每15秒,30秒,45秒时触发任务
“15-45 * * * * ?” 15到45秒内,每秒都触发任务
“15/5 * * * * ?” 每分钟的每15秒开始触发,每隔5秒触发一次
“15-30/5 * * * * ?” 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次
“0 0/3 * * * ?” 每小时的第0分0秒开始,每三分钟触发一次
“0 15 10 ? * MON-FRI” 星期一到星期五的10点15分0秒触发任务
“0 15 10 L * ?” 每个月最后一天的10点15分0秒触发任务
“0 15 10 LW * ?” 每个月最后一个工作日的10点15分0秒触发任务
“0 15 10 ? * 5L” 每个月最后一个星期四的10点15分0秒触发任务
“0 15 10 ? * 5#3” 每个月第三周的星期四的10点15分0秒触发任务
感谢大佬,原链接如下
例子链接:https://blog.csdn.net/qq_39361915/article/details/113507521

具体语法详见此篇文档的3、cron表达式,

代码

项目目录结构

在这里插入图片描述

启动类(Application)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling//按照时间cron对象的频率执行所有@Scheduled注释的方法
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

定时任务类(Task)

package nju.software;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component
public class Task {@Scheduled(cron = "1 * * * * ?")//每1分钟执行一次该方法@PostConstruct//刚启动SpringBoot项目时就执行该方法public void task1(){System.out.println("执行定时任务!!");}}

配置类(application.properties)

server.port = 8191
server.ip = 127.0.0.1

pom依赖

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/> <!-- lookup parent from repository -->
</parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>

展望(Quartz)

目前此种标识只能实现单线程定时任务,如果需要多线程的定时任务需要借助Quartz框架实现。(等待未来使用到再写教程)

http://www.lryc.cn/news/37613.html

相关文章:

  • 学习 Python 之 Pygame 开发魂斗罗(十一)
  • Linux驱动开发
  • 32--Vue-前端开发-Vue语法之组件化开发
  • 打怪升级之CFileDialog类介绍
  • 配天智造自主原创数字工厂:百余名员工人均创收122万
  • COLMAP
  • 2023-3-8 刷题情况
  • 关于长连接服务器和客户端之间要加入心跳的一些讨论
  • LeetCode——1590. 使数组和能被 P 整除
  • 12N65-ASEMI高压MOS管12N65
  • cushy-serial 一个轻量级Python serial库
  • 音视频开发系列(7)——Opengl常用Api介绍part1
  • linux时间的特殊用法
  • axios 封装,API接口统一管理
  • SpringBoot使用Redis实现缓存
  • [失业前端恶补算法]JavaScript leetcode刷题top100(三)
  • Spark RDD的设计与运行原理
  • Golang的下载与安装
  • 广州蓝景分享—8大Web前端开发的趋势
  • Java学习-MySQL-创建数据库表
  • Ethercat学习-GD32以太网学习
  • 项目规模估算如何精准 4大注意事项
  • 低代码:助力乡村振兴事业开启“智慧模式”
  • Flutter——Isolate主线机制
  • 提取游戏《Limbus Company》(边狱公司)内素材
  • 学生信息表
  • FOTA在AUTOSAR中的应用
  • 2023/3/10 Vue核心知识的学习- Vue - v-model双向绑定原理
  • 面朝大海,春暖花开丨2023年Kaadas凯迪仕全国经销商大会成功召开
  • 【ubuntu】安装cuda+anaconda的docker环境,并用Vscode远程访问