spring boot: 计划任务@ EnableScheduling和@Scheduled
spring boot: 计划任务@ EnableScheduling和@Scheduled
@Scheduled中的参数说明
@Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;@Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;@Scheduled(cron="* * * * * ?"):按cron规则执行。
常用Cron表达式( 秒/分/时/日/月/周/年 )
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午12点触发
"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: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 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
实例:
service
1 package ch2.scheduler; 2 3 //时间处理,时间格式化 4 import java.util.Date; 5 import java.text.SimpleDateFormat; 6 7 8 //spring计划任务声明(针对方法声明) 9 import org.springframework.scheduling.annotation.Scheduled; 10 //spring组件声明 11 import org.springframework.stereotype.Service; 12 13 14 //组件什么 15 @Service 16 public class SchedulerService { 17 18 //创建日期模板 19 private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH::MM::ss"); 20 21 22 //每5秒钟执行一次 23 @Scheduled(fixedRate = 5000) 24 public void reportCurrentTime() 25 { 26 System.out.println("每五秒执行一次: " + dateFormat.format( new Date() )); 27 } 28 29 //按照cron属性指定执行时间:秒分时 30 @Scheduled(cron = "0 34 18 ? * *") 31 public void fixTimeExecution() 32 { 33 System.out.println("在指定的时间内执行: " + dateFormat.format( new Date()) ); 34 } 35 36 37 }
config
1 package ch2.scheduler; 2 3 //自动引入包 4 import org.springframework.context.annotation.ComponentScan; 5 //配置类声明 6 import org.springframework.context.annotation.Configuration; 7 8 //计划任务类声明 9 import org.springframework.scheduling.annotation.EnableScheduling; 10 11 12 //配置类声明 13 @Configuration 14 //自动引入包 15 @ComponentScan("ch2.scheduler") 16 //通过@EnableScheduling开启对计划任务的支持 17 @EnableScheduling 18 public class TaskSchedulerConfig { 19 20 21 22 }
main
1 package ch2.scheduler; 2 //引入容器 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 5 public class Main { 6 7 8 public static void main(String[] args) 9 { 10 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class); 11 //SchedulerService schedulerService = context.getBean(SchedulerService.class); 12 13 14 } 15 }
转载于:https://www.cnblogs.com/achengmu/p/8178585.html
spring boot: 计划任务@ EnableScheduling和@Scheduled相关推荐
- 终于把 Spring Boot 3.0 写成书了!
大家好,我是R哥. 我的新书<Spring Boot 3 核心技术与最佳实战>打磨一年多,今天终于上市了,定价 158 元,今天刚上市搞 5 折促销,80 元不到上车,这可能是全网最便宜的 ...
- scheduled 一秒钟执行一次_spring boot的Scheduled帮你实现定时任务,spring boot实践(11)...
01 spring boot读取配置信息 02 多环境配置 03 处理全局异常 04 spring boot admin 05 spring mvc + mybatis 06 spring boot ...
- scheduled每天下午1点执行一次_在Spring Boot项目中使用@Scheduled注解实现定时任务...
在java开发中定时任务的实现有多种方式,jdk有自己的定时任务实现方式,很多框架也有定时任务的实现方式.这里,我介绍一种很简单的实现方式,在Spring Boot项目中使用两个注解即可实现. 在sp ...
- 在Spring Boot中使用@Scheduled实现定时任务
2019独角兽企业重金招聘Python工程师标准>>> 在Spring Boot中使用@Scheduled实现定时任务 之前没有使用Spring Boot的Java风格配置的时候,用 ...
- Spring Boot中使用@Scheduled创建定时任务
我们在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. 创建定时任务 在Spring Boot中编写定时 ...
- Spring Boot笔记-线程池调度计划仅运行一次
这里是有这样的一个需求,启动spring boot后用一个新线程,跑一次就可以了,首先是线程池申请和配置: @Configuration @EnableAsync public class Async ...
- Spring : @EnableScheduling注解 @Scheduled
文章目录 1.美图 2.概述 3.案例 4.@Scheduled 5.源码 6.抽取代码 1.美图 2.概述 @EnableScheduling注释,它可以初始化一个调度器. 3.案例 Spring ...
- @scheduled注解配置时间_Java开发重写Spring Boot定时任务,支持可动态调整执行时间...
Spring Boot应该是目前最火的java开源框架了,它简化了我们创建一个web服务的过程,让我们可以在很短时间.基本零配置就可以启动一个web服务. 定时任务在我们平常的业务开发用的非常多,Sp ...
- 为什么你学了 N 遍 Spring Boot,至今还是学生项目?你的问题在这里 | 原力计划...
作者 | bugpool 出品 | CSDN博客 为什么你学了n遍<1天精通Spring Boot>,至今还是不精通Spring Boot,甚至还是停留在学生项目?真正要做项目就应该一步到 ...
最新文章
- 美电信运营商Verizon推企业云计算服务
- 万字总结 MySQL核心知识,赠送25连环炮
- mysql2012用户名_mysql2012 代码怎么用
- python3常用模块_Python3 常用模块
- mysql中when用法_Mysql 中的CASE WHEN 用法
- android获取设备的唯一编号,android如何获取设备的唯一设备码
- JS分页控件,可用于无刷新分页
- Alipay Direct Bankpay 支付宝网银支付 (For OpenCart 2.x)
- AI 是否会取代计算机程序员
- zw版【转发·台湾nvp系列Delphi例程】HALCON HighpassImage
- HTML+JS实现页面跳转
- 输出流创建txt文件
- 河南自考本科英语可用计算机代替,河南自考选考2至3门专业课可代替英语课程...
- win7nodejs压缩包配置环境变量
- maven引用公共包_maven怎么 引入(或引用/使用) 自定义(或本地/第三方) jar的三种方式 图文教程-Fun言...
- Linux gdb调试(4):多进程与多线程调试
- 15款最好用的腾讯短链接(url.cn接口)批量在线生成接口
- NAS群晖Docker安装odoo16社区版和企业版教程
- Python黑客帝国代码雨
- Java 里的异常(Exception)详解