如何在一台主机上运行石英作业

时间:2014-07-08 19:49:07

标签: java timer quartz-scheduler jobs job-scheduling

目前我们在应用程序中使用石英计时器每天午夜执行一个方法。问题是,我们将它部署到多个服务器上,我们不希望每个服务器都解雇该作业。我们只想要一台服务器来解雇这份工作。

由于我们通过JMX公开,我们可以在其中一个主机上暂停/关闭石英计时器,但这不是理想的解决方案。

我们可以通过代码,将石英作业设置为仅在一个主机上执行吗?

以下是我们正在使用的xml -

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/util  
http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<bean id="timerJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" value="com.x.y.TestClass" />
<property name="targetMethod" value="testMethod" />
 </bean>

<!-- Cron Trigger, runs every day at 2 AM -->
 <bean id="cronTrigger"  
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="timerJob" />
<property name="cronExpression" value="$timer.chronExpression}" />

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
  <list>
    <ref bean="timerJob" />
  </list>
</property>
<property name="triggers">
  <list>
    <ref bean="cronTrigger" />
  </list>
</property>
<property name="quartzProperties">
  <util:properties>
    <prop key="org.quartz.scheduler.jmx.export">false</prop>
    <prop key="org.quartz.scheduler.jmx.objectName">
      quartz:type=QuartzScheduler,name=TestClass
    </prop>
  </util:properties>
</property>
</bean>
</beans>

2 个答案:

答案 0 :(得分:0)

您可以使用SchedulerFactoryBean的autoStartup属性和Spring PropertyPlaceholderConfigurer(context:property-placeholder ...)从JVM系统属性或类路径上的标准Java属性文件中获取标志的值。

<bean id="scheduler"
  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  ...
  <property name="autoStartup" value="${quartz.started}"/>
...
</bean>

或者,您可以使用:

<property name="autoStartup" ref="custom_factory_bean_that_reads_the_flag_from_app_cfg"/>

这显然会影响您的所有工作,因为它们只会在autoStartup = true的主机上执行。

AFAIK Quartz API本身不允许您将特定作业的执行限制在特定主机上。

答案 1 :(得分:0)

您只能为该计算机定义计划程序。调度程序将负责安排作业。