74dfb82a264f1b779e50058145953caca907f6f2..5dbfaac33b7a1868e5753600aa5886ca13543cf3
3 天以前 maven
yys 定时任务持久化
5dbfaa 对比 | 目录
3 天以前 maven
yys 定时任务持久化
850487 对比 | 目录
3 天以前 maven
yys 数据持久化
a78fb9 对比 | 目录
已修改6个文件
63 ■■■■ 文件已修改
src/main/java/com/ruoyi/inspectiontask/service/impl/QuartzConfig.java 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskJob.java 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/project/monitor/service/impl/SysJobServiceImpl.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-dev.yml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-hckx.yml 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/inspectiontask/service/impl/QuartzConfig.java
@@ -1,5 +1,6 @@
package com.ruoyi.inspectiontask.service.impl;
import org.quartz.*;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
@@ -7,13 +8,21 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import javax.sql.DataSource;
@Configuration
public class QuartzConfig {
    @Autowired
    private ApplicationContext applicationContext;
    // 假设已配置名为dataSource的数据源Bean
    @Autowired
    private DataSource dataSource;
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
@@ -24,10 +33,17 @@
        jobFactory.setApplicationContext(applicationContext);
        schedulerFactory.setJobFactory(jobFactory);
        // 在schedulerFactoryBean()方法中添加
        schedulerFactory.setDataSource(dataSource);
        // 其他配置...
        return schedulerFactory;
    }
    @Bean
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }
    // 自定义JobFactory,支持自动注入
    public static class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory
            implements ApplicationContextAware {
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskJob.java
@@ -13,6 +13,7 @@
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.Serializable;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
@@ -23,7 +24,8 @@
@Component
@DisallowConcurrentExecution // 禁止并发执行同一个Job
public class TimingTaskJob implements Job {
public class TimingTaskJob implements Job, Serializable {
    private static final long serialVersionUID = 1L; // 必须定义序列化ID
    @Autowired
    private TimingTaskMapper timingTaskMapper;
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java
@@ -31,7 +31,7 @@
     * 更新已有任务
     */
    public void rescheduleTimingTask(TimingTask task) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey("trigger_" + task.getId(), "TIMING_TASK_TRIGGER_GROUP");
        TriggerKey triggerKey = new TriggerKey("trigger_" + task.getId());
        // 获取现有触发器并转换为 CronTrigger
        Trigger oldTrigger = scheduler.getTrigger(triggerKey);
@@ -91,7 +91,7 @@
    private JobDetail buildJobDetail(TimingTask task) {
        // 1. 构建唯一JobKey(基于任务ID,确保重启后能识别)
        JobKey jobKey = new JobKey("timingTask_" + task.getId(), "TIMING_TASK_GROUP");
        JobKey jobKey = new JobKey("timingTask_" + task.getId());
        // 2. 封装任务数据(仅使用基本类型,确保可序列化)
        JobDataMap jobDataMap = new JobDataMap();
@@ -105,20 +105,20 @@
                .withIdentity(jobKey)                    // 唯一标识,用于持久化存储
                .withDescription(task.getTaskName())     // 任务描述,存入数据库
                .usingJobData(jobDataMap)                // 绑定任务数据
                .storeDurably()                          // 即使没有触发器关联也持久化保存
                .storeDurably(true)                          // 即使没有触发器关联也持久化保存
                .requestRecovery(true)                   // 当调度器崩溃后恢复时,重新执行未完成的任务
                .build();
    }
    private Trigger buildJobTrigger(TimingTask task, JobDetail jobDetail) {
        // 1. 构建唯一TriggerKey(基于任务ID)
        TriggerKey triggerKey = new TriggerKey("trigger_" + task.getId(), "TIMING_TASK_TRIGGER_GROUP");
        TriggerKey triggerKey = new TriggerKey("trigger_" + task.getId());
        // 2. 生成Cron表达式(原逻辑不变)
        String cronExpression = convertToCronExpression(task);
        // 3. 构建CronTrigger,确保持久化配置
        CronTrigger trigger = TriggerBuilder.newTrigger()
        return TriggerBuilder.newTrigger()
                .withIdentity(triggerKey)                // 唯一标识,用于持久化存储
                .withDescription(task.getTaskName() + "_TRIGGER") // 触发器描述
                .forJob(jobDetail)                       // 关联对应的Job
@@ -131,7 +131,6 @@
                        ? Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant())
                        : new Date())
                .build();
        return trigger;
    }
    private String convertToCronExpression(TimingTask task) {
        // 参数校验
src/main/java/com/ruoyi/project/monitor/service/impl/SysJobServiceImpl.java
@@ -37,7 +37,7 @@
    @PostConstruct
    public void init() throws SchedulerException, TaskException
    {
        scheduler.clear();
//        scheduler.clear();
        List<SysJob> jobList = jobMapper.selectJobAll();
        for (SysJob job : jobList)
        {
src/main/resources/application-dev.yml
@@ -35,6 +35,7 @@
# 日志配置
logging:
  level:
    org.quartz: DEBUG
    com.ruoyi: warn
    org.springframework: warn
@@ -160,7 +161,7 @@
  quartz:
    job-store-type: jdbc  # 使用数据库存储
    jdbc:
      initialize-schema: always  # 首次运行时自动创建表结构,成功后改为never
      initialize-schema: never  # 首次运行时自动创建表结构,成功后改为never
      schema: classpath:org/quartz/impl/jdbcjobstore/tables_mysql_innodb.sql  # MySQL表结构脚本
    properties:
      org:
@@ -171,7 +172,7 @@
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate  # MySQL适配
            tablePrefix: QRTZ_  # 表名前缀,与脚本一致
            tablePrefix: qrtz_  # 表名前缀,与脚本一致
            isClustered: false  # 单节点模式(集群需改为true)
            clusterCheckinInterval: 10000
            txIsolationLevelSerializable: true
src/main/resources/application-hckx.yml
@@ -157,6 +157,31 @@
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms
  # Quartz定时任务配置(新增部分)
  quartz:
    job-store-type: jdbc  # 使用数据库存储
    jdbc:
      initialize-schema: never  # 首次运行时自动创建表结构,成功后改为never
      schema: classpath:org/quartz/impl/jdbcjobstore/tables_mysql_innodb.sql  # MySQL表结构脚本
    properties:
      org:
        quartz:
          scheduler:
            instanceName: RuoYiScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate  # MySQL适配
            tablePrefix: qrtz_  # 表名前缀,与脚本一致
            isClustered: false  # 单节点模式(集群需改为true)
            clusterCheckinInterval: 10000
            txIsolationLevelSerializable: true
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10  # 线程池大小
            threadPriority: 5
            makeThreadsDaemons: true
          updateCheck: false  # 关闭版本检查
# token配置
token:
  # 令牌自定义标识