Skip to content

Commit eafc89e

Browse files
committed
scheduler可配置
1 parent 26e2e60 commit eafc89e

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

hsweb-web-service/hsweb-web-service-simple/src/main/java/org/hsweb/web/service/impl/SchedulerAutoConfiguration.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.quartz.spi.JobFactory;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
27+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2528
import org.springframework.context.ApplicationContext;
2629
import org.springframework.context.annotation.Bean;
2730
import org.springframework.context.annotation.Configuration;
@@ -33,8 +36,12 @@
3336

3437
@Configuration
3538
@ConditionalOnClass(QuartzScheduler.class)
39+
@EnableConfigurationProperties(SchedulerProperties.class)
3640
public class SchedulerAutoConfiguration {
3741

42+
@Autowired
43+
private SchedulerProperties schedulerProperties;
44+
3845
@Autowired
3946
private ApplicationContext applicationContext;
4047

@@ -55,13 +62,15 @@ public JobFactory jobFactory() {
5562
public SchedulerFactoryBean schedulerFactory(JobFactory jobFactory) {
5663
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
5764
schedulerFactoryBean.setApplicationContext(applicationContext);
58-
schedulerFactoryBean.setAutoStartup(true);
65+
schedulerFactoryBean.setAutoStartup(schedulerProperties.isAutoStartup());
5966
schedulerFactoryBean.setDataSource(dataSource);
6067
schedulerFactoryBean.setTransactionManager(platformTransactionManager);
61-
schedulerFactoryBean.setOverwriteExistingJobs(true);
68+
schedulerFactoryBean.setOverwriteExistingJobs(schedulerProperties.isOverwriteExistingJobs());
6269
schedulerFactoryBean.setSchedulerFactoryClass(StdSchedulerFactory.class);
63-
schedulerFactoryBean.setBeanName("scheduler");
70+
schedulerFactoryBean.setBeanName(schedulerProperties.getBeanName());
6471
schedulerFactoryBean.setJobFactory(jobFactory);
72+
schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(schedulerProperties.isWaitOnShutdown());
73+
schedulerFactoryBean.setQuartzProperties(schedulerProperties.getProperties());
6574
return schedulerFactoryBean;
6675
}
6776
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2015-2016 http://hsweb.me
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.hsweb.web.service.impl;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
import java.util.Properties;
22+
23+
@ConfigurationProperties(prefix = "scheduler")
24+
public class SchedulerProperties {
25+
private boolean autoStartup = true;
26+
27+
private boolean overwriteExistingJobs = true;
28+
29+
private String beanName = "scheduler";
30+
31+
private boolean waitOnShutdown = true;
32+
33+
private Properties properties;
34+
35+
public boolean isAutoStartup() {
36+
return autoStartup;
37+
}
38+
39+
public void setAutoStartup(boolean autoStartup) {
40+
this.autoStartup = autoStartup;
41+
}
42+
43+
public boolean isOverwriteExistingJobs() {
44+
return overwriteExistingJobs;
45+
}
46+
47+
public void setOverwriteExistingJobs(boolean overwriteExistingJobs) {
48+
this.overwriteExistingJobs = overwriteExistingJobs;
49+
}
50+
51+
public String getBeanName() {
52+
return beanName;
53+
}
54+
55+
public void setBeanName(String beanName) {
56+
this.beanName = beanName;
57+
}
58+
59+
public boolean isWaitOnShutdown() {
60+
return waitOnShutdown;
61+
}
62+
63+
public void setWaitOnShutdown(boolean waitOnShutdown) {
64+
this.waitOnShutdown = waitOnShutdown;
65+
}
66+
67+
public Properties getProperties() {
68+
return properties;
69+
}
70+
71+
public void setProperties(Properties properties) {
72+
this.properties = properties;
73+
}
74+
}

hsweb-web-service/hsweb-web-service-simple/src/test/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
logging:
22
config: classpath:logback.xml
3+
scheduler:
4+
properties:
5+
org.quartz.jobStore.isClustered: true
36
spring:
47
aop:
58
auto: true

0 commit comments

Comments
 (0)