spring boot 学习(八)定时任务 @Scheduled
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的更多相关文章
- 【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 ...
- Spring boot 学习八 Springboot的filter
一: 传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...
- 整理了八个开源的 Spring Boot 学习资源
Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 3 次,Spring Boot 的受 ...
- 【spring boot】spring boot中使用定时任务配置
spring boot中使用定时任务配置 =============================================================================== ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Spring Boot(九):定时任务
Spring Boot(九):定时任务 一.pom包配置 pom包里面只需要引入springboot starter包即可 <dependencies> <dependency> ...
- spring boot 基础篇 -- 定时任务
在日常项目中,常常会碰到定时监控项目中某个业务的变化,下面是spring boot 集成的定时任务具体配置: @Component public class IndexWarningScheduled ...
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...
- Spring Boot学习大全(入门)
Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...
随机推荐
- c++性能之map实现性能比较
http://www.cnblogs.com/zhjh256/p/6346501.html讲述了基本的map操作,在测试的时候,发现map的性能极为低下,与java相比相差了接近200倍.测试的逻辑如 ...
- java安全体系之JCA、JCE、JAAS、JSSE及其关系
首先.如果是运行在internet上的系统,并且如果是个涉及到利益性的系统,不可避免的会遭受各种攻击(我们公司的很多系统从OS到DB到webapp就实时有收到攻击和破解),所以尽可能保证安全性将不再是 ...
- sublime3 离线安装插件
直接去:https://packagecontrol.io/installation搜索插件,插件一般会有个git网址(格式化html的插件可以用这个:https://github.com/victo ...
- cmd命令安装、卸载、启动和停止Windows Servic
1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft.NET ...
- Linux多线程--使用信号量同步线程【转】
本文转载自:http://blog.csdn.net/ljianhui/article/details/10813469 信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过 ...
- UVa 11636 你好 世界!(贪心)
https://vjudge.net/problem/UVA-11636 题意: 经过一次复制,一条语句会变成两条语句,再经过一次变成四条语句...求最少需要复制几次能使条数恰好为n? 思路: 贪心水 ...
- XML序列化、反序列化
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- Python matplot画柱状图(一)
图的存在,让数据变得形象化.无论多么复杂的东西,都是简单的组合. import matplotlib.pyplot as plt import numpy as np dict = {'A': 40, ...
- ros python 订阅robot_pose
#!/usr/bin/env python import rospy import tf import time from tf.transformations import * from std_m ...
- python 重复元素排序
def counting_sort(array1, max_val): m = max_val + count = [] * m for a in array1: # count occurences ...