Spring Boot (29) 定时任务
使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取
定时任务概述
定时任务:顾名思义就是在特定/指
定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务。
实现方式:
1.Timer:JDK自带的java.util.Timer;通过调度java.util.TimerTask的方式 让程序按照某一个频率执行,但不能在指定时间运行,一般使用较少。
2.ScheduledExecutorService:JDK1.5增加的,位于Java.util.concurrent包种,是基于线程池设计的定时任务类,每个调度任务都会被分配到线程池中,并发执行,互不影响。
3.Spring Task:spring 3.0以后新增了task,一个轻量级的Quartz,功能够用,用法简单。
4.Quartz:功能最为强大的调度器,可以让程序在指定时间执行,也可以按照某一个频率执行,他还可以动态开关,但是配置起来比较复杂,现如今开源社区中已经很多基于Quartz 实现的分布式定时任务项目。
Timer方式
基于Timer实现的定时调度,目前应用较少,不推荐使用
@GetMapping("/test")
public String test() { TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println("执行任务"+ LocalDateTime.now());
}
};
Timer timer = new Timer();
//参数1 需要执行的任务 参数2 延迟时间毫秒 参数3 间隔时间毫秒
timer.schedule(timerTask,5000,3000); return "test";
}
ScheduledExecutorService
基于ScheduledExecutorService实现的调度任务,它与TImer很类似,但它的效果更好,多线程并行处理定时任务时,Timer运行多个TimeTask时,只要其中有一个因任务报错没有捕获抛出的异常,其他任务便会自动终止运行,使用scheduledExecutorService可以规避这个问题
@GetMapping("/cheduled")
public String cheduled() { ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
//参数1具体执行的任务 2首次执行的延迟时间 3任务执行间隔 4间隔时间单位
service.scheduleAtFixedRate(()->System.out.println("执行任务"+LocalDateTime.now()),0,3, TimeUnit.SECONDS); return "cheduled";
}
Spring Task(关键)
导入依赖
在pom.xml中添加spring-boot-starter-web依赖即可,它包含了spring-context,定时任务相关的就属于这个JAR下的org.springframework.scheduling包中
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
定时任务
@Scheduled 定时任务的核心
cron:cron表达式,根据表达式循环执行,与fixedRate属性不同的是它将时间进行了切割
fixeRate:每隔多久执行一次,无视工作时间(@Scheduled(fixedRate = 1000))假设第一次工作时间为2018-06-15 00:00:00,工作时长为5秒,那么下次任务的时间就是 2018-06-15 00:00:05)
initialDelay:第一次执行延迟时间,只是做延迟的设定,与fixedDelay关系密切,配合使用。
@Async 代表任务可以进行一步工作,由原本的串行改为并行
package com.spring.boot.utils; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component
public class SpringTaskDemo {
@Async
@Scheduled(cron = "0/1 * * * * *")
public void scheduled1() throws InterruptedException {
Thread.sleep(3000);
System.out.println("scheduled1 每1秒执行一次" + LocalDateTime.now());
} @Scheduled(fixedRate = 1000)
public void scheduled2() throws InterruptedException {
Thread.sleep(3000);
System.out.println("scheduled2 每1秒执行一次" + LocalDateTime.now());
} @Scheduled(fixedRate = 3000)
public void scheduled3() throws InterruptedException {
Thread.sleep(5000);
System.out.println("scheduled3 航次执行完毕后间隔3秒继续执行" + LocalDateTime.now());
}
}
cron表达式在线生成:
http://www.pdtools.net/tools/becron.jsp
启动类中@EnableScheduling注解 表示开启对@Scheduled注解的解析;同时new ThreadPoolTaskScheduler()也是相当的关键,默认情况下的private volatile int poolSize = 1;这就导致了多个任务的情况下容易出现竞争情况(多个任务的情况下,如果第一个任务没执行完毕,后续的任务将会进入等待状态)。
@EnableAsync 代表开启@Async异步的解析,并行化运行
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class BootApplication{ public static void main(String[] args) {
SpringApplication.run(BootApplication.class,args);
} @Bean
public TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
return taskScheduler;
}
}
测试
启动项目 观察日志信息如下:
scheduled2 每1秒执行一次2018-06-14T17:33:42.245
scheduled1 每1秒执行一次2018-06-14T17:33:43.030
scheduled1 每1秒执行一次2018-06-14T17:33:44.009
scheduled3 航次执行完毕后间隔3秒继续执行2018-06-14T17:33:44.244
scheduled1 每1秒执行一次2018-06-14T17:33:45.011
scheduled2 每1秒执行一次2018-06-14T17:33:45.249
scheduled1 每1秒执行一次2018-06-14T17:33:46.008
scheduled1 每1秒执行一次2018-06-14T17:33:47.008
scheduled1 每1秒执行一次2018-06-14T17:33:48.010
scheduled2 每1秒执行一次2018-06-14T17:33:48.254
scheduled1 每1秒执行一次2018-06-14T17:33:49.005
scheduled3 航次执行完毕后间隔3秒继续执行2018-06-14T17:33:49.247
scheduled1 每1秒执行一次2018-06-14T17:33:50.008
scheduled1 每1秒执行一次2018-06-14T17:33:51.006
scheduled2 每1秒执行一次2018-06-14T17:33:51.258
scheduled1 每1秒执行一次2018-06-14T17:33:52.006
scheduled1 每1秒执行一次2018-06-14T17:33:53.008
scheduled1 每1秒执行一次2018-06-14T17:33:54.007
scheduled3 航次执行完毕后间隔3秒继续执行2018-06-14T17:33:54.252
scheduled2 每1秒执行一次2018-06-14T17:33:54.262
scheduled1 每1秒执行一次2018-06-14T17:33:55.007
scheduled1 每1秒执行一次2018-06-14T17:33:56.007
scheduled1 每1秒执行一次2018-06-14T17:33:57.005
scheduled2 每1秒执行一次2018-06-14T17:33:57.266
scheduled1 每1秒执行一次2018-06-14T17:33:58.007
scheduled1 每1秒执行一次2018-06-14T17:33:59.006
scheduled3 航次执行完毕后间隔3秒继续执行2018-06-14T17:33:59.257
Spring Boot (29) 定时任务的更多相关文章
- Spring Boot配置定时任务
在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...
- 【Spring Boot】定时任务
[Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...
- spring boot 创建定时任务
@Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...
- Spring Boot:定时任务
在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom 包配置 pom 包里面只需要引入 Spring ...
- 【Spring Boot学习之六】Spring Boot整合定时任务&异步调用
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2一.定时任务1.启动类添加注解@EnableScheduling 用于开启定时任务 package com.wjy; i ...
- Spring boot创建定时任务
基于spring boot的应用创建定时任务不要太简单,给一个类加上@Configuration @EnableScheduling注解,然后给该类需要定时执行的方法加上@Scheduled(cron ...
- spring boot 实现定时任务
定时任务或者说定时调度,是系统中比较普遍的一个功能,例如数据归档.清理,数据定时同步(非实时),定时收发等等都需要用到定时任务,常见的定时调度框架有Quartz.TBSchedule等. 如何在Spr ...
- Spring Boot 实现定时任务的 4 种方式
作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...
- Spring Boot Scheduled定时任务特性
SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...
随机推荐
- Ubuntu 16.04升级Linux内核为4.7.0最快的方法
升级内容有很多好处,比如支持最新硬件驱动,使系统更安装等.但是升级内容也会带来一些问题,比如一些软件的兼容性问题,从而出现一些莫名其妙的问题等,所以升级时要慎重考虑. 升级方法: 下载脚本: http ...
- Microsoft Office 2016 for win10 全版本下载+注册激活_Office教程学习网
Microsoft Office 2016 for win10 全版本下载+注册激活_Office教程学习网 http://pan.baidu.com/s/1qWxdvT6
- Kettle循环删除数据
1.问题描述: 某个系统原库的数据同步到备份库.但是由于原库的的数据会物理删除,此时需要删除备份库的数据. 2.不理想的解决1: 1)首先从备份库获取该表的所有ID: 2)循环备份库的ID,去原库检测 ...
- 我的arcgis培训照片9
来自:http://www.cioiot.com/successview-547-1.html
- 基于Wi-Fi的HID注射器,利用WHID攻击实验
WHID代表基于 Wi-Fi 的 HID 注射器,即对 HID 攻击进行无线化攻击的一种注入工具. 实验攻击原理如下图: 攻击者使用ESP8266作为AP,在自己的电脑创建客户端连接AP.在客户端键入 ...
- 手动安装Firefox Linux
(2015-06-05 17:22:19)[编辑][删除] 转载▼ 标签: 股票 Firefox 下载文件以.tar和.bz2格式保存,必须从这些压缩包中提取文件.不想删除当前安装的 Firefox, ...
- LintCode 近期公共祖先
中等 近期公共祖先 查看执行结果 34% 通过 给定一棵二叉树,找到两个节点的近期公共父节点(LCA). 近期公共祖先是两个节点的公共的祖先节点且具有最大深度. 您在真实的面试中是否遇到过这个题? Y ...
- 《转》 Ceilometer项目源代码分析----ceilometer项目源代码结构分析
感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/gaoxingnengjisua ...
- JAVA实现N皇后问题(回溯法)
package com.leetCode; /** * Follow up for N-Queens problem. Now, instead outputting board configurat ...
- 2016/2/21 JavaScript简介
1,javaScript 是什么? 是脚本语言,需要有宿主文件,它的宿主文件是HTML文件.2,它与Java什么关系? 没有什么直接的关系,Java是Sun公司(被Oracle收购了), netspa ...