Spring - 要使用的Switch SchedulerFactoryBean [英] Spring - Switch SchedulerFactoryBean To Be Used

查看:142
本文介绍了Spring - 要使用的Switch SchedulerFactoryBean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring的SchedulerFactoryBean在基于Spring的Java应用程序中运行一些Quartz作业。目前,这是一个开发中的单实例应用程序,但是一旦我们开始横向扩展这个,我们将希望使用基于jdbc的JobStore for Quartz,因此只有一个应用程序将运行给定的作业。

I'm using Spring's SchedulerFactoryBean to run some Quartz jobs within a Spring based java application. At present, this is a single instance application in development, but as soon as we start horizontally scaling this we will want to use a jdbc based JobStore for Quartz so no more than one app will run a given job.

现在,SchedulerFactoryBean配置如下:

Right now, SchedulerFactoryBean is configured as follows:

 <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >       
     <property name="taskExecutor" ref="taskExecutor"/>
     <property name="triggers">
         <list>
             <!-- a bunch of triggers here -->
         </list>
     <property name="applicationContextSchedulerContextKey">
         <value>applicationContext</value>
     </property>
</bean>

使用基于jdbc的JobStore它看起来像这样

and with using a jdbc based JobStore it will look like this

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        <property name="dataSource" ref="mysqlJobDataSource"/>
        <property name="taskExecutor" ref="taskExecutor"/>
        <property name="triggers">
            <list>
                <!-- a bunch of triggers here -->
            </list>
        </property>
        <property name="applicationContextSchedulerContextKey">
            <value>applicationContext</value>
        </property>
        <property name="quartzProperties">
            <props>
                <prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
                <prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</prop>
                <!-- and a bunch of other quartz props -->
            </props>
        </property>
    </bean>

理想情况下,我想继续为开发人员使用默认的RAMJobStore版本(第一个)但是对于已部署的环境使用jdbc版本。但是,似乎没有一种非常好的方法可以通过类似属性的东西在两者之间切换,因为jdbc存储涉及更多的配置,而SchedulerFactoryBean上仅存在dataSource属性意味着它尝试基于JDBC的作业商店。

Ideally, I'd like to continue using the default RAMJobStore version (the first one) for developers, but use the jdbc version for deployed environments. However, there doesn't seem to be a very good way to switch between the two through something like a property, since the jdbc store involves lots more configuration and the mere existence of the dataSource property on SchedulerFactoryBean means it tries to a JDBC based job store.

此外,由于SchedulerFactoryBean是一个初始化bean,初始化基本上开始运行所有作业,所以我不能在配置文件中定义这两个bean进入弹簧环境,这意味着我将运行并行作业。

Also, Since SchedulerFactoryBean is an initializing bean where the initializing basically starts running all of the jobs, so I can't have both of those beans defined in a config file loaded into the spring context either, which means I'll have parallel jobs running.

我还读过这个答案,但这种情况不同之处在于我正在处理两个不应该同时在同一个上下文中的InitializingBeans。

I've also read through this answer, but this situtation differs in that I'm dealing with two InitializingBeans that should never be in the same context at the same time.

什么是配置SchedulerFactoryBean这两种配置之间切换的最简单方法吗?

What would be the simplest way to configure switching between these two configurations of SchedulerFactoryBean?

推荐答案

从Spring 3.1开始,您可以使用Spring配置文件:

From Spring 3.1 you can use Spring profiles:

<bean name="schedulerFactoryBean" profile="dev" ...

<bean name="schedulerFactoryBean" profile="prd" ...

然后你可以指示Spring容器哪个配置文件使用,请参阅如何通过适当的文件设置活动的spring 3.1环境配置文件,而不是通过env变量或系统属性弹簧autowire一个存根服务 - 重复的bean

Then you can instruct Spring container which profile to use, see How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property and Spring autowire a stubbed service - duplicate bean.

如果你不能使用3.1或配置文件,解决这些问题的老派就是有两个上下文files: schedulerContext-dev.xml 和schedulerContext-prd.xml`。然后你可以有选择地导入它们:

If you can't use 3.1 or profiles, the old-school of solving such issues is to have two context files: schedulerContext-dev.xml and schedulerContext-prd.xml`. Then you can import them selectively:

<import resource="schedulerContext-${some.property}"/>

这篇关于Spring - 要使用的Switch SchedulerFactoryBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆