弹簧模块化石英配置

时间:2017-12-12 11:11:43

标签: spring spring-mvc quartz-scheduler

我正在研究使用JDBC Quartz Scheduler执行作业的Spring MVC Web应用程序。此Web应用程序具有多个服务,每个服务都打包为jar并具有关联的作业。我有一个在web项目中的石英配置,在其下我为作业,触发器和调度程序创建了bean。在调度程序配置下,我指定触发器列表。当我们开始我们的项目时,这很好。随着我们开始添加功能,工作数量增加,现在大约100多个。维护此文件变得很困难。是否有任何可用的方法使其模块化,以便在单个文件中不定义作业/触发/关联触发器到调度程序。

 <bean name="incidentAutoClosure" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="itsm.scheduler.task.IncidentAutoClosureTask"/>
 </bean>
 <bean id="incidentAutoClosureTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="incidentAutoClosure" />
    <property name="cronExpression" value="0 0 12 1/1 * ? *" />
 </bean>
 <bean id="quartzJdbcScheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
    p:waitForJobsToCompleteOnShutdown="true">
    <property name="autoStartup" value="true" />
    <property name="startupDelay" value="0" />
    <property name="overwriteExistingJobs" value="false" />
    <property name="quartzProperties">
        <props>
            <prop key="org.quartz.scheduler.instanceName">mvcJdbcScheduler</prop>
            <prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
            <prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.PostgreSQLDelegate</prop>
            <prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
            <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
            <prop key="org.quartz.threadPool.threadCount">${quartz.async.threadCount}</prop>
            <prop key="org.quartz.threadPool.threadPriority">${quartz.async.threadPriority}</prop>
            <prop key="org.quartz.jobStore.misfireThreshold">${quartz.async.misfireThreshold}</prop>
            <prop key="org.quartz.jobStore.isClustered">true</prop>
            <prop key="org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer">true</prop>
                <prop key="org.quartz.plugin.jobHistory.class">org.quartz.plugins.history.LoggingJobHistoryPlugin</prop>
                <prop key="org.quartz.plugin.jobHistory.jobToBeFiredMessage">${quartz.async.initTriggerFormat}</prop>
                <prop key="org.quartz.plugin.jobHistory.jobSuccessMessage">${quartz.async.endTriggerFormat}</prop>
        </props>
    </property>
     <property name="triggers">
        <list>
                <ref bean="incidentAutoClosureTrigger" />
                <!-- List of 100 Jobs -->
        </list>
    </property>
</bean>

2 个答案:

答案 0 :(得分:0)

很少有东西,你在使用春季靴子吗?解决此问题的一个好方法是创建自动配置类,您可以在其中获得组合列表。这是一个示例:

@Configuration
@ConditionalOnBean(SomeClassDependency.class)
public class JobConfig {

    @Bean
    @ConditionalOnProperty(....)
    public SchedulerFactoryBean config1() {
        ....
    }

    @Bean
    @ConditionalOnProperty(....)
    public SchedulerFactoryBean config2() {
        ....
    }

    @Bean
    @ConditionalOnProperty(....)
    public SchedulerFactoryBean config3() {
        ....
    }
}

public class MyDependency {

    private final SchedulerFactoryBean schedulerFactoryBean;

    public MyDependency(@Qualifier("config2") SchedulerFactoryBean schedulerFactoryBean) {
        this.schedulerFactoryBean = schedulerFactoryBean
    }
}

答案 1 :(得分:0)

您可以通过配置类以编程方式执行此操作,该配置类将自动配置您的bean而不是使用xml文件;

这是我使用的。注意:将applicationContext添加到jobFactory允许Quartz调度作业在作业执行时知道弹簧自动装配的类,否则您将获得空的自动装载

@Configuration
public class PortalQuartzSchedulerConfiguration {

  @Autowired
  private ApplicationContext applicationContext;

  @Bean
  public JobFactory jobFactory() {
    ApplicationContextHolder jobFactory = new ApplicationContextHolder();
    jobFactory.setApplicationContext(applicationContext);
    return jobFactory;
  }

  @Bean
  public SchedulerFactoryBean schedulerFactory() {
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
    factory.setAutoStartup(true);
    factory.setSchedulerName("Portal scheduler");
    factory.setOverwriteExistingJobs(true);
    factory.setJobFactory(jobFactory());
    return factory;
  }

}