Initialize Quartz scheduler with Spring 4/Boot

2019-08-02 14:03发布

问题:

I have a Spring 4 application with Spring Boot -

There is no WEBINF/web.xml file, however, I'd like to initialize a Quartz 2.2.1 scheduler on application startup. However, all the examples using QuartzInitializerServlet define the settings in the web.xml file.

Can I add these configurations to my application startup configuration?

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
        @Bean
public DataSource dataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setUrl("jdbc:postgresql://localhost/...");
    ds.setUsername("...");
    ds.setPassword("...!");
    return ds;
}
    /** Add configuration to start Quartz here so 
        I can access it throughout the app? **/ 

@Bean
public org.springframework.scheduling.quartz.SchedulerFactoryBean SchedulerFactoryBean(){
    SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
    scheduler.setAutoStartup(true);
    scheduler.setDataSource(dataSource());
    return scheduler;

}

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Update

Figured out the spring-framework quartz bean, now I need to correctly implement the datastore to restore jobs in-between runs.

I'm using postgresql + spring-data & hibernate. This configuration reinitializes the database on each run. HSQL reinitializes some 'import.sql' data as well. Should I create a hibernate interface so that the jobs will be restored on testing?