You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Quartz Scheduler集群中统一设置所有节点进入Standby模式?

集群模式Quartz Scheduler批量设置Standby模式的方案

核心思路

Quartz集群基于共享数据库维护全局状态,但scheduler.standby()是实例级本地调用,无法直接跨节点生效。要实现所有节点同步进入Standby模式,可通过共享信号触发或远程统一调用两种思路实现,且均支持从单个节点操作。

方案一:数据库全局开关触发(推荐长期调试/运维场景)

通过在共享数据库中新增全局控制表,让所有节点轮询该表状态,自动触发Standby:

  • 步骤1:创建全局控制表
    CREATE TABLE quartz_global_control (
        control_key VARCHAR(50) PRIMARY KEY,
        control_value VARCHAR(50) NOT NULL,
        update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    );
    -- 初始化Standby触发开关
    INSERT INTO quartz_global_control (control_key, control_value) VALUES ('standby_trigger', 'false');
    
  • 步骤2:每个节点添加轮询逻辑
    编写本地轮询任务(可基于Quartz Job或独立线程),每隔1-5秒查询开关状态:
    // 示例轮询逻辑(伪代码)
    while (true) {
        String triggerValue = jdbcTemplate.queryForObject(
            "SELECT control_value FROM quartz_global_control WHERE control_key = ?",
            String.class, "standby_trigger"
        );
        if ("true".equals(triggerValue) && !scheduler.isInStandbyMode()) {
            scheduler.standby();
            // 重置开关避免重复触发
            jdbcTemplate.update(
                "UPDATE quartz_global_control SET control_value = ? WHERE control_key = ?",
                "false", "standby_trigger"
            );
        }
        Thread.sleep(3000);
    }
    
    注意:该轮询任务需配置为每个节点独立执行,不要设置为集群分布式Job(否则仅单个节点会执行轮询)。
  • 步骤3:单节点触发
    在任意节点执行SQL修改开关,所有节点会在轮询周期内自动进入Standby:
    UPDATE quartz_global_control SET control_value = 'true' WHERE control_key = 'standby_trigger';
    

方案二:远程接口统一调用(适合临时调试场景)

给每个节点暴露触发Standby的接口,在单节点通过脚本批量调用:

  • 步骤1:每个节点添加触发接口
    以Spring Boot项目为例,添加HTTP接口:
    @RestController
    @RequestMapping("/api/scheduler")
    public class SchedulerController {
        @Autowired
        private Scheduler scheduler;
    
        @PostMapping("/standby")
        public ResponseEntity<String> enterStandby() throws SchedulerException {
            if (!scheduler.isInStandbyMode()) {
                scheduler.standby();
                return ResponseEntity.ok("已进入Standby模式");
            }
            return ResponseEntity.ok("已处于Standby模式");
        }
    }
    
  • 步骤2:单节点执行批量调用脚本
    用bash脚本批量请求所有节点接口:
    # 示例bash脚本
    nodes=("node1:8080" "node2:8080" "node3:8080")
    for node in "${nodes[@]}"
    do
        curl -X POST http://$node/api/scheduler/standby
        echo -e "\n处理节点:$node"
    done
    
    执行该脚本的节点即为操作节点,无需登录其他机器。

补充说明

  • 恢复模式:若需恢复调度,对应将数据库开关设为false,或调用每个节点的/api/scheduler/start接口即可。
  • 任务执行说明:Standby模式会暂停调度器的任务触发,但正在执行的任务默认会继续完成;若需中断正在执行的任务,可配置参数org.quartz.scheduler.interruptJobsOnStandby=true。

内容的提问来源于stack exchange,提问作者z0nk0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:40:03