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 ...
随机推荐
- Unity 游戏对象的组件列表
描述: 1 个游戏对象,上面有 4 个组件, 如图: 脚本 Test_01 的内容,如下: using System.Collections; using System.Collections.Gen ...
- (转)Python作业day2购物车
Python作业day2购物车 原文:https://www.cnblogs.com/spykids/p/5163108.html 流程图: 实现情况: 可自主注册, 登陆系统可购物,充值(暂未实现) ...
- spring boot 配置redis
先配置属性: # database name spring.redis.database=0 # server host spring.redis.host=127.0.0.1 # server pa ...
- DEDE把变量放进session中,结果取值为null的问题
最近在基于织梦CMS(dedecms)做公司网站,可以说改动不少,而其中最令我印象深刻的就是织梦的session. 自己想在前台页面限制一些用户的访问,且后台用户可以访问.必须验证织梦后台用户的 ...
- 左侧栏与右侧内容之锚点、offsetHeight、scrollTop()
常用功能 1.点击左侧,右侧相关内容随时点到. 2.滚动右侧信息,左侧标题随之显示背景. 第一点很简单,只要在左侧栏 <li><a href="#aaa"&g ...
- jar包介绍
1.基本jar包 4+1:4个核心(beans+core+context+expression)+一个依赖(commons-logging...)
- MVC中的验证码
下面是一个完整的mvc controller类 public class CodeController : Controller { private const string CODE = " ...
- Calendar计算一个月前的日期,踩坑记录
错误示范:calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);//获取一个月前的今天这种写法假设传入的日期为2019-03-3 ...
- SpingCloud微服务架构学习(一)之服务提供者与服务消费者
微服务构建的是分布式系统,各个微服务之间通过网络进行服务调用,这就有了服务提供者(被调用方)和服务消费者(调用方),以电影售票系统为例,假设服务调用关系如下图所示: 围绕此场景,我们先编写一个用户微服 ...
- wamp的安装
1.下载wamp. 2.如果安装了apache,先卸载. 进入到你的apache的bin目录,输入指令 httpd.exe -k stop停止服务,再输入httpd.exe -k uninstall. ...