#63 tested the default name assignment to the scheduler instance

This commit is contained in:
Fabio Formosa
2022-10-01 17:20:41 +02:00
parent 1571ab6d12
commit 1e99602c68
4 changed files with 94 additions and 30 deletions

View File

@@ -21,34 +21,48 @@ import java.util.Properties;
@ConditionalOnProperty(name = "quartz.enabled", matchIfMissing = true)
public class SchedulerConfig {
@Autowired(required = false)
private QuartzModuleProperties quartzModuleProperties;
protected static final String QUARTZ_MANAGER_SCHEDULER_DEFAULT_NAME = "quartz-manager-scheduler";
@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
private final QuartzModuleProperties quartzModuleProperties;
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Autowired(required = false)
public SchedulerConfig(QuartzModuleProperties quartzModuleProperties) {
this.quartzModuleProperties = quartzModuleProperties;
}
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setJobFactory(jobFactory);
Properties mergedProperties = new Properties();
if(quartzModuleProperties != null)
mergedProperties.putAll(quartzModuleProperties.getProperties());
mergedProperties.putAll(quartzProperties());
factory.setQuartzProperties(mergedProperties);
factory.setAutoStartup(false);
return factory;
}
@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean("quartzDefaultProperties")
public QuartzModuleProperties persistenceQuartzProps() {
QuartzModuleProperties quartzModuleProperties = new QuartzModuleProperties();
quartzModuleProperties.getProperties().setProperty("org.quartz.scheduler.instanceName", QUARTZ_MANAGER_SCHEDULER_DEFAULT_NAME);
quartzModuleProperties.getProperties().setProperty("org.quartz.threadPool.threadCount", "1");
return quartzModuleProperties;
}
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setJobFactory(jobFactory);
Properties mergedProperties = new Properties();
if (quartzModuleProperties != null)
mergedProperties.putAll(quartzModuleProperties.getProperties());
mergedProperties.putAll(quartzProperties());
factory.setQuartzProperties(mergedProperties);
factory.setAutoStartup(false);
return factory;
}
}

View File

@@ -3,9 +3,12 @@ package it.fabioformosa.quartzmanager.configuration;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static it.fabioformosa.quartzmanager.configuration.SchedulerConfig.QUARTZ_MANAGER_SCHEDULER_DEFAULT_NAME;
@SpringBootTest
class SchedulerConfigDefaultAppPropertiesTest {
@@ -13,9 +16,14 @@ class SchedulerConfigDefaultAppPropertiesTest {
private Scheduler scheduler;
@Test
void givenTheQuartzPropMissing_whenTheBootstrapOccurs_thenAQuartzInstanceShouldBeInstanciated(){
void givenTheQuartzPropMissing_whenTheBootstrapOccurs_thenAQuartzInstanceShouldBeInstantiated(){
Assertions.assertThat(scheduler).isNotNull();
}
@Test
void givenTheQuartzNameMissing_whenTheBootstrapOccurs_thenAQuartzInstanceShouldBeTheDefaultName() throws SchedulerException {
Assertions.assertThat(scheduler.getSchedulerName()).isEqualTo(QUARTZ_MANAGER_SCHEDULER_DEFAULT_NAME);
}
}

View File

@@ -0,0 +1,42 @@
package it.fabioformosa.quartzmanager.configuration;
import it.fabioformosa.quartzmanager.common.properties.QuartzModuleProperties;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.quartz.Scheduler;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
class SchedulerConfigTest {
public static final String TEST_SCHEDULER_NAME = "foo";
public static final String QUARTZ_SCHEDULER_DEFAULT_NAME = "QuartzScheduler";
@Test
void givenASchedulerName_whenTheSchedulerIsInstatiated_thenTheSchedulerHasThatName() throws Exception {
QuartzModuleProperties quartzModuleProperties = new QuartzModuleProperties();
quartzModuleProperties.getProperties().put("org.quartz.scheduler.instanceName", TEST_SCHEDULER_NAME);
SchedulerConfig schedulerConfig = new SchedulerConfig(quartzModuleProperties);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
SchedulerFactoryBean schedulerFactoryBean = schedulerConfig.schedulerFactoryBean(schedulerConfig.jobFactory(applicationContext));
schedulerFactoryBean.afterPropertiesSet();
Scheduler scheduler = schedulerFactoryBean.getScheduler();
Assertions.assertThat(scheduler.getSchedulerName()).isEqualTo(TEST_SCHEDULER_NAME);
}
@Test
void givenNoSchedulerName_whenTheSchedulerIsInstatiated_thenTheSchedulerHasTheDefaultName() throws Exception {
QuartzModuleProperties quartzModuleProperties = new QuartzModuleProperties();
SchedulerConfig schedulerConfig = new SchedulerConfig(quartzModuleProperties);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
SchedulerFactoryBean schedulerFactoryBean = schedulerConfig.schedulerFactoryBean(schedulerConfig.jobFactory(applicationContext));
schedulerFactoryBean.afterPropertiesSet();
Scheduler scheduler = schedulerFactoryBean.getScheduler();
Assertions.assertThat(scheduler.getSchedulerName()).isEqualTo(QUARTZ_SCHEDULER_DEFAULT_NAME);
}
}

View File

@@ -1,2 +1,2 @@
org.quartz.scheduler.instanceName=example
org.quartz.threadPool.threadCount=1
#org.quartz.scheduler.instanceName=test //disabled to use the default value
#org.quartz.threadPool.threadCount=1 //disabled to use the default value