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

springboot新增调度任务

【README】

本文参考如下文章,使用EnableScheduling注解新增调度任务; (翻译)spring注解接口EnableScheduling说明-CSDN博客文章浏览阅读21次。作为参考,上面例子可以与下面的xml配置做比较。这些示例是等效的,只是在 XML 中使用了固定利率周期而不是自定义触发器实现;这是因为 task: namespace scheduling 无法轻松公开此类支持。这只是一个演示,展示了基于代码的方法如何通过直接访问实际组件来实现最大程度的可配置性。 https://blog.csdn.net/PacosonSWJTU/article/details/147029339


【1】调度装配类

/**
 * @author Tom
 * @version 1.0.0
 * @ClassName BusiSchedulerConfig.java
 * @Description TODO
 * @createTime 2025年04月06日 21:49:00
 */
@Configuration
@EnableScheduling
public class BusiSchedulerConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(this.taskScheduler());
    }

    @Bean(name = "taskScheduler", destroyMethod = "shutdown")
    public ScheduledThreadPoolExecutor taskScheduler() {
        ScheduledThreadPoolExecutor scheduledExecutorService =
                new ScheduledThreadPoolExecutor(1, new ThreadPoolExecutor.DiscardPolicy());
        return scheduledExecutorService;
    }
}

【2】调度任务

@Component
@Slf4j
public class BusiProbeRedisScheduleTask {

    @Autowired
    @Qualifier("taskScheduler")
    private ScheduledThreadPoolExecutor taskScheduler;

    @Scheduled(fixedRate = 5000)
    public void reportCurTime() {
        BlockingQueue<Runnable> queue = taskScheduler.getQueue();
        log.info("reportCurTime: curTime = {}, threadName={}, blockQueueSize={}"
                , BusiDateTimeUtils.getNowText(), Thread.currentThread().getName(), queue.size());
    }
}

【3】运行结果

BusiProbeRedisScheduleTask : reportCurTime: curTime = 2025-04-12 11:52:55, threadName=pool-2-thread-1, blockQueueSize=0
BusiProbeRedisScheduleTask : reportCurTime: curTime = 2025-04-12 11:53:00, threadName=pool-2-thread-1, blockQueueSize=0
BusiProbeRedisScheduleTask : reportCurTime: curTime = 2025-04-12 11:53:05, threadName=pool-2-thread-1, blockQueueSize=0
BusiProbeRedisScheduleTask : reportCurTime: curTime = 2025-04-12 11:53:10, threadName=pool-2-thread-1, blockQueueSize=0
BusiProbeRedisScheduleTask : reportCurTime: curTime = 2025-04-12 11:53:15, threadName=pool-2-thread-1, blockQueueSize=0
BusiProbeRedisScheduleTask : reportCurTime: curTime = 2025-04-12 11:53:20, threadName=pool-2-thread-1, blockQueueSize=0

相关文章:

  • 当当平台商品详情接口设计与调用指南
  • PostgreSQL 的 COPY 命令
  • 算法思想之位运算(一)
  • Model Context Protocol (MCP) 模型上下文协议
  • U盘引导盘制作Rufus v4.7.2231
  • 第十六届蓝桥杯 省赛C/C++ 大学B组
  • 大模型开发:源码分析 Qwen 2.5-VL 视频抽帧模块(附加FFmpeg 性能对比测试)
  • 软考day03
  • THM Billing
  • Win10 开机自动开启手动代理 “手动设置代理”,如何关闭 “使用代理服务器” 如何开机时保持关闭VPN
  • C++初阶-inline的使用
  • Linux xorg-server 解析(一)- 编译安装Debug版本的xorg-server
  • Java基础知识
  • SQL 语句基础(增删改查)
  • 电流互感器的两相星形接线的建模与仿真
  • 撰写学位论文Word图表目录的自动生成
  • 基于单片机的病房呼叫系统设计
  • 【MCAL】AUTOSAR架构下基于SPI通信的驱动模块详解-以TJA1145为例
  • [网鼎杯 2022 青龙组]fakeshell
  • 开启深度学习之旅
  • 同城招聘网站自助建站/自助建站申请
  • 凡客装修/googleseo推广
  • 网站安全设计/重庆优化seo
  • 斐讯k3做网站/软文
  • 哪个网站可以查蛋白互做/腾讯云服务器
  • 做相亲网站犯法吗/网站如何优化一个关键词