SpringBoot 定时任务 @Scheduled

前言

有时候,我们有这样的需求,需要在每天的某个固定时间或者每隔一段时间让应用去执行某一个任务。一般情况下,可以使用多线程来实现这个功能;在 Spring 框架下可以搭配 Quartz 来实现,附上笔记 Spring Quartz 实现多任务定时调用。在 SpringBoot 框架下,我们可以用 Spring scheduling 来实现定时任务功能。
首先,我们先创建一个 Spring Boot 项目。创建方法:
* (自动完成初始化)http://blog.csdn.net/u011244202/article/details/54767036
* (手动完成初始化)http://blog.csdn.net/u011244202/article/details/54604421

同时要注意,SpringBoot 项目需要 JDK8 的编译环境!

然后,在项目主类中加入@EnableScheduling注解,启用定时任务的配置

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

最后,创建定时任务实现类,注意加上注解@Scheduled

@Scheduled 的介绍

1. cron 表达式与 zone

zone:表示将解析cron表达式的时区。
关于 cron 表达式,可以参考一下 http://blog.csdn.net/u011244202/article/details/54382466 里面的附录。

2. fixedRate 的解释

调用固定周期(以毫秒为单位)执行方法。就是上一次开始执行时间点之后延迟执行。

3. fixedDelay 的解释

上次调用结束和下一次调用结束之间的固定周期(以毫秒为单位)执行方法。就是上一次执行完毕时间点之后延迟执行。

4. initialDelay 的解释

在第一次执行fixedRate()或fixedDelay()任务之前延迟(以毫秒为单位)。

实例

在项目启动 8s 后,每隔 5s 调用定时任务。

@Component
public class ScheduledTasks { //输出时间格式
private static final SimpleDateFormat format = new SimpleDateFormat("HH(hh):mm:ss S"); @Scheduled(initialDelay = 5000, fixedRate = 5000)
public void firstScheduledTasks(){
System.out.println("定时任务执行,现在时间是 : "+format.format(new Date()));
}
}

结果图:

项目参考地址

项目参考地址 : https://github.com/FunriLy/springboot-study/tree/master/%E6%A1%88%E4%BE%8B4

注意:

cron、fixedDelay、fixedRate 三者之间不能共存!!!
会抛出一个错误:

//Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'firstScheduledTasks': Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required.

官方文档

实例文档 : http://spring.io/guides/gs/scheduling-tasks/
注解文档 : http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

spring boot 学习(八)定时任务 @Scheduled的更多相关文章

  1. 【spring boot】使用定时任务@Scheduled 报错:Encountered invalid @Scheduled method 'dealShelf': Cron expression must consist of 6 fields (found 7 in "0 30 14 * * ? *")

    在spring boot中使用使用定时任务@Scheduled 报错: org.springframework.beans.factory.BeanCreationException: Error c ...

  2. Spring boot 学习八 Springboot的filter

    一:  传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...

  3. 整理了八个开源的 Spring Boot 学习资源

    Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 3 次,Spring Boot 的受 ...

  4. 【spring boot】spring boot中使用定时任务配置

    spring boot中使用定时任务配置 =============================================================================== ...

  5. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  6. Spring Boot(九):定时任务

    Spring Boot(九):定时任务 一.pom包配置 pom包里面只需要引入springboot starter包即可 <dependencies> <dependency> ...

  7. spring boot 基础篇 -- 定时任务

    在日常项目中,常常会碰到定时监控项目中某个业务的变化,下面是spring boot 集成的定时任务具体配置: @Component public class IndexWarningScheduled ...

  8. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

  9. Spring Boot学习大全(入门)

    Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...

随机推荐

  1. 20165310_java_blog_week1.md

    2165310 <Java程序设计>第1周学习总结 教材学习内容总结 了解Java基本配置环境 学习Java命名.编译.运行规则 拓展学习包的编译.运行 熟悉Git的运用方式 教材学习中的 ...

  2. 李白打酒|2014年蓝桥杯B组题解析第三题-fishers

    李白打酒 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上,他一共遇到店5次,遇到花 ...

  3. 洛谷P2777 [AHOI2016初中组]自行车比赛

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. Linux——进程管理简单学习笔记(二)

    计划任务: 为什么要设置计划任务 : 实现数据库备份,发送系统通知神马的..... 计划任务的命令: 一: at :  安排作业在某一时刻执行一次 nbatch    安排作业在系统负载不重时执 行一 ...

  5. python 文件分割

    import sys,os def split(fromfile,todir,chunksize): partnum = inputfile = open(fromfile,'rb')#open th ...

  6. C++学习笔记(一)——一个字符串分割和统计的工具(TextUtils)

    第一讲先从一个实例开始——我们需要完成一个遍历文件并统计单词出现次数的任务.分解功能:首先,按行读取文件并舍弃可能的空行.其次,将每一行都按照空格划分单词.因为可能存在标点符号,我们还需要将标点符号都 ...

  7. URAL 1635 Mnemonics and Palindromes

    URAL 1635 思路:区间dp+贪心,先n^2处理出每段区间是否是回文串,然后贪心地找每一段1到i的最少分割. 代码: #include<bits/stdc++.h> using na ...

  8. Codeforces 847I - Noise Level

    847I - Noise Level 思路:bfs. 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

  9. Codeforces 837D - Round Subset(dp)

    837D - Round Subset 思路:dp.0是由2*5产生的. ①dp[i][j]表示选i个数,因子2的个数为j时因子5的个数. 状态转移方程:dp[i][j]=max(dp[i][j],d ...

  10. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...