spring boot 实现定时任务
定时任务或者说定时调度,是系统中比较普遍的一个功能,例如数据归档、清理,数据定时同步(非实时),定时收发等等都需要用到定时任务,常见的定时调度框架有Quartz、TBSchedule等。
如何在Spring boot里实现定时任务呢?
SpringBoot定时方式有很多种,我才用 @Scheduled 注解配置定时任务
Spring自3.0版本起也增加了任务调度功能Schedule,它是一个轻量级的Quartz,使用起来方便、简洁,且不需要依赖其他的JAR包。
@EnableScheduling开启计划任务支持,在入口类中添加
@Scheduled计划任务声明,在具体方法中添加
eg:
package com.tydt.bim.common; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date;
@EnableScheduling
@Component
@EnableAsync
public class ScheduleSetting {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /**
* fixedRate多久执行一次,单位:毫秒
* initialDelay 延迟容器启动后执行,单位:毫秒
*/
@Async
@Scheduled(fixedRate = 10000,initialDelay = 2000)
public void scheduleTest1(){
System.out.println("10s执行一次--->"+sdf.format(new Date()));
} /**
* fixedDelay在指定的时间执行一次,单位:毫秒
* 在创建后会执行一次,在第一次执行后,会每隔5S执行一次
*/
@Async
@Scheduled(fixedDelay = 5000)
public void scheduleTest2(){
System.out.println("5s执行一次--->"+sdf.format(new Date()));
} /**
* cron表达式,下面表示,每隔20s执行一次,具体的可以自行定义
*/
@Async
@Scheduled(cron = "0/20 * * * * ? ")
public void scheduleTest3(){
System.out.println("20执行一次--->"+sdf.format(new Date()));
}
}
说明:
@Scheduled 参数可以接受两种定时的设置
表示每隔五秒执行一次
(1)fixedRate = 5000
(2)cron = "0/5 * * * * ? "
cron表达式
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。按顺序依次为:
秒(0~59)
分钟(0~59)
3 小时(0~23)
4 天(0~31)
5 月(0~11)
6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于”月份中的日期”和”星期中的日期”这两个元素互斥的,必须要对其中一个设置。配置实
例:
/5 * * * ? 每隔5秒执行一次
0 /1 * * ? 每隔1分钟执行一次
0 0 10,15,18 * * ? 每天上午10点,下午3点,6点
0 0 12 * * ? 每天中午12点触发
0 15 10 * * ? * 每天上午10:15触发
@EnableAsync开启多线程
@Async标记其为一个异步任务
注:
自带的Schedule不支持分布式部署
spring boot 实现定时任务的更多相关文章
- Spring Boot配置定时任务
在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...
- 【Spring Boot】定时任务
[Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...
- spring boot 创建定时任务
@Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...
- Spring Boot:定时任务
在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom 包配置 pom 包里面只需要引入 Spring ...
- Spring boot创建定时任务
基于spring boot的应用创建定时任务不要太简单,给一个类加上@Configuration @EnableScheduling注解,然后给该类需要定时执行的方法加上@Scheduled(cron ...
- Spring Boot (29) 定时任务
使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取 定时任务概述 定时任务:顾名思义就是在特定/指 定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务. 实现方式: 1.Timer:JDK ...
- 【Spring Boot学习之六】Spring Boot整合定时任务&异步调用
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2一.定时任务1.启动类添加注解@EnableScheduling 用于开启定时任务 package com.wjy; i ...
- Spring Boot 实现定时任务的 4 种方式
作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...
- Spring Boot Scheduled定时任务特性
SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...
随机推荐
- Nginx——配置文件服务下载
前言 只是临时搭建的一个下载服务,所以就直接用nginx来咯 步骤 解析域名 将域名解析到要部署应用对应的服务器,就是个解析操作,没啥好讲的 创建目录 # mkdir /data/install/ 配 ...
- oracle中删除表:drop、delete、truncate
相同点,使用drop delete truncate 都会删除表中的内容 drop table 表名 delete from 表名(后面不跟where语句,则删除表中所有的数据) truncate t ...
- wordpress下一篇next_post_link函数的使用方法
我们在用wordpress开发时经常会用到上一篇下一篇的功能,<?php previous_post_link('%link') ?> <?php next_post_link('% ...
- Pycharm激活方法使用的是(license server)
pycharm所有版本 http://www.jetbrains.com/pycharm/download/previous.html打开激活窗口 选择 Activate new license wi ...
- Linux计划作业练习
1.crontab -eu zh //每天晚上10天提醒用户可以去睡觉了 * */10 * * * go to sleep 2.查询crontab的工作内容 3.当crontab命令格式出错时 ...
- Boggle Game
Description Given a board which is a 2D matrix includes a-z and dictionary dict, find the largest co ...
- Bomb Enemy
Description Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number z ...
- Python3.x保留字(33个)
- 关于STM32 Flash的一些问题
注:本人感觉是STM32 Flash本身的问题. 最近做STM32的远程升级,保存到Flash里面,用于记录更新状态的信息总是无故的清理掉 最终测试发现 STM32的 Flash 擦除操作 并不一定会 ...
- Linux环境下安装Redis
记录一下Linux环境下安装Redis,按顺序执行即可,这里下载的是Redis5,大家可根据自己的需求,修改版本号就好了,亲测可行. 1.下载Redis安装包cd /usr/local/wget ht ...