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

时间:2015-11-30 18:45:07

标签: java spring spring-mvc quartz-scheduler

我将使用Spring 3构建的Web应用程序迁移到基于Spring 4 Boot的应用程序。

我想用@Configuration类替换所有xml和属性。

原始的春天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

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

我设法用以下代码替换spring xml,但它仍然使用外部quartz.properties来配置大多数石英功能,包括数据源。

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

请注意,这会在应用启动期间导致以下输出:

  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.

1 个答案:

答案 0 :(得分:0)

我设法使用以下代码实现完整的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;
    }
}

这会在应用启动期间产生以下输出

  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.

为了我的目的,这是令人满意的;虽然我不确定不同的作业存储实现(JobStoreTX和现在的LocalDataSourceJobStore)。我想知道这与使用弹簧自动装配的数据源有关。