使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取

定时任务概述

  定时任务:顾名思义就是在特定/指

定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务。

实现方式:

  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) 定时任务的更多相关文章

  1. Spring Boot配置定时任务

    在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...

  2. 【Spring Boot】定时任务

    [Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...

  3. spring boot 创建定时任务

    @Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...

  4. Spring Boot:定时任务

    在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom 包配置 pom 包里面只需要引入 Spring ...

  5. 【Spring Boot学习之六】Spring Boot整合定时任务&异步调用

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2一.定时任务1.启动类添加注解@EnableScheduling 用于开启定时任务 package com.wjy; i ...

  6. Spring boot创建定时任务

    基于spring boot的应用创建定时任务不要太简单,给一个类加上@Configuration @EnableScheduling注解,然后给该类需要定时执行的方法加上@Scheduled(cron ...

  7. spring boot 实现定时任务

    定时任务或者说定时调度,是系统中比较普遍的一个功能,例如数据归档.清理,数据定时同步(非实时),定时收发等等都需要用到定时任务,常见的定时调度框架有Quartz.TBSchedule等. 如何在Spr ...

  8. Spring Boot 实现定时任务的 4 种方式

    作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...

  9. Spring Boot Scheduled定时任务特性

    SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...

随机推荐

  1. SPOJ SUMPRO(数学)

    题意: 给出一个数N,问所有满足n/x=y(此处为整除)的所有x*y的总和是多少.对答案mod(1e9+7). 1 <= T <= 500. 1 <= N <= 1e9. 分析 ...

  2. easyui datagrid-detailview 嵌套高度自适应

    实现效果 原因 异步加载,明细展开时,可能会遇到父列表不能自动适应子列表高度的变化 具体代码 $('#centerdatagrid').datagrid({ url:'${ctx}/offer/off ...

  3. MongoDB小结14 - find【查询条件$lt $lte $gt $gte】

    $lt $lte $gt $gte 以上四个分别表示为:< . <= . > . >= . 通常的做法是将他们组合起来,以便查找一个范围. 比如,查询年龄在18到25岁(含)的 ...

  4. zabbix学习系列之基础概念

    触发器 概念 "监控项"仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送警告信息,"触发器"正式英语为监控项所收集的数据 ...

  5. 带头尾和动画的下拉刷新RecyclerView

    项目地址:https://github.com/shichaohui/AnimRefreshRecyclerView 项目中包括一个demo(普通Androidproject)和Android Lib ...

  6. 小贝_mysql 存储过程

    存储过程 简要: 1.什么是存储过程 2.使用存储过程 一.存储过程 概念类似于函数,就是把一段代码封装起来.当要行这段代码的时候,能够通过调用该存储过程来实现.在封装的语句体里面.能够用if/els ...

  7. pojWindow Pains(拓扑排序)

    题目链接: 啊哈哈,点我点我 题意: 一快屏幕分非常多区域,区域之间能够相互覆盖,要覆盖就把属于自己的地方所有覆盖. 给出这块屏幕终于的位置.看这块屏幕是对的还是错的.. 思路: 拓扑排序,这个简化点 ...

  8. XML(一)语法

    一.xml语法 1.文档声明 2.元素 3.属性 4.凝视 5.CDATA区.转义字符 6.处理指令 1.文档声明: 用来声明xml的基本属性,用来指挥解析引擎怎样去解析当前xml 通常一个xml都要 ...

  9. 洛谷 P1383 高级打字机==codevs 3333 高级打字机

    P1383 高级打字机 18通过 118提交 题目提供者yeszy 标签倍增图论高级数据结构福建省历届夏令营 难度省选/NOI- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 早苗入手 ...

  10. 安装 matplotlib

    比较推荐还是用pip来安装,用源码安装还是会比较麻烦,进入到CMD窗口下,执行python -m pip install -U pip setuptools进行升级. 接着键入python -m pi ...