Rework scheduler to use Spring factory (#2134)

* Rework scheduler to use Spring factory

* Add test
This commit is contained in:
James Agnew 2020-10-19 10:33:12 -04:00 committed by GitHub
parent a25aa37e6a
commit 9e8e98b6fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 3 deletions

View File

@ -33,6 +33,7 @@ import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.annotation.Nonnull;
import java.util.Properties;
@ -49,7 +50,7 @@ public abstract class BaseHapiScheduler implements IHapiScheduler {
private final String myThreadNamePrefix;
private final AutowiringSpringBeanJobFactory mySpringBeanJobFactory;
private final StdSchedulerFactory myFactory = new StdSchedulerFactory();
private final SchedulerFactoryBean myFactory = new SchedulerFactoryBean();
private final Properties myProperties = new Properties();
private Scheduler myScheduler;
@ -73,12 +74,26 @@ public abstract class BaseHapiScheduler implements IHapiScheduler {
@Override
public void init() throws SchedulerException {
setProperties();
myFactory.initialize(myProperties);
myFactory.setQuartzProperties(myProperties);
myFactory.setBeanName(myInstanceName);
myFactory.setSchedulerName(myThreadNamePrefix);
myFactory.setJobFactory(mySpringBeanJobFactory);
massageJobFactory(myFactory);
try {
Validate.notBlank(myInstanceName, "No instance name supplied");
myFactory.afterPropertiesSet();
} catch (Exception e) {
throw new SchedulerException(e);
}
myScheduler = myFactory.getScheduler();
myScheduler.setJobFactory(mySpringBeanJobFactory);
myScheduler.standby();
}
protected void massageJobFactory(SchedulerFactoryBean theFactory) {
// nothing by default
}
protected void setProperties() {
addProperty("org.quartz.threadPool.threadCount", "4");
myProperties.setProperty(PROP_SCHED_INSTANCE_NAME, myInstanceName + "-" + nextSchedulerId());

View File

@ -0,0 +1,24 @@
package ca.uhn.fhir.jpa.sched;
import org.junit.jupiter.api.Test;
import org.quartz.SchedulerException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public class BaseHapiSchedulerTest {
@Test
public void testMissingConfig() {
BaseHapiScheduler sched = new BaseHapiScheduler("hello", new AutowiringSpringBeanJobFactory()) {
};
try {
sched.init();
fail();
} catch (SchedulerException e) {
assertEquals("java.lang.NullPointerException: No instance name supplied", e.getMessage());
}
}
}