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 ...
随机推荐
- [bzo1211][HNOI2004]树的计数_prufer序列
树的计数 bzoj-1211 HNOI-2004 题目大意:题目链接. 注释:略. 想法: prufer序列有一个性质就是一个数在prufer序列中出现的次数等于这个prufer序列生成的树中它的度数 ...
- spring boot file上传
用Spring Boot写读取Excel文件小工具的时候遇到的一些小坑已经填平,复制即可满足普通的文件上传功能POI方面只需一个包,其他通用包工程中一般都会带TIPS:前端为了扩展我用ajax异步请求 ...
- guava cache学习
Guava Cache与ConcurrentMap很相似,但也不完全一样.最基本的区别是ConcurrentMap会一直保存所有添加的元素,直到显式地移除.相对地,Guava Cache为了限制内存占 ...
- SAS编程基础 - 逻辑库和数据集
1. SAS逻辑库 1.1 创建SAS逻辑库: libname lb 'F:\Data_Model'; libname是关键字,lb是创建的逻辑库的名称,引号内的内容是目录路径,最后一个分号结束程序语 ...
- [TypeScript] Define Custom Type Guard Functions in TypeScript
One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a ...
- (六)Net Core项目使用Controller之一 c# log4net 不输出日志 .NET Standard库引用导致的FileNotFoundException探究 获取json串里的某个属性值 common.js 如何调用common.js js 筛选数据 Join 具体用法
(六)Net Core项目使用Controller之一 一.简介 1.当前最流行的开发模式是前后端分离,Controller作为后端的核心输出,是开发人员使用最多的技术点. 2.个人所在的团队已经选择 ...
- java 学习第一步---安装JDK以及配置环境变量
1.下载jdk 链接:https://pan.baidu.com/s/1FiTGhxdHK0KTFawdkLT26g 提取码:zcy0 我已经在官网上面下载了1.8的jdk,通过百度云盘分 ...
- ZOJ 3876 May Day Holiday 蔡勒公式
H - May Day Holiday Description As a university advoc ...
- 图片存储系统TFS
1 TFS和GFS比较 1.1 GFS的应用场景 第一,百万级别的文件,并且是大文件,文件都是100MB以上,1G级别的文件很常见. 第二,集群是建立在商业计算机之上,并不可靠,监控各个节点的状态,当 ...
- arm-linux交叉编译工具链的制作(基于S3C2440)【转】
本文转载自:http://eric-gao.iteye.com/blog/2160622 制作arm-linux交叉编译工具链一般通过crosstool工具或者crosstool-NG,前者使用方便, ...