Skip to content

Commit c58853f

Browse files
Daniil Pozdeevsbrannen
Daniil Pozdeev
authored andcommitted
Use LocalDataSourceJobStore only if one is not set via Quartz properties
Prior to this commit, Spring's SchedulerFactoryBean always set the "org.quartz.jobStore.class" property to LocalDataSourceJobStore even if the user had already specified a custom JobStore implementation via the Quartz properties file or Properties object, thereby effectively ignoring the user configuration. This commit addresses this by configuring Quartz to use Spring's LocalDataSourceJobStore only if a JobStore has not already been specified via user configuration. Closes gh-27560
1 parent 66826ac commit c58853f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ private void initSchedulerFactory(StdSchedulerFactory schedulerFactory) throws S
570570

571571
CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
572572
if (this.dataSource != null) {
573-
mergedProps.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
573+
mergedProps.putIfAbsent(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
574574
}
575575

576576
// Determine scheduler name across local settings and Quartz properties...

spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21+
import java.util.Properties;
2122

2223
import javax.sql.DataSource;
2324

@@ -30,6 +31,7 @@
3031
import org.quartz.SchedulerFactory;
3132
import org.quartz.impl.JobDetailImpl;
3233
import org.quartz.impl.SchedulerRepository;
34+
import org.quartz.impl.jdbcjobstore.JobStoreTX;
3335

3436
import org.springframework.beans.factory.config.BeanDefinition;
3537
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -40,6 +42,8 @@
4042
import org.springframework.core.task.TaskExecutor;
4143
import org.springframework.core.testfixture.EnabledForTestGroups;
4244
import org.springframework.jdbc.core.JdbcTemplate;
45+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
46+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4347

4448
import static org.assertj.core.api.Assertions.assertThat;
4549
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -396,6 +400,29 @@ public void schedulerWithHsqlDataSource() throws Exception {
396400
}
397401
}
398402

403+
@Test
404+
public void schedulerFactoryBeanWithCustomJobStore() throws Exception {
405+
StaticApplicationContext context = new StaticApplicationContext();
406+
407+
final String dbName = "mydb";
408+
final EmbeddedDatabase database = new EmbeddedDatabaseBuilder().setName(dbName).build();
409+
410+
final Properties properties = new Properties();
411+
properties.setProperty("org.quartz.jobStore.class", JobStoreTX.class.getName());
412+
properties.setProperty("org.quartz.jobStore.dataSource", dbName);
413+
414+
BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(SchedulerFactoryBean.class)
415+
.addPropertyValue("autoStartup", false)
416+
.addPropertyValue("dataSource", database)
417+
.addPropertyValue("quartzProperties", properties)
418+
.getBeanDefinition();
419+
context.registerBeanDefinition("scheduler", beanDefinition);
420+
421+
Scheduler bean = context.getBean("scheduler", Scheduler.class);
422+
423+
assertThat(bean.getMetaData().getJobStoreClass()).isEqualTo(JobStoreTX.class);
424+
}
425+
399426
private ClassPathXmlApplicationContext context(String path) {
400427
return new ClassPathXmlApplicationContext(path, getClass());
401428
}

0 commit comments

Comments
 (0)