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 ...
随机推荐
- Codeforces 761E(DFS)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- WinExec可能会引起消息重入
WinExec不仅会造成延迟,并且还会引起消息的重入. 以下是调用堆栈: WinvoiceCC.exe!CWinvoiceCCDlg::OnMsgHttpReq(unsigned int wParam ...
- 编程算法 - n个骰子的点数(递归) 代码(C)
n个骰子的点数(递归) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 把n个骰子仍在地上, 全部骰子朝上一面的点数之和为s. 输入n, 打印出 ...
- chosen.jquery.js 搜索框只能从头匹配的解决思路+方法
chosen.jquery.js 搜索框只能从头匹配的解决思路+方法 心急者请直接看下方 总结 ,由于本问题未能找到直接答案,所以只能通过修改源码解决.故将修改源码思路贴出来供大家参考,在遇到其他改源 ...
- 在对象内部尽量直接訪问实例变量 --Effictive Objective-C 抄书
在对象之外訪问实例变量时,应该总是通过属性来做.在那么在对象内部訪问实例变量的时候,又该怎样呢? 这是 OCer们一直激烈讨论的问题.有人觉得,不管什么情况,都应该通过属性来訪问实例变量;也有人说,& ...
- Ubuntu安装JDK及环境变量配置(sun java)
捣鼓了尽一天的时间,终于把sun的java安装上了,不是openjava了,网上试了好多的方法好多都是不可以的,所以当自己成功后就立马把方法贴出来,以方便后来者少走弯路,此文的方法绝对可行! 这里先简 ...
- 剑指Offer面试题29(java版):数组中出现次数超过一半的数字
题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 比如输入一个长度为9的数组{1,2,3,2.2,2.5,4,2}.因为数字2在数组中出现5次,超过数组长度的一半,因此输出2. 解 ...
- java7新特性之Diamond syntax
java7新特性之Diamond syntax Java 7 also introduces a change that means less typing for you when dealing ...
- 【bzoj1149】 [CTSC2007]风玲Mobiles
题目意为:给一颗二叉树,每一次操作可以交换该子树的左右两颗子树,要将该树变为完全二叉树,求最小操作次数.从根开始进行一遍DFS.记录每棵子树的大小size,如果左子树的size小于右子树的size那么 ...
- YTU 2634: E3 继承了,成员函数却不可访问
2634: E3 继承了,成员函数却不可访问 时间限制: 1 Sec 内存限制: 128 MB 提交: 521 解决: 435 题目描述 下面的程序中,派生类Derive继承自基类Base,mai ...