# 调度器配置 返回

package chances.quartz.config;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;

import javax.sql.DataSource;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * 调度器配置
 * Schedule:调度器
 * Trigger:触发器
 * job:任务(一个任务可以对应多个触发器)
 */
@Configuration
public class QuartzSchedulerConfig {

    @Value("${chances.system-name:DEFAULT}")
    private String jobGroup;

    @Autowired
    private DataSource dataSource;

    /**
     * 调度器
     * @return
     * @throws Exception
     */
    @Bean
    public Scheduler scheduler() throws Exception {
        return schedulerFactoryBean().getScheduler();
    }

    /**
     * Scheduler工厂类
     * @return
     * @throws IOException
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setDataSource(dataSource);
        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
        factory.setTaskExecutor(schedulerThreadPool());

        factory.setQuartzProperties(getProperties());

        factory.setSchedulerName("ClusterQuartz_" + this.jobGroup);
        
        // 延时启动
        factory.setStartupDelay(0);
        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
        // 可选,QuartzScheduler
        // 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
        factory.setOverwriteExistingJobs(true);
        // 设置自动启动,默认为true
        factory.setAutoStartup(true);
        return factory;
    }

    public Properties getProperties() {
        Properties properties = new Properties();
        /* 调度器属性配置 */
        // 调度标识名 集群中每个实例都须使用相同的名称
        properties.put("org.quartz.scheduler.instanceName", "ClusterQuartz_" + this.jobGroup);
        // ID每个须不同 设置为自动获取
        properties.put("org.quartz.scheduler.instanceId", "AUTO");
        
        /* JobStore配置 */
        // 数据保存方式:数据库持久化
        properties.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        
        /* 集群配置 */
        // 加入集群
        properties.put("org.quartz.jobStore.isClustered", "true");
        // 调度实例失效的检查时间间隔
        properties.put("org.quartz.jobStore.clusterCheckinInterval", "5000");
        properties.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
        properties.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");

        properties.put("org.quartz.jobStore.misfireThreshold", "12000");
        // quartz相关表的前缀 默认QRTZ_
        properties.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
        
        /* 线程池配置 */
        // 线程池的实现类(一般使用SimpleThreadPool即可满足几乎所有用户的需求)
        properties.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        // 指定线程数,一般设置为1-100直接的整数,根据系统资源配置
        properties.put("org.quartz.threadPool.threadCount", "20");
        // 设置线程的优先级(可以是Thread.MIN_PRIORITY(即1)和Thread.MAX_PRIORITY(这是10)之间的任何int 。默认值为Thread.NORM_PRIORITY(5)。)
        properties.put("org.quartz.threadPool.threadPriority", "5");
        return properties;
    }

    /**
     * schedule配置线程池
     * @return
     */
    // @Bean
    public Executor schedulerThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors());
        executor.setQueueCapacity(Runtime.getRuntime().availableProcessors());
        return executor;
    }

    public String getJobGroup() {
        return jobGroup;
    }

    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }
}
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123