Initialize Quartz scheduler with Spring 4/Boot

给你一囗甜甜゛ 提交于 2020-01-15 10:59:06

问题


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?

来源:https://stackoverflow.com/questions/20726879/initialize-quartz-scheduler-with-spring-4-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!