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. java安全体系之JCA、JCE、JAAS、JSSE及其关系

    首先.如果是运行在internet上的系统,并且如果是个涉及到利益性的系统,不可避免的会遭受各种攻击(我们公司的很多系统从OS到DB到webapp就实时有收到攻击和破解),所以尽可能保证安全性将不再是 ...

  2. 20145339 Exp5 MS11_050

    20145339 Exp5 MS11_050 实验过程 使用命令msfconsole命令进入控制台 使用命令search ms11_050查看针对MS11_050漏洞的攻击模块 确定相应模块名之后,我 ...

  3. C++11标准 STL正则表达式 验证电子邮件地址

    转自:http://www.cnblogs.com/yejianfei/archive/2012/10/07/2713715.html 我们最经常遇到的验证,就是电子邮件地址验证.网站上常见.各种网页 ...

  4. git下载速度太慢【学习笔记】

    使用了sshFQ的伙伴添加这个配置下载速度有极大的提升. git config --global http.proxy 'socks5://127.0.0.1:1080'

  5. jz2440-linux3.4.2-kernel移植【学习笔记】【原创】

    平台:jz2440 作者:庄泽彬(欢迎转载,请注明作者) 说明:韦东山二期视频学习笔记 交叉编译工具:arm-linux-gcc (GCC)4.3.2 linux:linu3.4.2 PC环境:ubu ...

  6. POJ 1270 Following Orders(拓扑排序)题解

    Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...

  7. Harmonic Number (调和级数+欧拉常数)题解

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

  8. python工具

    目录 Python工具 1. argparse 2. tqdm 3. os, shutil 4. csv Python工具 1. argparse import argparse parser = a ...

  9. jquery扩展的两个方法与区别 $.extend $.fn.extend

    jQuery.extend:Query本身的扩展方法 jQuery.fn.extent(Object) jquery 所选对象扩展方法 jQuery.extend 我们先把jQuery看成了一个类,这 ...

  10. python 插入查找

    def interpolation_search(data,val): low= high=len(data)- print('查找过程中......') : mid=low+int((val-dat ...