maven
3 天以前 a78fb9a836c51b5b569b2c7da32d4d2fc2b58925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.ruoyi.inspectiontask.service.impl;
 
import org.quartz.JobDataMap;
import org.quartz.Scheduler;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
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 JobDetailFactoryBean jobDetail() {
        JobDetailFactoryBean factory = new JobDetailFactoryBean();
        factory.setJobClass(TimingTaskJob.class); // 你的自定义 Job 类(需实现 Job 接口)
 
        // 1. 核心:设置为持久化任务(必须,否则不写入 qrtz_job_details 或重启清理)
        factory.setDurability(true);
 
        // 2. 可选:任务执行崩溃后,重启时是否恢复执行(建议开启)
        factory.setRequestsRecovery(true);
 
//        // 3. 可选:设置 JobDataMap(若需传递参数,参数需可序列化)
//        JobDataMap jobDataMap = new JobDataMap();
//        jobDataMap.put("key", "value"); // 示例参数(若参数是自定义对象,需实现 Serializable)
//        factory.setJobDataMap(jobDataMap);
 
        return factory;
    }
 
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
 
        // 配置SpringBeanJobFactory,用于支持Job中的依赖注入
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        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 {
 
        private transient AutowireCapableBeanFactory beanFactory;
 
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.beanFactory = applicationContext.getAutowireCapableBeanFactory();
        }
 
        @Override
        protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
            Object jobInstance = super.createJobInstance(bundle);
            // 将Job实例交给Spring容器管理,使其能够进行依赖注入
            beanFactory.autowireBean(jobInstance);
            return jobInstance;
        }
    }
}