springboot(五)Scheduling demo
在项目开发过程中,经常会使用到定时任务(跑批),springboot默认已经实现了,只需要添加相应的注解就可以实现
在启动类上加入注解,开启定时任务
@SpringBootApplication
@EnableScheduling
public class App { public static void main(String[] args){
SpringApplication.run(App.class, args);
} }
创建跑批任务并注册到spring中管理,并在方法上加上跑批注解,配置core表达式
@Component
public class SchedulingTask1 { private int count = 0; @Scheduled(cron = "*/5 * * * * ?")//每5s执行一次
public void process(){
System.out.println("SchedulingTask1 is "+ count+"times run");
count++;
}
}
@Component
public class SchedulingTask2 { SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd hh:mm:ss"); @Scheduled(fixedRate = 5000)//每5s执行一次
public void process(){
System.out.println("process->This time is "+ sdf.format(new Date()) +"times run");
} @Scheduled(fixedDelay = 10000)//上一次执行完毕时间点之后10秒再执行
public void process2(){
System.out.println("process2->This time is "+ sdf.format(new Date()) +"times run");
} @Scheduled(initialDelay=10000, fixedRate=5000) //第一次延迟10秒后执行,之后按fixedRate的规则每5秒执行一次
public void process3(){
System.out.println("process3->This time is "+ sdf.format(new Date()) +"times run");
}
}
启动项目即可运行,运行结果:
process->This time is 20200510 11:43:37times run
process2->This time is 20200510 11:43:37times run
SchedulingTask1 is 0times run
process->This time is 20200510 11:43:42times run
SchedulingTask1 is 1times run
process->This time is 20200510 11:43:47times run
process3->This time is 20200510 11:43:47times run
process2->This time is 20200510 11:43:47times run
SchedulingTask1 is 2times run
process->This time is 20200510 11:43:52times run
process3->This time is 20200510 11:43:52times run
SchedulingTask1 is 3times run
process->This time is 20200510 11:43:57times run
process3->This time is 20200510 11:43:57times run
process2->This time is 20200510 11:43:57times run
SchedulingTask1 is 4times run
process->This time is 20200510 11:44:02times run
process3->This time is 20200510 11:44:02times run
SchedulingTask1 is 5times run
process->This time is 20200510 11:44:07times run
process3->This time is 20200510 11:44:07times run
process2->This time is 20200510 11:44:07times run
SchedulingTask1 is 6times run
process->This time is 20200510 11:44:12times run
process3->This time is 20200510 11:44:12times run
SchedulingTask1 is 7times run
process->This time is 20200510 11:44:17times run
process3->This time is 20200510 11:44:17times run
process2->This time is 20200510 11:44:17times run
SchedulingTask1 is 8times run
常用表达式如下:
0 * * * * ? 每1分钟执行一次
0 0 * * * ? 每天每1小时执行一次
0 0 10 * * ? 每天10点执行一次
0 * 14 * * ? 在每天下午14点到下午14:59期间的每1分钟执行一次
0 30 10 1 * ? 每月1号上午10点30执行一次
0 10 10 10 * ? 每月10日上午10:10执行一次 */5 * * * * ? 每隔5秒执行一次
0 */1 * * * ? 每隔1分钟执行一次
0 0 12-15 * * ? 每天12-15点整点执行一次
0 0/5 * * * ? 每5分钟执行一次
0 0-5 15 * * ? 在每天下午15点到下午15:05期间的每1分钟执行一次
0 0/10 15 * * ? 在每天下午15点到下午15:50期间的每10分钟执行一次
0 0/10 15,18 * * ? 在每天下午15点到15:50期间和下午18点到18:50期间的每10分钟执行一次
0 0/30 10-15 * * ? 在每天上午10点到下午15:30每半小时执行一次
0 0 10,12,14 * * ? 每天上午10点,下午12点,14点执行一次
表达式生成、解析、反解析地址https://cron.qqe2.com/ https://qqe2.com/cron 示例如下
springboot(五)Scheduling demo的更多相关文章
- SpringBoot(五)_表单验证
SpringBoot(五)_表单验证 参数校验在我们日常开发中非常常见,最基本的校验有判断属性是否为空.长度是否符合要求等,在传统的开发模式中需要写一堆的 if else 来处理这些逻辑,很繁琐,效率 ...
- springboot(五)过滤器和拦截器
前言 过滤器和拦截器二者都是AOP编程思想的提现,都能实现诸如权限检查.日志记录等.二者有一定的相似之处,不同的地方在于: Filter是servlet规范,只能用在Web程序中,而拦截器是Sprin ...
- springBoot+mybatisPlus小demo
项目介绍:采用restful api进行接口规范 / 项目框架SpringBoot+mybatis Plus / 采用mysql进行数据存储 / 采用swaggerUI进行前后端业务分离式开发. 开发 ...
- Java学习之SpringBoot整合SSM Demo
背景:在Java Web中Spring家族有着很重要的地位,之前JAVA开发需要做很多的配置,一堆的配置文件和部署调试一直是JavaWeb开发中的一大诟病,但现在Spring推出了SpringBoot ...
- 第2章 构建springboot工程 2-1 构建SpringBoot第一个demo
以后的趋势肯定是以一个微服务为主导的, Spring-Boot的指导 Maven整个环境构建之前的整个项目其实是一个很普通的J2SE项目,它构建完之后会进行重构,重构为Maven的一个项目路径.可以看 ...
- SpringBoot整合Swagger2(Demo示例)
写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...
- SpringBoot(五) -- SpringBootWeb登录示例
一.解决index.html访问 在SpringBoot中默认访问的首页是静态资源文件夹下的index.html,无法被Thymeleaf模板引擎解析,因此我们可以定义一个controller将默认请 ...
- SpringBoot(五):@ConfigurationProperties配置参数绑定
在springmvc或其他ssh框架中如果我们要实现一个配置参数的加载,需要使用代码实现读取properties文件等操作,或者需要使用其他属性@value(name="username&q ...
- spring-boot(五) RabbitMQ详解 定时任务
学习文章来自:springboot(八):RabbitMQ详解 springboot(九):定时任务 RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分 ...
随机推荐
- Poj-P3468题解【线段树】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=3468 题目描述: 给N个数A1, A2 ...
- xtrabakcup基本用法 安装、全量备份恢复、增量备份恢复
xtrabackup备份原理以及工作流程 备份流程日志分析:1.##读取mysql配置文件2.## 扫描innodb日志lsn并复制inndodb系统表空间3.## 缓冲写出到数据文件并锁表4.## ...
- 攻击JWT的一些方法
JWT安全隐患之绕过访问控制 https://mp.weixin.qq.com/s/xe8vOVhaysmgvxl-A3nkBA 记录一次JWT的越权渗透测试 https://mp.weixin.qq ...
- map 传递给函数的代价
https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/08.1.md map 传递给函数的代价很小:在 32 位机器上占 4 ...
- (004)每日SQL学习:物化视图之二
一. 物化视图概述 Oracle的物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表.物化视图存储基于远程表的数据,也可以称为快照. 物化视图可 ...
- Redis集群动态增加和删除节点
一.添加节点 1.首先将需要添加的节点启动: 这里启动redis6383.conf和redis6393.conf两个节点 查看原有节点: 3个主节点所对应的哈希槽(hash slo ...
- 安装、登入centos7
系统CentOS7.4 http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1708.iso 虚 ...
- Elasticsearch从0到千万级数据查询实践(非转载)
1.es简介 1.1 起源 https://www.elastic.co/cn/what-is/elasticsearch,es的起源,是因为程序员Shay Banon在使用Apache Lucene ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
- MVC架构 项目实践
MVC MVC架构程序的工作流程 springmvc 中dao层和service层的区别 项目实践 项目目录 项目实现流程 JSP登录页面View层 LoginServletjavaControlle ...