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 ...
随机推荐
- POJ 3734_Blocks
题意: 用红绿蓝黄四种颜色对一序列n个方块涂色,求出绿和红色方块数同时为偶数的染色方案数.mod=10007 分析: dp+矩阵快速幂 首先明确有三种状态: 红和绿均为偶数 红和绿只有一个为奇数 红和 ...
- Django学习系列之Form表单结合ajax
Forms结合ajax Forms的验证流程: 定义用户输入规则的类,字段的值必须等于html中name属性的值(pwd= forms.CharField(required=True)=<i ...
- REST API 安全设计
REST API 安全设计 2017年04月27日 18:34:27 阅读数:1699 Rest API 的那些事儿 作者/ asterisk 在软件行业快速发展的今天,传统的软件授权已经不能足以 ...
- vSphere,ESXi,vCenter之间的关系
vSphere是什么? vSphere 是VMware公司公布的一整套产品包,包括类似于VMware ESXi hypervisor.VMware vCenter Server等产品 ESXi是什么? ...
- Django打造大型企业官网(七)
4.13.新闻列表tab栏布局完成 templates/news/index.html <div class="list-outer-group"> <ul cl ...
- 【EasyUI】——可编辑的DataGrid
利用EasyUI做的可编辑的DataGrid大致分为两种类型.一种是启动行编辑的,一种是启动单元格编辑.且不说启动编辑的效果怎样.单启动编辑这一块它就封装的非常厉害.好些功能没有办法去更改.如今项目的 ...
- SSH-struts2的异常处理
在学习j2se的时候学习过利用java的exception类去处理异常.在struts2框架中也提供了对于异常的处理.简单说就是当Action发生异常时.能够在struts2.xml文件里进行配置,将 ...
- Gif验证码类
package com.paic.bics.common.utils.vcode; import java.awt.AlphaComposite; import java.awt.Color; imp ...
- python generator iterator和iterable object
1 iterable object list.dict.set.tuple.file(在每行上iterate)等都是iterable object,但是它们不是iterator.但是它们可以转换成it ...
- Python: PS 滤镜--万花筒效果
本文用 Python 实现 PS 的一种滤镜效果,称为万花筒.也是对图像做各种扭曲变换,最后图像呈现的效果就像从万花筒中看到的一样: 图像的效果可以参考之前的博客: http://blog.csdn. ...