SpringBoot(十七)-- 定时任务
日常开发中,经常会使用定时任务来执行跑批,springboot默认已经帮助我们整合了定时任务。
参考:https://blog.csdn.net/u013845177/article/details/78244524
1.启动类启用定时
在启动类上添加@EnableScheduling
2.创建定时任务实现类
package com.xsjt.timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* ClassName:SchedulerTask
* 定时任务
* Date: 2018年4月22日 下午5:48:34
* @author Joe
* @version
* @since JDK 1.8
*/
@Component
public class SchedulerTask { /**
* executeTask:(每5秒中执行一次).
* @author Joe
* Date:2018年4月22日下午5:51:34
*/
@Scheduled(cron = "0/5 * * * * *")
public void executeTask() {
System.out.println("--executeTask方法,每隔5秒执行一次,当前时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
} /**
* executeTask2
* 在上一次执行完之后等待xxx毫秒(xxx就是fixedDelay = 5000中的5000)再执行,循环下去 ,
* 上一次执行多久都没关系 ,反正上一次执行完后xxx毫秒我才执行
* @author Joe
* Date:2018年4月22日下午7:03:11
*/
@Scheduled(fixedDelay = 5000)
public void executeTask2() {
System.out.println("**executeTask2方法, 执行一次,当前时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
} /**
* fixedRate 举个例子:比如:假设有5个执行时间点 间隔是5000毫秒:分别是:
* T1:14:00:00
* T2:14:00:05
* T3:14:00:10
* T4:14:00:15
* T5:14:00:20
* 如果T1执行时间花了4秒,也就是到了14:00:04,那么你会看到14:00:05分就开始执行了T2,很正常,此时T1结束时间和T2开始时间只差1000毫秒,没毛病
* 如果T1执行时间花了8秒,怎么办?这时T1执行完的时间是14:00:08,已经覆盖了T2的时间,T2在14:00:05到14:00:08是等等状态。现在是14:00:08,看起来接着是T3执行,
* 但实际不是,而是立马执行T2,立马执行T2,立马执行T2(T2说:我不管,T1搞我超时了,我无论也是执行),这时你会发现T2的执行时间(也就是第2次执行时间 )是:14:00:08,真的是立马。。。
* 如此类推,只要时执行时间被覆盖了的,到它了就立马执行
*/
@Scheduled(fixedRate = 5000)
public void executeTask3() {
System.out.println(">>executeTask3方法, 一次,当前时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3.源码下载
https://gitee.com/xbq168/spring-boot-learn
SpringBoot(十七)-- 定时任务的更多相关文章
- 玩转SpringBoot之定时任务详解
序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- SpringBoot 配置定时任务
SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...
- SpringBoot - 添加定时任务
SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...
- springboot之定时任务
定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...
- SpringBoot整合定时任务和异步任务处理 3节课
1.SpringBoot定时任务schedule讲解 定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- 十三、springboot集成定时任务(Scheduling Tasks)
定时任务(Scheduling Tasks) 在springboot创建定时任务比较简单,只需2步: 1.在程序的入口加上@EnableScheduling注解. 2.在定时方法上加@Schedule ...
- SpringBoot创建定时任务
之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单. (1 ...
- springboot开启定时任务 添加定时任务 推送
最近在自学Java的springboot框架,要用到定时推送消息.参考了网上的教程,自己调试,终于调好了.下面将网上的教程归纳下,总结复习下. springboot开启定时任务 在SpringBo ...
- (入门SpringBoot)SpringBoot结合定时任务task(十)
SpringBoot整合定时任务task 使用注解EnableScheduling在启动类上. 定义@Component作为组件被容器扫描. 表达式生成地址:http://cron.qqe2.com ...
- SpringBoot整合定时任务和异步任务处理
SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...
随机推荐
- __getattr__和__setattt__使用
# coding:utf-8 """ __setattr__(self, name, value),如果要给name赋值,调用此方法 __getattr__(self, ...
- JVM Debugger Memory View for IntelliJ IDEA
Posted on August 19, 2016 by Andrey Cheptsov Every day we try to find new ways to improve developer ...
- DHCP服务原理与搭建(Linux系统+路由器,二选一方案)
大家都知道上网的最基本前提是要在终端上设置IP.子网掩码.网关.DNS等地址信息,在家里或者在办公室很多时候打开电脑后发现就可以上网,并没有手动设置IP.掩码.DNS地址也能上网,这是什么原因呢?其实 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(三十):使用flatMapGroupsWithState替换agg
flatMapGroupsWithState的出现解决了什么问题: flatMapGroupsWithState的出现在spark structured streaming原因(从spark.2.2. ...
- [Algorithm] Calculate Pow(x,n) using recursion
Asking you to implement the Math.pow method The navie implemenation can be: // O(N) const pow1 = (x, ...
- vmware中如何检查cpu的使用状况-一个考题引发的思考
来自一个VCP的考题,有点兴趣.可参看: 如何在VMware里使用esxtop? http://thocm.com/a/caozuoxitongzixun/xunihuazonghezixun/VMw ...
- efcore数据库自动生成
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. p ...
- Gson解析第三方提供Json数据(天气预报,新闻等)
之前都是自己写后台,自己的server提供数据给client. 近期在看第三方的数据接口,訪问其它站点提供的信息.比方.我们可能自己收集的数据相当有限.可是网上提供了非常多关于天气预报.新闻.星座运势 ...
- 使用LinkedHashMap来实现一个使用LRU(Least Recently Used)算法的cache
removeEldestEntry在使用put或者putAll方法插入一个新的entry到map中时被调用,是否要删除年老的entry取决于是否满足既定的条件(比如本例中的条件:MAP中entry数量 ...
- JMX-Java Management Extensions
JMX全称Java Management Extensions, 为Java应用提供管理扩展功能.在Java 5的时候引入 概念 Name Description MBean 全称为Managed B ...