Spring-Boot与Quartz和多个调度程序

3
我正在处理一个场景,我们有一个包含多个模式的数据库,每个客户都有一个模式。这使得每个客户都可以为其作业设置不同的时间表。所有模式都具有相同的作业集,只是时间表不同。
我需要编写一个Spring-Boot应用程序来运行所有模式的所有作业。
似乎可以通过为每个模式定义不同的quartz.properties,然后为每个模式配置不同的调度器来完成此操作,例如:
@SpringBootApplication
@Configuration
public class MyApplication{

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

    @Bean
    public Scheduler schedulerA(Trigger trigger, JobDetail job) {
        StdSchedulerFactory factory = new StdSchedulerFactory();
        factory.initialize(new ClassPathResource("quartzA.properties").getInputStream());

        Scheduler scheduler = factory.getScheduler();
        scheduler.setJobFactory(springBeanJobFactory());
        scheduler.scheduleJob(job, trigger);

        scheduler.start();
        return scheduler;
    }

    @Bean
    public Scheduler schedulerB(Trigger trigger, JobDetail job) {
        StdSchedulerFactory factory = new StdSchedulerFactory();
        factory.initialize(new ClassPathResource("quartzB.properties").getInputStream());

        Scheduler scheduler = factory.getScheduler();
        scheduler.setJobFactory(springBeanJobFactory());
        scheduler.scheduleJob(job, trigger);

        scheduler.start();
        return scheduler;
    }    
}

我的问题是,这样做是否正确?我只需在标有@Configuration的SpringBootApplication类中定义这些调度程序,并期望它能正常工作(假设属性是正确的)吗?我有遗漏什么吗?

2个回答

2
我的问题是,这个做法正确吗?我可以在标记有@Configuration的SpringBootApplication类中定义这些调度程序。
这是正确的。另外,您还可以使用Spring的@Scheduled注释,并在属性文件中定义Cron。
@Scheduled(cron = "0 15 10 15 * ?")
public void scheduleTaskUsingCronExpression() {
.
.
.

但是,如果您想要更多关于作业故障转移、重试策略或从仪表板跟踪和运行/重新运行作业的控制,请考虑使用spring-batch


1
受上述示例的启发,我找到了一种使用应用程序属性管理配置的方法,这似乎更容易且符合Spring-Boot应用程序的其余部分。它特别适用于重用数据源配置。第二类bean的数量是任意的。
@Configuration
class MainQuartzConfiguration {
    /**
     * Main scheduler bean where all jobDetails, calendars and trigger beans are attached.
     * 
     */
    @Primary @Bean
    public SchedulerFactoryBean mainScheduler(QuartzProperties properties,
            ObjectProvider<SchedulerFactoryBeanCustomizer> customizers,
            ObjectProvider<JobDetail[]> jobDetails, Map<String, Calendar> calendars,
            ObjectProvider<Trigger[]> triggers, ApplicationContext applicationContext) {
        SchedulerFactoryBean factory = new QuartzAutoConfiguration(properties, customizers, jobDetails, calendars, triggers, applicationContext)
                .quartzScheduler();
        factory.setSchedulerName("mainScheduler");
        return factory;
    }
}

@Configuration 
class AnotherConfiguration {
    /**
     * Second scheduler bean which has the same configuration but different thread count and thread priority.
     */
    
    @Bean
    SchedulerFactoryBean secondScheduler(
            QuartzProperties properties,
            ObjectProvider<SchedulerFactoryBeanCustomizer> customizers,
            @Value("${spring.quartz.properties.secondScheduler.org.quartz.threadPool.threadPriority:7}") int threadPriority,
            @Value("${spring.quartz.properties.secondScheduler.org.quartz.threadPool.threadCount:1}") int threadCount,
            ApplicationContext applicationContext)
    {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        schedulerFactoryBean.setJobFactory(jobFactory);
        schedulerFactoryBean.setSchedulerName("secondScheduler");
        schedulerFactoryBean.setAutoStartup(properties.isAutoStartup());
        schedulerFactoryBean
        .setStartupDelay((int) properties.getStartupDelay().getSeconds());
        schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(
                properties.isWaitForJobsToCompleteOnShutdown());
        Properties propertiesVariant = new Properties();
        propertiesVariant.putAll(properties.getProperties());
        propertiesVariant.setProperty("org.quartz.threadPool.threadPriority", Integer.toString(threadPriority));
        propertiesVariant.setProperty("org.quartz.threadPool.threadCount", Integer.toString(threadCount));
        schedulerFactoryBean.setQuartzProperties(propertiesVariant);
        schedulerFactoryBean.setJobDetails(CatalogBenchmarkJob.createJob());
        customizers.orderedStream().forEach(
                (customizer) -> customizer.customize(schedulerFactoryBean));
        return schedulerFactoryBean;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接