简体   繁体   中英

Replace Quartz Spring configuration xml and properties with @Configuration

I'm migrating a web application built with Spring 3 to a Spring 4 Boot based app.

I'd like to replace all xml and properties with a @Configuration class.

Original spring xml config

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="configLocation" value="classpath:quartz.properties"/>
    <property name="applicationContextSchedulerContextKey">
        <value>applicationContext</value>
    </property>
</bean>

Original quartz.properties

org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.dataSource=psqldatasource
org.quartz.dataSource.psqldatasource.driver=${db.driver}
org.quartz.dataSource.psqldatasource.URL=${db.url}
org.quartz.dataSource.psqldatasource.user=${db.usr}
org.quartz.dataSource.psqldatasource.password=${db.pwd}
org.quartz.threadPool.threadCount = 3

org.quartz.jobStore.useProperties = false
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate

I've managed to replace spring xml with the following code, but it still use an external quartz.properties to configure most of the quartz features, including datasource.

@Bean
public Scheduler configureScheduler() throws SchedulerException {
    StdSchedulerFactory f = new StdSchedulerFactory();
    f.initialize(this.getClass().getClassLoader().getResourceAsStream("quartz.properties"));
    return f.getScheduler();
}

Note that this leads to the following output during app boot :

  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads.
  Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is not clustered.

I've managed to achieve a full Java Spring configuration using the following code :

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.myapp")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public SchedulerFactoryBean configureScheduler() {
        SchedulerFactoryBean f = new SchedulerFactoryBean();
        f.setDataSource(dataSource);
        f.setJobFactory(new SpringBeanJobFactory());
        f.setAutoStartup(false);
        Properties properties = new Properties();
        properties.setProperty("org.quartz.threadPool.threadCount", "3");
        properties.setProperty("org.quartz.jobStore.useProperties", "false");
        properties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate");
        f.setQuartzProperties(properties);
        f.setApplicationContext(applicationContext);
        f.setApplicationContextSchedulerContextKey("applicationContext");
        return f;
    }
}

This produce the following output during app boot

  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is not clustered.

To my purpose this is satisfying ; although i'm not sure with the different job-store implementation (was JobStoreTX and now LocalDataSourceJobStore). I'm wondering this is related to the use of the spring autowired datasource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM