Spring Boot设置定时任务
在 http://start.spring.io/ 中新建一个Group为com.zifeiy
,Artifact为task
的工程。
然后在TaskApplication中添加注释:@EnableScheduling
,用以开启定时任务:
package com.zifeiy.task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class TaskApplication {
public static void main(String[] args) {
SpringApplication.run(TaskApplication.class, args);
}
}
然后新建一个类用于部署定时任务:
package com.zifeiy.task.component;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TaskComponent {
@Scheduled(cron="0 * * * * *")
public void schedule1() {
System.out.println("schedule 1: " + new Date());
}
@Scheduled(fixedRate = 5000)
public void schedule2() {
System.out.println("schedule 2: " + new Date());
}
@Scheduled(fixedDelay = 5000)
public void schedule3() {
System.out.println("schedule 3: " + new Date());
}
@Scheduled(initialDelay=1000, fixedRate=5000)
public void schedule4() {
System.out.println("schedule 4: " + new Date());
}
}
- @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
- @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
- @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
- @Scheduled(cron="0 * * * * *") :通过cron表达式定义规则
注意,这里的时间,单位是毫秒,1秒=1000毫秒
cron属性
- 第一位,表示秒,取值0-59
- 第二位,表示分,取值0-59
- 第三位,表示小时,取值0-23
- 第四位,日期天/日,取值1-31
- 第五位,日期月份,取值1-12
- 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思 另外:1表示星期天,2表示星期一。
- 第7为,年份,可以留空,取值1970-2099
参考链接:
- https://blog.csdn.net/jinyangbest/article/details/79304343
- https://blog.csdn.net/weixin_39800144/article/details/79270120
Spring Boot设置定时任务的更多相关文章
- spring boot 创建定时任务
@Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...
- Spring Boot配置定时任务
在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...
- 【Spring Boot】定时任务
[Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...
- Spring Boot:定时任务
在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom 包配置 pom 包里面只需要引入 Spring ...
- 【Spring Boot学习之六】Spring Boot整合定时任务&异步调用
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2一.定时任务1.启动类添加注解@EnableScheduling 用于开启定时任务 package com.wjy; i ...
- spring boot 实现定时任务
定时任务或者说定时调度,是系统中比较普遍的一个功能,例如数据归档.清理,数据定时同步(非实时),定时收发等等都需要用到定时任务,常见的定时调度框架有Quartz.TBSchedule等. 如何在Spr ...
- Spring Boot 实现定时任务的 4 种方式
作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...
- Spring Boot 创建定时任务(配合数据库动态执行)
序言:创建定时任务非常简单,主要有两种创建方式:一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer). 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库 ...
- Spring Boot 设置静态资源访问
问题描述 当使用spring Boot来架设服务系统时,有时候也需要用到前端页面,当然就不可或缺地需要访问其他一些静态资源,比如图片.css.js等文件.那么如何设置Spring Boot网站可以访问 ...
随机推荐
- JQuery实践--Ajax
Ajax概览无需刷新用户页面而发起服务器请求的技术.创建一个XHR实例: var xhr if(window.XMLHttpRequest) { xhr = new XML ...
- Girls and Boys POJ - 1466 【(二分图最大独立集)】
Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...
- Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- [Luogu] 让我们异或吧
https://www.luogu.org/problemnew/show/P2420 异或满足 A ^ B = B ^ A A ^ A = 0 0 ^ A = A #include <cstd ...
- LOJ166 拉格朗日插值 2【卷积,NTT】
题目链接:LOJ 题目描述:输入多项式的次数$n$,一个整数$m$和$f(0),f(1),f(2),\ldots,f(n)$,输出$f(m),f(m+1),f(m+2),\ldots,f(m+n)$ ...
- pycham更换主题
在pycham的file-->Setting-->Editor-->Colors&Fonts
- python网络爬虫(一):网络爬虫的定义
网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛.网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一个页面(通常是首页)开 ...
- sql语句 基本
1.sql不区分大小写,一般结尾要加分号: 2.select 列,列,列 from 表 3.distinct ,返回列中不同的值.需要哪个列不同,关键词哪个列 4.where子句,select 列 f ...
- Oracle impdp导入数据报错:无法读取要读取的存储文件(Linux)
当向Linux下的Oracle11g通过数据泵impdp导入数据库时,出现如图所示错误. 错误原因:bdck.dmp该为大写. 切记:Linux系统严格区分大小写.
- 第04组 Beta冲刺(1)
队名:斗地组 组长博客:地址 作业博客:Beta冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...