springboot之定时任务
定时线程
说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务。
回顾一下定时线程池。
public static ScheduledExecutorService newScheduledThreadPool(int var0) {return new ScheduledThreadPoolExecutor(var0);}public static ScheduledExecutorService newScheduledThreadPool(int var0, ThreadFactory var1) {return new ScheduledThreadPoolExecutor(var0, var1);}
常用的两个方法:
scheduleAtFixedRate:是以固定的频率去执行任务,周期是指每次执行任务成功执行之间的间隔。
schedultWithFixedDelay:是以固定的延时去执行任务,延时是指上一次执行成功之后和下一次开始执行的之前的时间。
看一个DEMO:
public class ScheduledExecutorServiceDemo {public static void main(String args[]) {ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);ses.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {try {Thread.sleep(4000);System.out.println(Thread.currentThread().getId() + "执行了");} catch (InterruptedException e) {e.printStackTrace();}}}, 0, 2, TimeUnit.SECONDS);}
}
具体细节我就不再赘述了,有兴趣的可以查看我关于线程池的博客:链接
springboot的定时任务
pom的依赖:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
启动类启用定时
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling
@SpringBootApplication
public class StartApplication {public static void main(String args[]){SpringApplication application = new SpringApplication(StartApplication.class);application.run(args);}
}
定时任务业务类:
package com.schedule;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;@Component
public class ScheduleTask {private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");private AtomicInteger count = new AtomicInteger();@Scheduled(fixedRate = 6000)public void reportTime(){System.out.println("现在的时间是:"+format.format(new Date()));}/*** 以固定的频率去执行任务*/@Scheduled(initialDelay = 10000,fixedRate = 3000)public void reportNumber(){System.out.println(count.incrementAndGet());}/*** 以固定的延时去执行任务*/@Scheduled(initialDelay = 10000,fixedDelay = 3000)public void reportNumberDelay(){System.out.println(count.incrementAndGet());}
}
运行结果如下:
现在的时间是:09:59:57
1
2
现在的时间是:10:00:03
3
4
5
6
现在的时间是:10:00:09
7
使用说明:
- @Scheduled(fixedRate = 1000) :上一次开始执行时间点之后1秒再执行
- @Scheduled(fixedDelay = 1000) :上一次执行完毕时间点之后1秒再执行
- @Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
@Scheduled(initialDelay=1000, fixedDelay=6000) :第一次延迟1秒后执行,之后按fixedDelay的规则每6秒执行一次
源码地址
个人博客网站 http://www.janti.cn
springboot之定时任务相关推荐
- SpringBoot 实战定时任务 Scheduled
序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- springboot配置定时任务及常用的cron表达式
springboot引入定时任务 springboot引入定时任务主要需要以下几步: 1.引入相关的依赖 2.配置程序开启定时任务 3. 编写定时任务 引入相关的依赖 只用引一个基础的web的依赖就可 ...
- SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)
SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 目录 SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 1.概述 2.最佳实践 2.1创建项目引入依赖(mail) ...
- SpringBoot实现定时任务的三种方式,总有一款适合你!
点击关注公众号,利用碎片时间学习 序言 SpringBoot创建定时任务,目前主要有以下三种实现方式: 基于注解(@Scheduled): 基于注解@Scheduled默认为单线程,开启多个任务时,任 ...
- SpringBoot整合定时任务和Emil发送
SpringBoot整合定时任务和Emil发送 定时任务 任务系统指的是定时任务.定时任务是企业级开发中必不可少的组成部分,诸如长周期业务数据的计算,例如年度报表,诸如系统脏数据的处理,再比如系统 ...
- linux定时任务重复率,基于SpringBoot实现定时任务的设置(常用:定时清理数据库)...
1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...
- SpringBoot | :定时任务的使用
前言 上一章我们简单的讲解了关于异步请求相关知识点.这一章节,我们来讲讲开发过程也是经常会碰见的定时任务.比如每天定时清理无效数据.定时发送短信.定时发送邮件.支付系统中的定时对账等等,往往都会定义一 ...
- SpringBoot中定时任务与异步定时任务的实现
场景 若依前后端分离版手把手教你本地搭建环境并运行项目: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662 在上面 ...
- 复盘SpringBoot中定时任务和异步线程池
作者:溪~源 blog.csdn.net/xuan_lu/article/details/110568508 项目中最近使用了多个定时任务处理业务需求,于是在实现业务逻辑过程中,产生了上图一些思考和疑 ...
最新文章
- 反编译使用yield关键字的方法
- Spring Boot 面试杀手锏:自动配置原理
- 类会默认产生的成员函数
- 项目管理中的组织计划
- 【python】编程学习练习题-2
- Javascript实现AES加密解密(ECB/CBC)
- 围棋人机大战属于计算机在什么方面的应用,《信息技术基础》第一章复习题库...
- python虚拟环境搭建mac_在MAC上安装Python虚拟环境
- 数学基础加强3---矩阵和线性代数
- 课题申报书范文_教师课题申报书范例
- python练习-华氏转摄氏
- 基于Tofino2的64X100GE高性能可编程交换机MX7636-64X
- (幼儿园毕业)Javascript小学级随机生成四则运算
- 思科三层交换机不同vlan互通_cisco(三层交换和动态路由,不同vlan间的通信,静态路由实现全网互通)...
- 矩阵分析之Householder Reduction
- can和could的用法_情态动词can和could的用法及例句
- android毗邻(Pilin)即时聊天应用源码
- BUUCTF not_the_same_3dsctf_2016
- Andersen Global与Baptiste Co. Law Firm合作加强加勒比海平台
- 《数据密集型应用系统设计》读书笔记——第二部分 分布式数据系统(一)
热门文章
- 深度神经网络中的Batch Normalization介绍及实现
- Ubuntu下安装Cppcheck源码操作步骤
- 人脸识别引擎SeetaFaceEngine中Detection模块使用的测试代码
- Git简介以及与SVN的区别
- 【Qt】使用QPalette设置按钮颜色时,不生效
- 2021北师大丰台实验高考成绩查询,2020北京丰台区中考各高中录取分数线公布
- ps背景不变换字_分享五个超级实用的PS小技巧
- lede 插件_家中路由换新——lede软路由安装教程
- gpg加密命令 linux_用 PGP 保护代码完整性(五):将子密钥移到一个硬件设备中 | Linux 中国...
- 【工具软件】markdown编译器待办事项无法打勾