(1)在resource目录下新建quartz.properties文件
#============================================================================ # 基础配置 #============================================================================ org.quartz.scheduler.instanceName = JobScheduler org.quartz.scheduler.instanceId = AUTO org.quartz.scheduler.rmi.export = false org.quartz.scheduler.rmi.proxy = false org.quartz.scheduler.wrapJobExecutionInUserTransaction = false #============================================================================ # 调度器线程池配置 #============================================================================ org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 20 org.quartz.threadPool.threadPriority = 5 org.quartz.jobStore.misfireThreshold = 60000 #============================================================================ # Configure JobStore 作业存储配置 #============================================================================ org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties = true org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.dataSource = qzDS org.quartz.jobStore.isClustered = true org.quartz.jobStore.clusterCheckinInterval = 15000 #============================================================================ # JDBC #============================================================================ org.quartz.dataSource.qzDS.driver = com.mysql.jdbc.Driver org.quartz.dataSource.qzDS.URL = jdbc:mysql://localhost:3306/job_scheduler?useUnicode=true&characterEncoding=UTF-8&useSSL=false org.quartz.dataSource.qzDS.user = **************** org.quartz.dataSource.qzDS.password = ************** org.quartz.dataSource.qzDS.maxConnections = 5 org.quartz.dataSource.qzDS.validationQuery = select 0 from dual
关于quartz配置的详情,参见官方文档。
(2) 写配置解析类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import org.quartz.Scheduler; 2 import org.quartz.ee.servlet.QuartzInitializerListener; 3 import org.springframework.beans.factory.config.PropertiesFactoryBean; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.core.io.ClassPathResource; 7 import org.springframework.scheduling.quartz.SchedulerFactoryBean; 8 import org.wcc.crypt.Crypter; 9 import org.wcc.crypt.CrypterFactory; 10 11 import java.io.IOException; 12 import java.util.Properties; 13 14 @Configuration //类似xml中的<beans>标签,一般和@bean注解一起使用来配置一个Bean,让Spring来管理它的生命周期 15 public class SchedulerConfig { 16 17 @Bean(name="SchedulerFactory") 18 public SchedulerFactoryBean schedulerFactoryBean() throws IOException { 19 SchedulerFactoryBean factory = new SchedulerFactoryBean(); 20 factory.setQuartzProperties(quartzProperties()); 21 return factory; 22 } 23 24 /** 25 * 加载Quartz配置 26 * 27 */ 28 @Bean 29 public Properties quartzProperties() throws IOException { 30 //使用Spring的PropertiesFactoryBean对属性配置文件进行管理 31 PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); 32 propertiesFactoryBean.setLocation(new ClassPathResource("/quartz_config.properties")); 33 propertiesFactoryBean.afterPropertiesSet(); 34 35 Properties properties = propertiesFactoryBean.getObject(); 36 37 // 账号密码解密 38 Crypter crypter = CrypterFactory.getCrypter(CrypterFactory.AES_CBC); 39 String user = properties.getProperty("org.quartz.dataSource.qzDS.user"); 40 if (user != null) { 41 user = crypter.decrypt(user); 42 properties.setProperty("org.quartz.dataSource.qzDS.user", user); 43 } 44 String password = properties.getProperty("org.quartz.dataSource.qzDS.password"); 45 if (password != null) { 46 password = crypter.decrypt(password); 47 properties.setProperty("org.quartz.dataSource.qzDS.password", password); 48 } 49 50 return properties; 51 } 52 53 /** 54 * 初始化Quartz监听器,让Spring boot启动时初始化Quartz 55 * 56 */ 57 @Bean 58 public QuartzInitializerListener executorListener() { 59 return new QuartzInitializerListener(); 60 } 61 62 /** 63 * 通过SchedulerFactoryBean获取Scheduler的实例 64 */ 65 @Bean(name="Scheduler") 66 public Scheduler scheduler() throws IOException { 67 return schedulerFactoryBean().getScheduler(); 68 } 69 }