maven
7 小时以前 0f373c673262105ee6a437be4ef32d5ecc9f1114
yys  定时任务巡检加入持久化
已修改5个文件
136 ■■■■ 文件已修改
src/main/java/com/ruoyi/inspectiontask/service/impl/QuartzConfig.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskScheduler.java 86 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/project/monitor/service/impl/SysJobServiceImpl.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-jyhj.yml 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/inspectiontask/service/impl/QuartzConfig.java
@@ -1,5 +1,6 @@
package com.ruoyi.inspectiontask.service.impl;
import org.quartz.Scheduler;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
@@ -10,10 +11,16 @@
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 +31,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/TimingTaskScheduler.java
@@ -39,18 +39,21 @@
            throw new SchedulerException("Existing trigger is not a CronTrigger");
        }
        // 构建新触发器
        Trigger newTrigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerKey)
                .withDescription(task.getTaskName())
                .withSchedule(CronScheduleBuilder.cronSchedule(convertToCronExpression(task)))
                .startAt(Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant()))
                .forJob(oldTrigger.getJobKey())
        // 3. 构建CronTrigger,确保持久化配置
        CronTrigger newTrigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerKey)                // 唯一标识,用于持久化存储
                .withDescription(task.getTaskName() + "_TRIGGER") // 触发器描述
                .forJob(oldTrigger.getJobKey())                       // 关联对应的Job
                .withSchedule(CronScheduleBuilder
                        .cronSchedule(convertToCronExpression(task)) // 错过执行时的策略(根据业务调整)
                )
                // 4. 设置开始时间(若为null则立即生效)
                .startAt(task.getNextExecutionTime() != null
                        ? Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant())
                        : new Date())
                .build();
        scheduler.rescheduleJob(triggerKey, newTrigger);
    }
    /**
     * 暂停任务
     */
@@ -70,41 +73,60 @@
    /**
     * 删除任务
     */
    public void unscheduleTimingTask(Long taskId) throws SchedulerException {
        JobKey jobKey = new JobKey("timingTask_" + taskId);
        scheduler.deleteJob(jobKey);
    public void unscheduleTimingTask(Long taskId) {
        try {
            JobKey jobKey = new JobKey("timingTask_" + taskId);
            scheduler.deleteJob(jobKey);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    private JobDetail buildJobDetail(TimingTask task) {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("taskId", task.getId());
        // 1. 构建唯一JobKey(基于任务ID,确保重启后能识别)
        JobKey jobKey = new JobKey("timingTask_" + task.getId());
        // 2. 封装任务数据(仅使用基本类型,确保可序列化)
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("taskId", task.getId());           // 任务ID(Long,可序列化)
        jobDataMap.put("taskName", task.getTaskName());   // 任务名称(String,可序列化)
        jobDataMap.put("taskType", task.getFrequencyType()); // 任务类型(String)
        // 按需添加其他必要的基本类型参数
        // 3. 构建JobDetail,设置持久化相关属性
        return JobBuilder.newJob(TimingTaskJob.class)
                .withIdentity("timingTask_" + task.getId())
                .withDescription(task.getTaskName())
                .usingJobData(jobDataMap)
                .storeDurably()
                .withIdentity(jobKey)                    // 唯一标识,用于持久化存储
                .withDescription(task.getTaskName())     // 任务描述,存入数据库
                .usingJobData(jobDataMap)                // 绑定任务数据
                .storeDurably(true)                          // 即使没有触发器关联也持久化保存
                .requestRecovery(true)                   // 当调度器崩溃后恢复时,重新执行未完成的任务
                .build();
    }
    private Trigger buildJobTrigger(TimingTask task, JobDetail jobDetail) {
        // 1. 构建唯一TriggerKey(基于任务ID)
        TriggerKey triggerKey = new TriggerKey("trigger_" + task.getId());
        // 2. 生成Cron表达式(原逻辑不变)
        String cronExpression = convertToCronExpression(task);
            TriggerBuilder<CronTrigger> triggerBuilder = TriggerBuilder.newTrigger()
                .withIdentity("trigger_" + task.getId())
                .withDescription(task.getTaskName())
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression));
        if (jobDetail != null) {
            triggerBuilder.forJob(jobDetail);
        }
        if (task.getNextExecutionTime() != null) {
            triggerBuilder.startAt(Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant()));
        }
        return triggerBuilder.build();
        // 3. 构建CronTrigger,确保持久化配置
        return TriggerBuilder.newTrigger()
                .withIdentity(triggerKey)                // 唯一标识,用于持久化存储
                .withDescription(task.getTaskName() + "_TRIGGER") // 触发器描述
                .forJob(jobDetail)                       // 关联对应的Job
                .withSchedule(CronScheduleBuilder
                        .cronSchedule(cronExpression)
                        .withMisfireHandlingInstructionDoNothing() // 错过执行时的策略(根据业务调整)
                )
                // 4. 设置开始时间(若为null则立即生效)
                .startAt(task.getNextExecutionTime() != null
                        ? Date.from(task.getNextExecutionTime().atZone(ZoneId.systemDefault()).toInstant())
                        : new Date())
                .build();
    }
    private String convertToCronExpression(TimingTask task) {
        // 参数校验
        if (task == null || task.getFrequencyType() == null || task.getFrequencyDetail() == null) {
src/main/java/com/ruoyi/inspectiontask/service/impl/TimingTaskServiceImpl.java
@@ -444,7 +444,13 @@
    @Override
    public int delByIds(Long[] ids) {
        return timingTaskMapper.deleteBatchIds(Arrays.asList(ids));
        int i = timingTaskMapper.deleteBatchIds(Arrays.asList(ids));
        if(i > 0){
            for (Long id : ids) {
                timingTaskScheduler.unscheduleTimingTask(id);
            }
        }
        return i;
    }
}
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-jyhj.yml
@@ -157,6 +157,32 @@
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        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:
  # 令牌自定义标识