@Scheduled 定时任务自定义
简介
@Scheduled 定时任务自定义可以通过SchedulingConfigurer实现。
SchedulingConfigurer 是 Spring Framework 中的一个接口,用于配置定时任务。当你需要对定时任务进行更高级别的定制时,这个接口就显得非常有用。
- 可以通过
SchedulingConfigurer接口来自定义一些高级配置 - 可以通过
SchedulingConfigurer自定义执行定时任务的线程池。 - 可以通过
SchedulingConfigurer自定义任务调度器,比如使用 Quartz 或其他调度框架。
实现
创建一个类实现SchedulingConfigurer
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10);
scheduler.setThreadNamePrefix("schedule-executor-");
scheduler.initialize();
taskRegistrar.setScheduler(scheduler);
}
}
可以通过 ThreadPoolTaskScheduler 或者 ScheduledExecutorService 来配置线程池
使用
- 1.启动类添加
@EnableScheduling注解 - 2.使用
@Scheduled注解
@Scheduled(cron = "0/30 * * * * ?")
public void test(){
log.info("Scheduled Test -- The time is now " + LocalDateTime.now());
}
效果
2024-08-15 20:07:00,014 INFO [schedule-executor-2] com.zk.app.manager.PartitionManager - Scheduled Test -- The time is now 2024-08-15T20:07:00.014
2024-08-15 20:07:30,004 INFO [schedule-executor-3] com.zk.app.manager.PartitionManager - Scheduled Test -- The time is now 2024-08-15T20:07:30.004
2024-08-15 20:08:00,010 INFO [schedule-executor-2] com.zk.app.manager.PartitionManager - Scheduled Test -- The time is now 2024-08-15T20:08:00.010
2024-08-15 20:08:30,014 INFO [schedule-executor-1] com.zk.app.manager.PartitionManager - Scheduled Test -- The time is now 2024-08-15T20:08:30.013
2024-08-15 20:09:00,016 INFO [schedule-executor-5] com.zk.app.manager.PartitionManager - Scheduled Test -- The time is now 2024-08-15T20:09:00.016
总结
SchedulingConfigurer提供了一个灵活的方式来定制定时任务的执行方式。- 可以配置线程池的大小、执行策略等。
- 通过实现该接口并提供给 Spring,你可以完全控制定时任务的执行环境。
- 能够根据自己的需求来配置定时任务,确保它们按照预期的方式运行。
结束
@Scheduled 定时任务自定义的更多相关文章
- SpringBoot学习18:springboot使用Scheduled 定时任务器
Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...
- scheduled定时任务+实例请求数据库
1.scheduled定时任务类:ScheduledDemo.java package com.nantian.scheduled; import java.util.Date; import org ...
- SpringBoot: 18.使用Scheduled 定时任务器(转)
Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...
- Scheduled定时任务器在Springboot中的使用
Scheduled定时任务器是Spring3.0以后自带的一个定时任务器. 使用方式: 1.添加依赖 <!-- 添加 Scheduled 坐标 --> <dependency> ...
- Spring Boot Scheduled定时任务特性
SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...
- 你不知道的Scheduled定时任务骚操作
目录 一.什么是定时任务 二.项目依赖 三.注解式定时任务 3.1 cron 3.2 fixedDelay 3.3 fixedDelayString 3.4 fixedRate 3.5 fixedRa ...
- spring boot注解之@Scheduled定时任务实现
java实现定时任务一般使用timer,或者使用quartz组件.现在在spring boot提供了更加方便的实现方式. spring boot已经集成了定时任务.使用@Secheduled注解. @ ...
- Spring注解@Scheduled定时任务
一.首先配置applicationContext-task.xml (1)添加 xmlns:task="http://www.springframework.org/schema/task& ...
- spring的@Scheduled定时任务,同一时间段的定时任务只会执行一个,其余的会被阻塞,@Scheduled注解定时任务并发执行解决办法,即多线程运行定时任务
原文:https://blog.csdn.net/qq_35937303/article/details/88851064 现有两个定时任务 @Component("aa") pu ...
- Java SpringBoot Scheduled定时任务
package task.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import ...
随机推荐
- Docker的安装和配置
1.在CentOS上安装Docker 在CentOS上安装Docker的过程相对简单,可以通过官方提供的安装包安装.以下是详细的安装步骤. 1.安装依赖包 在安装Docker之前,确保安装了一些必要的 ...
- 218:解释LINUX文件系统权限
- manim边学边做--时针方向变换
今天介绍的两个动画类ClockwiseTransform和CounterclockwiseTransform, 用于将某一个元素按照时针方向变换为另一个对象. ClockwiseTransform:将 ...
- 同步工具-Oceanus打通mysql到Iceberg
一.服务配置 已配置好gt_oneline_2,其它集群还需按照下面方式特殊配置 1.需要腾讯Oceanus同学在后端修改高途flink集群配置core-site.xml文件,增加如下配置.只能绑定一 ...
- Yarn角色
一.ResourceManager ResourceManager中的调度器负责资源的分配 二.NodeManager NodeManager则负责资源的供给和隔离 ResourceManager将某 ...
- 为什么TCP需要三次握手?深入解析背后的设计哲学
在互联网通信中,TCP(传输控制协议)是确保数据可靠传输的基石.而TCP连接的建立过程--"三次握手"(Three-Way Handshake),看似简单的三个步骤,却蕴含了网络协 ...
- 赶上AI的大潮:在VSCode中使用DeepSeek编程的极简方法
1 赶上AI的大潮:在VSCode中使用DeepSeek编程的极简方法 1.1 背景 DeepSeek在春节期间突然大行其道,欣喜国力大增的同时,对于普通IT工作者,如何才能享受这一波AI红利,让 ...
- Typecho Mirages 主题自定义公告样式
使用步骤 将以下代码加入到 <head> 标签中.对于本主题,依次进入 控制台 - 外观 - 设置外观 - 主题自定义扩展,将代码加入到 自定义 HTML 元素拓展 - 标签: head ...
- PowerShell开发游戏 · 打蜜蜂
可以看到,虽然非常抽象简单,但是基础游戏框架已经搭建,游戏机制完善,就缺美工了,哈哈~~~~ [首先] Powershell不是用来开发游戏的,但是没人规定不能开发.因为它可以调取windo ...
- C#(面向对象的托管语言)类库(区别于应用程序)的异常处理思路
1.不要做出任何应用程序才需要考虑抉择策略,不能想当然的决定一些错误情形.具体的一个体现形式是什么异常都捕获.这不是类库的职责,因为无法掌握所有的调用者的使用情形,这些不确定性是委托.虚方法.接口等特 ...