Spring @Scheduled Annotation
1.Overview
这里我们将会学习Spring @Scheduled 标签,了解它是如何配置,如何设置定时任务。
关于它的使用,有两点简单的规则需要记住:
※它的方法应该是一个void返回值类型
※它的方法不能接收任何参数
2.配置
如下所示,我们使用注解来进行配置。
1 @SpringBootApplication
2 @EnableScheduling //这个注解开启定时任务的功能
3 public class AppReadCsv {
4 @Autowired
5 JobLauncher jobLauncher;
6
7 @Autowired
8 Job job;
9
10 public static void main(String[] args) {
11 SpringApplication.run(App.class, args);
12 }
13
14 /*
15 * batch job will run every one minute after application is started
16 *
17 * */
18 @Scheduled(cron = "0 */1 * * * ?")
19 public void perform() throws Exception {
20 JobParameters params = new JobParametersBuilder()
21 .addString("JobID", String.valueOf(System.currentTimeMillis()))
22 .toJobParameters();
23 jobLauncher.run(job, params);
24 }
3.Schedule a task at Fixed Delay
上次任务的结束和下次任务的开始,中间的间隔时间是固定的。
1 @Scheduled(fixedDelay = 1000) // millisecond
2 public void scheduleFixedDelayTask() {
3 System.out.println(
4 "Fixed delay task - " + System.currentTimeMillis() / 1000);
5 }
4.Schedule a task at a Fixed Rate
以固定的间隔时间来执行任务
1 @Scheduled(fixedRate = 1000) // millisecond
2 public void scheduleFixedRateTask() {
3 System.out.println(
4 "Fixed rate task - " + System.currentTimeMillis() / 1000);
5 }
上一次任务开始后的一秒,下一次任务开始执行。
注意:下次任务的开始必须等到上次任务的结束,如果上次任务没有结束,那么及时到了设置的一秒间隔时间,那么下次任务也不会开始执行。
如果想要任务并行执行,需要使用@Async标签
1 @EnableAsync
2 public class ScheduledFixedRateExample {
3 @Async
4 @Scheduled(fixedRate = 1000)
5 public void scheduleFixedRateTaskAsync() throws InterruptedException {
6 System.out.println(
7 "Fixed rate task async - " + System.currentTimeMillis() / 1000);
8 Thread.sleep(2000);
9 }
10 }
5.Schedule tasks with initial delay
1 @Scheduled(fixedDelay = 1000, initialDelay = 1000) // millisecond
2 public void scheduleFixedRateWithInitialDelayTask() {
3
4 long now = System.currentTimeMillis() / 1000;
5 System.out.println(
6 "Fixed rate task with one second initial delay - " + now);
7 }
6.Cron Expressions
1 @Scheduled(cron = "0 15 10 15 * ?") // 执行时间 每个月第15天上午10:15执行.
2 public void scheduleTaskUsingCronExpression() {
3
4 long now = System.currentTimeMillis() / 1000;
5 System.out.println(
6 "schedule tasks using cron jobs - " + now);
7 }
表达式的说明:cron = "0 15 10 15 * ?"
<second> <minute> <hour> <day-of-month> <month> <day-of-week> <year> <command>
cron表达式中共有6个字符(space当做分隔符,不算)。分别对应上一行,红字部分。<year>作为可选项。
* 表示All。在month的位置,表示for every month
?表示any。使用在<day-of-month> and <day-of -week> 这两个选项中。
– 表示range 。例如,“10-11” in <hour> 表示 10点和11点
, 表示分隔多个数值 。例如, “MON, WED, FRI” 在 <day-of-week> 表示“Monday, Wednesday, and Friday”
下面举实例说明:
0 0 12 * * ? 2017 2017年每天的12点执行
0 0/5 13,18 * * ? 每天13点和18点的0,5,10,15,20,25,30,35,40,45,50,55分执行
0 15,45 13 ? 6 Tue 6月的每周二13点的15分和45分执行
参考链接:
https://www.baeldung.com/spring-scheduled-tasks
https://www.baeldung.com/cron-expressions
Spring @Scheduled Annotation的更多相关文章
- Spring @Scheduled应用解析
最近,遇到的一个需求,需要执行定时任务,每个一定时间需要执行某个方法 因为项目是SpringMVC的项目,所以使用的是Spring @Scheduled(由于quartz应用起来太麻烦,所以没有采用) ...
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...
- 实现运行在独立线程池的调度功能,基于Spring和Annotation
使用Spring的注解(@Scheduled)声明多个调度的时候,由于其默认实现机制,将导致多个调度方法之间相互干扰(简单理解就是调度不按配置的时间点执行). 为了解决该问题尝试了修改线程池大小,但是 ...
- 使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...
- Spring的annotation用在set方法上 hibernate的annotation用get方法上
1.Spring的annotation用在set方法上 2.hibernate的annotation用在get方法上
- 定时任务 spring @Scheduled注解
使用spring @Scheduled注解执行定时任务: 运行!!! 关于Cron表达式(转载) 表达式网站生成: http://cron.qqe2.com/ 直接点击 cronExpression ...
- Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别
Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...
- spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on
spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on
- spring @Scheduled注解执行定时任务
以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...
随机推荐
- Pinpoint 编译环境搭建(Pinpoint系列一)
本文基于 Pinpoint 2.1.0 版本 目录 一.2.1.0 版本特性 二.编译环境准备 三.编译注意事项 四.编译目录 五.注意事项 新版本的内容参考官方文档, Pinpoint的整个搭建是历 ...
- java开发两年,连这些多线程知识都还没掌握,你凭什么涨薪!
并发与并行 并发:两个或者多个事件在同一时间段发生(交替执行) 并行:两个或者多个事件在同一时刻发生(cpu多核.同时执行) 线程与进程 进程:是一个内存中运行的应用程序,有自己独立的内存空间,一个应 ...
- 循序渐进VUE+Element 前端应用开发(28)--- 附件内容的管理
在我们很多模块里面,都需要使用到一些诸如图片.Excel文件.PDF文件等附件的管理,一般我们倾向于把它独立为一个公用的附件管理模块,这样可以有效的统一管理附件的信息.本篇随笔介绍附件内容的管理,包括 ...
- FL Studio 插件使用技巧——Fruity Reeverb 2 (上)
许多学习FL的用户会发现,自己在听大师的电子音乐作品时都能感受到他们的音乐有一股强大的空间感,有时还能感知到深邃的意境.不少人会因此而疑惑:为什么出自我们之手的音乐就没有这样的效果呢?我们的音乐里到底 ...
- JVM(二)-内存区域之线程私有区
概述: 对于从事C.C++开发的程序员来说,在内存管理领域,他们既是拥有最高权力的"皇帝",又是从事最基础工作的劳动人民--既拥有每个对象的"所有权", 又担负 ...
- python截取视频制作动态表情包+文字
1:安装moviepy库 2:安装IPython库 代码如下: from moviepy.editor import * from IPython.display import Image def B ...
- php8.0正式版新特性和性能优化学习
前言 PHP团队宣布PHP8正式GA(链接).php的发展又开启了新的篇章,PHP8.0.0版本引入了一些重大变更及许多新特性和性能优化机制.火速学习下~ JIT(Just in Time Compi ...
- Tarjan 算法总结
一些概念 连通:无向图中的任意两点都可以互相到达. 强连通:有向图中的任意两点都可以互相到达. 连通分量:无向图的极大连通子图. 强连通分量:有向图的极大强连通子图. DFS 生成树:对一张图(有向无 ...
- dubbo起停之配置注解
虽然说多种方式配置dubbo最后殊途同归实例化为dubbo的各配置对象,但是了解下注解的解析过程也能让我们清楚dubbo在spring bean的什么时候怎么样实例化一个代理对象,这点来说了解整个过程 ...
- python:列表的去重:两种方法的问题是:结果是没有保持原来的顺序。
列表的去重 1.使用set的特型,python的set和其他语言类似, 是一个无序不重复元素集 orgList = [1,0,3,7,7,5] #list()方法是把字符串str或元组转成数组 for ...