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. c++ 11开始语言本身和标准库支持并发编程

    c++ 11开始语言本身和标准库支持并发编程,意味着真正要到编译器从语言和标准库层面开始稳定,估计得到17标准出来.14稳定之后的事情了,根据历史经验,新特性的引入到稳定被广泛采用至少要一个大版本的跨 ...

  2. 01: html常用标签

    目录: 1.1 web开发的三把利器介绍 1.2 网页头部head标签中几个常用标签 1.3 html常用标签归类 1.4 input系列标签 1.5 HTML其他标签 1.1 web开发的三把利器介 ...

  3. Node-webkit 安装使用npm安装模块方法

    原文链接:http://jingyan.baidu.com/article/5225f26b5aaa20e6fa0908a6.html package.json可以放在软件根目录下,也可以放在项目目录 ...

  4. 20145104张家明 《Java程序设计》第7周学习总结

    20145104张家明 <Java程序设计>第7周学习总结 教材学习内容总结 第13章 简单认识时间和日期 -时间的度量:GMT.UT.TAI.UTC.Unix.epoch. -UTC:保 ...

  5. bootstrap的 附加导航Affix导航 (侧边窄条式 滚动监控式导航) 附加导航使用3.

    affix: 意思是粘附, 附着, 沾上. 因此, 附加导航就是 bootstrap的 Affix.js组件. bootstrap的 附加导航, 不是说导航分成主导航, 或者什么 副导航的 而是指, ...

  6. 复习_for循环嵌套 及 流程图梳理

    原题: 使用for循环输出如下图形: * ** *** **** 分析及做法. 图链接: https://www.processon.com/view/link/5afba745e4b05352c2b ...

  7. shell 跳出循环

    跳出循环 break命令 例: #!/bin/bash while : do echo -n "输入 1 到 5 之间的数字:" read aNum case $aNum in 1 ...

  8. shell :

    示例一.(用作注释,占位)#!/bin/bash : this is single line comment : 'this is a multiline comment, second line e ...

  9. 会员通过消费攒积分,升级RENEW以及降级的需求

    需求看上去及其简单,如下: 用文字描述就开始不容易了. 先按等级排个序,根据下一个等级,推前一个等级: --C---B----V-----A 在计算一下升级需要的积分:--C表示普通会员-----需要 ...

  10. matplotlib-绘制精美的图表

    matplotlib库链接:http://matplotlib.org/gallery.html matplotlib绘制精美图标链接:http://old.sebug.net/paper/books ...