【问题标题】:Spring-Boot with Quartz and multiple schedulers带有 Quartz 和多个调度程序的 Spring-Boot
【发布时间】:2019-02-17 14:28:06
【问题描述】:

我正在处理一个场景,我们有一个具有多个架构的数据库,每个客户一个架构。这允许每个客户为其工作设置不同的时间表。所有模式都有相同的作业集,只是时间表不同。

我需要编写一个 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 类中定义这些调度程序,并期望它能够工作(假设属性是正确的)吗?我错过了什么吗?

【问题讨论】:

    标签: spring spring-boot quartz-scheduler


    【解决方案1】:

    我的问题是,这是正确的吗?我可以在 我用 @Configuration 注释的 SpringBootApplication 类

    这是正确的。或者,您可以将 Spring @Schelduled 注释与属性文件中定义的 Cron 一起使用。

    @Scheduled(cron = "0 15 10 15 * ?")
    public void scheduleTaskUsingCronExpression() {
    .
    .
    .
    

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

    【讨论】:

      【解决方案2】:

      受上述示例的启发,我找到了一种使用应用程序属性中管理的配置的方法,这似乎更容易,并且与 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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-17
        • 1970-01-01
        • 2017-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多