SchedulerConfig.java
3.31 KB
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
package com.idss.vulsync.config;
import cloud.agileframework.spring.util.PropertiesUtil;
import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import java.io.IOException;
import java.util.Properties;
@Configuration
@ConditionalOnProperty(prefix = "spring", value = "quartz.enable", havingValue = "true", matchIfMissing = false)
public class SchedulerConfig {
@Bean(name="SchedulerFactory")
public SchedulerFactoryBean schedulerFactoryBean(ApplicationContext applicationContext) throws IOException {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
schedulerFactoryBean.setJobFactory(jobFactory);
schedulerFactoryBean.setQuartzProperties(quartzProperties());
return schedulerFactoryBean;
}
@Bean(name="QuartzScheduler")
public Scheduler quartzScheduler(ApplicationContext applicationContext) throws IOException {
return schedulerFactoryBean(applicationContext).getScheduler();
}
/**
* quartz初始化监听器
* 这个监听器可以监听到工程的启动,在工程停止再启动时可以让已有的定时任务继续进行。
*
* @return
*/
@Bean
public QuartzInitializerListener executorListener() {
return new QuartzInitializerListener();
}
@Bean
public Properties quartzProperties() throws IOException {
Properties properties = new Properties();
properties.put("org.quartz.scheduler.instanceName", "asset-service-vulsync");
properties.put("org.quartz.scheduler.instanceId", "AUTO");
properties.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
properties.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate");
properties.put("org.quartz.jobStore.tablePrefix", PropertiesUtil.getProperty("spring.quartz.prefix"));
properties.put("org.quartz.jobStore.isClustered", "true");
properties.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
properties.put("org.quartz.threadPool.threadCount", "10");
properties.put("org.quartz.threadPool.threadPriority", "5");
properties.put("org.quartz.jobStore.dataSource", "myDS");
properties.put("org.quartz.dataSource.myDS.driver", PropertiesUtil.getProperty("spring.datasource.driver-class-name"));
properties.put("org.quartz.dataSource.myDS.URL", PropertiesUtil.getProperty("spring.datasource.url"));
properties.put("org.quartz.dataSource.myDS.user", PropertiesUtil.getProperty("spring.datasource.username"));
properties.put("org.quartz.dataSource.myDS.password", PropertiesUtil.getProperty("spring.datasource.password"));
properties.put("org.quartz.dataSource.myDS.maxConnections", "10");
return properties;
}
}