简体   繁体   English

用@Configuration 替换 Quartz Spring 配置 xml 和属性

[英]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.我正在将使用 Spring 3 构建的 Web 应用程序迁移到基于 Spring 4 Boot 的应用程序。

I'd like to replace all xml and properties with a @Configuration class.我想用@Configuration 类替换所有 xml 和属性。

Original spring xml config原始 spring xml 配置

<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原始的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.我已经设法用下面的代码替换了 spring xml,但它仍然使用外部quartz.properties 来配置大多数quartz 功能,包括数据源。

@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 :我设法使用以下代码实现了完整的 Java Spring 配置:

@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).虽然我不确定不同的作业存储实现(是 JobStoreTX,现在是 LocalDataSourceJobStore)。 I'm wondering this is related to the use of the spring autowired datasource.我想知道这与使用 spring 自动装配数据源有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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