在项目开发过程中,经常会使用到定时任务(跑批),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的更多相关文章

  1. SpringBoot(五)_表单验证

    SpringBoot(五)_表单验证 参数校验在我们日常开发中非常常见,最基本的校验有判断属性是否为空.长度是否符合要求等,在传统的开发模式中需要写一堆的 if else 来处理这些逻辑,很繁琐,效率 ...

  2. springboot(五)过滤器和拦截器

    前言 过滤器和拦截器二者都是AOP编程思想的提现,都能实现诸如权限检查.日志记录等.二者有一定的相似之处,不同的地方在于: Filter是servlet规范,只能用在Web程序中,而拦截器是Sprin ...

  3. springBoot+mybatisPlus小demo

    项目介绍:采用restful api进行接口规范 / 项目框架SpringBoot+mybatis Plus / 采用mysql进行数据存储 / 采用swaggerUI进行前后端业务分离式开发. 开发 ...

  4. Java学习之SpringBoot整合SSM Demo

    背景:在Java Web中Spring家族有着很重要的地位,之前JAVA开发需要做很多的配置,一堆的配置文件和部署调试一直是JavaWeb开发中的一大诟病,但现在Spring推出了SpringBoot ...

  5. 第2章 构建springboot工程 2-1 构建SpringBoot第一个demo

    以后的趋势肯定是以一个微服务为主导的, Spring-Boot的指导 Maven整个环境构建之前的整个项目其实是一个很普通的J2SE项目,它构建完之后会进行重构,重构为Maven的一个项目路径.可以看 ...

  6. SpringBoot整合Swagger2(Demo示例)

    写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...

  7. SpringBoot(五) -- SpringBootWeb登录示例

    一.解决index.html访问 在SpringBoot中默认访问的首页是静态资源文件夹下的index.html,无法被Thymeleaf模板引擎解析,因此我们可以定义一个controller将默认请 ...

  8. SpringBoot(五):@ConfigurationProperties配置参数绑定

    在springmvc或其他ssh框架中如果我们要实现一个配置参数的加载,需要使用代码实现读取properties文件等操作,或者需要使用其他属性@value(name="username&q ...

  9. spring-boot(五) RabbitMQ详解 定时任务

    学习文章来自:springboot(八):RabbitMQ详解 springboot(九):定时任务 RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分 ...

随机推荐

  1. requests模块的基本使用

    requests模块的基本使用 基于网络请求的模块. 环境的安装:pip install requests 作用:模拟浏览器发起请求 分析requests的编码流程: 1.指定url 2.发起了请求 ...

  2. 超精讲-逐例分析 CSAPP:实验2-Bomb!(下)

    好了话不多说我们书接上文继续来做第二个实验下面是前半部分实验的连接 5. 第五关 首先感觉应该是个递归问题 /* Round and 'round in memory we go, where we ...

  3. Linux性能监测(系统监测统计命令详解)

    通过这个命令,可以最简便的看出系统当前基本状态信息,这里面最有用是负载指标,如果你还想查看当前系统的CPU/内存以及相关的进程状态,可以使用TOP命令. TOP 通过TOP命令可以详细看出当前系统的C ...

  4. C#高级编程第11版 - 第四章 索引

    [1]4.2 继承的类型 1.C#不支持类的多继承,但它支持一个接口继承自多个接口. 2.单继承:单继承允许一个类继承自另外一个基类,C#支持. 3.多级继承:多级继承允许创建一个类继承自它的父类,而 ...

  5. echarts图表X轴文字过长解决解决方案:根据文字长度自动旋转

    Echarts 标签中文本内容太长的时候怎么办 ? 关于这个问题搜索一下,有很多解决方案.无非就是 省略(间隔显示).旋转文字方向.竖排展示 前面两种解决方案,就是echarts暴露的: {   ax ...

  6. 将ffmpeg编译为wasm版本且在浏览器中运行

    2020年大前端技术趋势解读 原创 IMWeb团队 腾讯IMWeb前端团队 5天前

  7. Pulsar Pub/Sub Messaging

    The Apache Software Foundation Announces Apache Pulsar as a Top-Level Project : The Apache Software ...

  8. 熟悉而陌生API:Promise

    前言 ES6 发布到现在差不多有5年时间了.在这5年时间里ES6摧枯拉朽般的将现代前端"改朝换代",Promise是其中"大将"般的存在,影响着无数的前端库和A ...

  9. 20201104gryz模拟赛解题报告

    写在前面 \(Luckyblock\) 良心出题人, 题面好评 T1还是蛮简单的,用一个栈来维护就能过(某天听说 \(Luckyblock\) 出了套题,T1是个考栈的,看来就是这道了 注:栈的清空只 ...

  10. loj10018数的划分

    题目描述 将整数 n 分成 k 份,且每份不能为空,问有多少种不同的分法.当 n=7,k=3 时,下面三种分法被认为是相同的:1 1 5;1 5 1 ;5 1 1 输入格式 一行两个数 n ,k . ...