当前位置: 首页 > news >正文

SpringBoot 实现定时任务

在项目我们会有很多需要在某一特定时刻自动触发某一时间的需求,例如我们提交订单但未支付的超过一定时间后需要自动取消订单。

定时任务实现的几种方式:

  • Timer:java自带的java.util.Timer类,使用这种方式允许你调度一个java.util.TimerTask任务。这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少。

  • ScheduledExecutorService:也是jdk自带的类;基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,既任务是并发执行,互不影响。

  • Spring Task:Spring3.0以后自带的task,相当于一个轻量级的Quartz,但其使用起来比Quartz简单很多。

  • Quartz:一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂。

下面我们看一下如何通过Scheduled实现SpringBoot 的定时任务。

1. 启用定时任务

在springboot主类增加注解@EnableScheduling启用定时任务

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication@EnableSchedulingpublic class ScheduledApplication {
    public static void main(String[] args) {        SpringApplication.run(SpringBootScheduledApplication.class, args);    }}

2.创建任务类

@Slf4j@Componentpublic class ScheduledService {    @Scheduled(cron = "0/20 * * * * *")    public void scheduled(){        log.info("1使用cron  {}",System.currentTimeMillis());    }    @Scheduled(fixedRate = 3000)    public void scheduled1() {        log.info("2使用fixedRate{}", System.currentTimeMillis());    }    @Scheduled(fixedDelay = 3000)    public void scheduled2() {        log.info("3fixedDelay{}",System.currentTimeMillis());    }}

默认为单线程,可以看到三个定时任务都已经执行,并且使同一个线程中串行执行,如果只有一个定时任务,这样做肯定没问题,当定时任务增多,如果一个任务卡死,会导致其他任务也无法执行。

3.实现多线程任务

3.1 添加配置类并启用异步事件

@Configuration@EnableAsyncpublic class ScheduledAsyncConfig {
private int corePoolSize = 20;private int maxPoolSize = 500;private int queueCapacity = 20;
@Beanpublic Executor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(corePoolSize);executor.setMaxPoolSize(maxPoolSize);executor.setQueueCapacity(queueCapacity);executor.initialize();return executor;}}

3.2,修改2.中的定时任务的类或者方法上添加@Async​​​​​​​

@Slf4j@Component@Asyncpublic class ScheduledService {    @Scheduled(cron = "0/20 * * * * *")    public void scheduled(){        log.info("1使用cron  {}",System.currentTimeMillis());    }    @Scheduled(fixedRate = 3000)    public void scheduled1() {        log.info("2使用fixedRate{}", System.currentTimeMillis());    }    @Scheduled(fixedDelay = 3000)    public void scheduled2() {        log.info("3fixedDelay{}",System.currentTimeMillis());    }}​​​​​​​
http://www.lryc.cn/news/290478.html

相关文章:

  • 将Vue2中的console.log调试信息移除
  • EMC设计检查建议,让PCB layout达到最佳性能
  • 常用抓包软件集合(Fiddler、Charles)
  • C++入门(一)— 使用VScode开发简介
  • PeakCAN连接到WSL2 Debian
  • Spring Boot导出EXCEL 文件
  • 编程笔记 html5cssjs 060 css响应式布局
  • 建筑行业如何应用3D开发工具HOOPS提升实时设计体验?
  • 【grafana】使用教程
  • seata 分布式
  • 前端面试题-说说你了解的js数据结构?(2024.1.29)
  • 音视频数字化(数字与模拟-录音机)
  • 鸿蒙开发-UI-组件3
  • 安全测试几种:代码静态扫描、模糊测试、黑盒测试、白盒测试、渗透测试
  • Mac安装及配置MySql及图形化工具MySQLworkbench安装
  • 【Vue】为什么Vue3使用Proxy代替defineProperty?
  • 3、css设置样式总结、节点、节点之间关系、创建元素的方式、BOM
  • 计算机网络-物理层传输介质(导向传输介质-双绞线 同轴电缆 光纤和非导向性传输介质-无线波 微波 红外线 激光)
  • springboot3+vue3支付宝在线支付案例-渲染产品列表页面
  • 数字美妆技术:美颜SDK和动态贴纸技术的崭新时代
  • 使用OpenCV实现一个简单的实时人脸跟踪
  • 关于监控的那些事,你有必要了解一下
  • C#学习笔记_数组
  • 微信小程序canvas画布实现文字自由缩放、移动功能
  • jQuery 获取并设置 CSS 类 —— W3school 详解 简单易懂(十五)
  • dart使用教程
  • CSS3:最新特性和实例教程
  • leetcode—跳跃游戏—贪心算法
  • Databend 开源周报第 130 期
  • 【web安全】文件上传漏洞