Springboot周期性任务调度

一、简介

​ 在开发中,我们经常会使用到定时任务实现某些功能,如心跳、数据同步等功能。在实现这些功能的时候首先想到的就是cron表达式,通过cron表达式来指定任务在某个时间点或者周期性的执行。无论是Linux系统,还是Spring的@Scheduled还是用Quartz框架,都支持cron表达式。

二、遇到的问题

​ 在使用Springboot进行开发时,我们明明写好了cron表达式去周期性的执行某段代码逻辑,但根据日志来看,实际的执行跟表达式执行的周期并不一样,而且随着程序的执行,这个间隔会越来越大

//10秒执行一次
@Scheduled(cron = "0/10 * * * * ?")
public void heartbeat() {logger.info("[向汇聚平台注册]{}", new Date());//使用异常捕获,防止代码报错导致该线程卡死try{//具体代码逻辑//post请求有参并设置超时时间 单位为毫秒(超时时间小于周期性间隔时间),不设置超时。在网络不好的情况下容易导致线程阻塞HttpUtil.post(url, params,5000);}catch (Exception e){e.printStackTrace();}
}
com.genersoft.iot.vmp.info.schedule.Heartbeat:50 - [向汇聚平台注册]Tue Nov 15 13:55:36 CST com.genersoft.iot.vmp.info.schedule.Heartbeat:50 - [向汇聚平台注册]Tue Nov 15 13:55:57 CST  com.genersoft.iot.vmp.info.schedule.Heartbeat:50 - [向汇聚平台注册]Tue Nov 15 13:56:39 CST


为了找到原因, 我们从 @Scheduled 注解的源码开始找,在如下的源码中会发现, 如果我们不主动配置我们需要的 TaskScheduler, SpringBoot 会默认使用一个单线程的scheduler来处理我们用 @Scheduled 注解实现的定时任务,通过前面的[scheduling-1]也可以看出确实是单线程。

/*** Set the {@link org.springframework.scheduling.TaskScheduler} that will invoke* the scheduled methods, or a {@link java.util.concurrent.ScheduledExecutorService}* to be wrapped as a TaskScheduler.* <p>If not specified, default scheduler resolution will apply: searching for a* unique {@link TaskScheduler} bean in the context, or for a {@link TaskScheduler}* bean named "taskScheduler" otherwise; the same lookup will also be performed for* a {@link ScheduledExecutorService} bean. If neither of the two is resolvable,* a local single-threaded default scheduler will be created within the registrar.* @see #DEFAULT_TASK_SCHEDULER_BEAN_NAME*/public void setScheduler(Object scheduler) {this.scheduler = scheduler;}

三、问题解决

定时任务多线程配置类

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;import java.util.concurrent.*;/*** @author yyb* @date 2022年11月15日 11:27*/
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {//设置线程数为5scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));}}


通过日志可以看出执行周期跟设置周期一样,且有多个线程执行。

Springboot周期性任务调度相关推荐

  1. 不羡鸳鸯不羡仙,一行代码调半天。SpringBoot集成任务调度,实现每天定时发送天气预报,随时做好“广冻人”的心理准备

    不羡鸳鸯不羡仙,一行代码调半天.SpringBoot集成任务调度,实现每天定时发送天气预报,随时做好"广冻人"的心理准备 下载地址: https://download.csdn.n ...

  2. 优雅的使用springboot集成任务调度

    一 任务调度基本介绍 任务调度器就是按照规定的计划完成任务:比如windows,linux的自带的任务调度系统功能:平常开发中也就是按照规定的时间点轮询执行计划任务(比如每周三的凌晨进行数据备份),或 ...

  3. DataGrip 初级与高级教程(仅提供链接)

    仅提供链接 初级使用教程 DataGrip使用入门(一) -- 安装与数据源管理 DataGrip使用入门(二) -- 常用设置 DataGrip使用入门(三) -- 常用操作 DataGrip使用入 ...

  4. springboot使用教程

    1.springboot helloworld 2.Springboot返回json数据 3. Springboot使用其他json转换框架 4. Springboot全局异常捕捉 5. Spring ...

  5. JVM致命错误日志(hs_err_pid14268.log)分析

    今天从原工程中剥离出来的PDF处理服务无响应-检查发现进程已经挂掉,多出一条JVM致命错误日志,今天就来好好分析一下这个日志. 首先第一句There is insufficient memory fo ...

  6. 四种Java线程池用法解析

    四种Java线程池用法解析 本文为大家分析四种Java线程池用法,供大家参考,具体内容如下 http://www.jb51.net/article/81843.htm 1.new Thread的弊端 ...

  7. 深入浅出 Java Concurrency (29): 线程池 part 2 Executor 以及Executors[转]

    Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...

  8. 想进大厂?50个多线程面试题,你会多少?(一)

    最近看到网上流传着,各种面试经验及面试题,往往都是一大堆技术题目贴上去,而没有答案. 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让 ...

  9. ScheduledThreadPoolExecutor()定时执行线程池详解,java线程池

    为什么80%的码农都做不了架构师?>>>    package com.dy.pool;import java.util.concurrent.ExecutorService; im ...

最新文章

  1. SQL Server 2008中原生的分层数据类型:hierarchyid
  2. 41.虚拟存储器以及相关算法
  3. rhel4 x86_64 php5.2.17 make安装 支持mysqli
  4. 浏览器事件捕获冒泡以及阻止冒泡
  5. 只包含因子 2 3 5 的数(51Nod-1010)
  6. mysql的Innodb为什么使用B+树
  7. C++ Primer Plus第二章课后编程答案
  8. OpenCV图像拼接之Stitching和Stitching_detailed
  9. 大数据分析平台建模及建议
  10. html中播放的语法是什么,css语法是什么?
  11. AS5040磁编码器使用笔记
  12. 关于android RTP驱动的问题
  13. django慢学日常
  14. opencv3.2教程linux,Linux编译OpenCV3.2.0-OCL模块并使用
  15. matlab中三视图如何画,[matlab 三维图]怎样把三维图导入到MATLAB
  16. tensorflow框架下,多进程model.predict(x)无响应/暂停/无输出
  17. FastDFS学习笔记 -- day04 与Nginx整合
  18. 重磅!《新一代人工智能伦理规范》发布丨附全文
  19. 高中计算机辗转相除法,高中数学的辗转相除法问题
  20. 英雄联盟s10信息详解

热门文章

  1. A1 为什么要面向服务的架构SOA?什么是SOA?
  2. 动态磁盘和基本磁盘及转换
  3. 记一次华为RH2288-3V _X86架构服务器的操作系统安装
  4. Java深度学习系列——对象流和序列化
  5. web应用的log4j配置
  6. 博图V14出现The Automation License Manager Service has not been started!Please start the Service.错误
  7. Linux 虚拟网络设备详解之 Bridge 网桥
  8. matlab中wav转txt6,WAV转TXT专家下载
  9. MyEclipse has detected that less than 5% of the 31MB of Eden Space (Heap memory)
  10. 图片和图形之扩展色彩内容增强图形(20)