11、SpringBoot------定时任务




开发工具:STS
代码下载链接:https://github.com/theIndoorTrain/Springboot/tree/52ef6c0c805913db1e66ed18671c322e284233f0
前言:
之前我们有讲过Quartz任务调度。
现在,我们来讲解下Spring自己的任务调度。
在Springboot项目中,这种超级简单的调度方式将会给我们带来便利。
任务一:
5s后开始,每10s执行一次,执行一次任务要2s;
1.在启动类中开启任务调度,加上@EnableScheduling
package com.xm; import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling
@SpringBootApplication
public class Demo0061Application { public static void main(String[] args) {
SpringApplication.run(Demo0061Application.class, args);
System.out.println("Start Job:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
2.定义任务
package com.xm.job; import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class HelloJob { @Scheduled(initialDelay=5000,fixedRate=10000)
public void run() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("hello world");
} }
3.运行结果截图

4.分析
1.initialDelay=5000:任务开始时间为5s以后
2.fixedRate=10000:任务每10s之内完成一次
在这里任务时间是小于fixedRate
现在我们让任务运行时间改为11s
@Scheduled(initialDelay=5000,fixedRate=10000)
public void run() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
try {
Thread.sleep(11000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("hello world");
}
运行结果为:
任务二:
5s后开始,每间隔10s执行一次,执行一次任务要2s;
1.定义任务
@Scheduled(initialDelay=5000,fixedDelay=10000)
public void run() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
try {
//Thread.sleep(11000);
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("hello world");
}
2.运行结果截图:

分析:
fixedDelay:每次任务完成结束后再开始计时
任务三:
每天16:00:00开始到16:21:59每5s执行一次
1.定义任务
@Scheduled(cron="0/5 0-21 16 * * ?")
public void run() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
try {
//Thread.sleep(11000);
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("hello world");
}
2.运行结果截图:

3.分析
这里运用到了Cron表达式,请点击这里查看详解。3.初识Cron表达式
2018-07-16
11、SpringBoot------定时任务的更多相关文章
- SpringBoot定时任务@Scheduled
SpringBoot定时任务主要由两个注解完成. @Scheduled加在方法上面. @EnableScheduling加在类上面.可以是Application类,也可以是@Component类,还可 ...
- SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)
最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...
- SpringBoot定时任务 - 开箱即用分布式任务框架xxl-job
除了前文介绍的ElasticJob,xxl-job在很多中小公司有着应用(虽然其代码和设计等质量并不太高,License不够开放,有着个人主义色彩,但是其具体开箱使用的便捷性和功能相对完善性,这是中小 ...
- springboot 定时任务部署至linux服务器上后会执行两次问题
springboot定时任务在本地运行时,正常执行且只执行一次,但是在maven打包成war包,部署至linux服务器上之后,定时任务奇怪的执行了两次. 由于未做负载均衡,所以可以先排除是因为多台服务 ...
- Spring boot(三) springboot 定时任务
这个不多说,springboot 定时任务非常简单就可以实现了. 30s运行一次 , @Scheduled(cron="0,30 * * * * ?") 通过这个控制定时时间 cr ...
- springboot定时任务之旅
springboot定时任务 假设场景:单体应用的定时任务,假设我们已经有了一个搭建好的springboot应用,但是需要添加一个定时执行的部分(比如笔者遇到的是定时去请求一个接口数据来更新某个表), ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解
笔记 1.SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- SpringBoot定时任务 - 什么是ElasticJob?如何集成ElasticJob实现分布式任务调度?
前文展示quartz实现基于数据库的分布式任务管理和job生命周期的控制,那在分布式场景下如何解决弹性调度.资源管控.以及作业治理等呢?针对这些功能前当当团队开发了ElasticJob,2020 年 ...
- springboot定时任务,去掉指定日期
今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日. 网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html ...
随机推荐
- Integer代码分析
我们都知道Integer是int的封装类,提供了一些类型转换等工具方法,有一个-128-127的缓存,而且是final的. ----------------------------- 干货: Inte ...
- DEDE模板中如何运行php脚本和php变量的使用
在使用dede模板的时候,经常会需要直接对dede数据库的底层字段进行处理,如果dede中没有相应的函数的时候,往往就需要我们想办法来处理了. 举例:我想取出数据表addonimages中的某一条记录 ...
- C#多线程进度条
public class ZyjProgressBar : System.Windows.Forms.ProgressBar { //用于跨线程访问控件的委托 private delegate voi ...
- IntelliJ IDEA实时模板变量
返回由当前方法返回的值的类型IntelliJ IDEA 实时模板中的模板变量允许用户输入.扩展模板后,变量将作为输入字段显示在编辑器中. IntelliJ IDEA 声明实时模板变量 模板中的变量以下 ...
- Spring IOC + AOP 的实现
Spring思想很不错,尽量减少侵入式编程.现在了解到的Spring提供的功能有,DI,IOC,数据库操作,AOP,MVC.针对DI,AOP写了一些小DEMO PS:AOP真的很棒 代码参考:< ...
- C语言函数调用简简介
1.函数的声明: 在编写程序时,首先要对函数进行声明,然后对函数进行定义: 函数的声明是要让编译器知道函数的名称.参数.返回值类型等信息: 函数的定义是要让编译器知道函数的功能: 函数声明的格式由函数 ...
- one + two = 3
读入两个小于100的正整数A和B,计算A+B.需要注意的是:A和B的每一位数字由对应的英文单词给出. 输入 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =", ...
- mysql测试和sysbench工具详解
前言 作为一名后台开发,对数据库进行基准测试,以掌握数据库的性能情况是非常必要的.本文介绍了MySQL基准测试的基本概念,以及使用sysbench对MySQL进行基准测试的详细方法. 文章有疏漏之处, ...
- selenium鼠标拖动
var builder = new Actions(_driver); builder.MoveToElement(_driver.GetElementByCssSelector("#com ...
- IIS 发表web 之后,访问注册表项失败得问题
错误: 对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\xx\xxxx\xxxxx”的访问被拒绝. 解决办法: 打开IIS,找到应用程序池,然后找到自己web使用得程序池,右键高级 ...

