使用石英触发器调度类/ bean?

时间:2014-08-20 11:14:03

标签: java spring quartz-scheduler

我正在使用Spring3。我有一个需要安排的bean。我的应用程序生成Jar而非war。我是否需要在tomcat中部署jar来触发这项工作?

谢谢!

1 个答案:

答案 0 :(得分:0)

绝对不是。如果您使用Quartz来安排和执行您的工作,那么您也可以在独立的Java应用程序中使用它。在你的Spring配置中你应该有这样的东西(假设你正在使用JDBC作业存储来保存你的工作和触发器):

<!--
  QuartzDesk scheduler.
-->
<bean id="scheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

  <property name="schedulerName" value="YourSchedulerName"/>

  <property name="autoStartup" value="true"/>

  <property name="waitForJobsToCompleteOnShutdown" value="true"/>

  <property name="dataSource" ref="dataSource"/>

  <property name="overwriteExistingJobs" value="true"/>

  <property name="quartzProperties">
    <props>
      <prop key="org.quartz.scheduler.instanceId">${scheduler.org.quartz.scheduler.instanceId}</prop>
      <prop key="org.quartz.scheduler.instanceIdGenerator.class">${scheduler.org.quartz.scheduler.instanceIdGenerator.class}</prop>
      <prop key="org.quartz.jobStore.driverDelegateClass">${scheduler.org.quartz.jobStore.driverDelegateClass}</prop>

      <prop key="org.quartz.jobStore.tablePrefix">${scheduler.org.quartz.jobStore.tablePrefix}</prop>
      <prop key="org.quartz.jobStore.isClustered">${scheduler.org.quartz.jobStore.isClustered}</prop>
      <prop key="org.quartz.jobStore.selectWithLockSQL">${scheduler.org.quartz.jobStore.selectWithLockSQL}</prop>
      <prop key="org.quartz.jobStore.lockHandler.class">${scheduler.org.quartz.jobStore.lockHandler.class}</prop>

      <!--
        The "use properties" flag instructs JDBCJobStore that all values in JobDataMaps will be Strings, and therefore can be stored as name-value pairs, rather than storing more complex objects in their serialized form in the BLOB column. This is can be handy, as you avoid the class versioning issues that can arise from serializing your non-String classes into a BLOB.
      -->
      <prop key="org.quartz.jobStore.useProperties">true</prop>

      <!--
        The the number of milliseconds the scheduler will 'tolerate' a trigger to pass its next-fire-time by, before being considered "misfired". The default value (if you don't make an entry of this property in your configuration) is 60000 (60 seconds).
      -->
      <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>

      <!--
        Configures Quartz to expose the scheduler through an MBean in the JMX MBeanServer.
      -->
      <prop key="org.quartz.scheduler.jmx.export">true</prop>

      <!--
        The scheduler thread will be marked as a daemon thread.
      -->
      <prop key="org.quartz.scheduler.makeSchedulerThreadDaemon">true</prop>
      <!--
        The scheduler thread pool threads will be marked as daemon threads.
      -->
      <prop key="org.quartz.threadPool.makeThreadsDaemons">true</prop>
    </props>
  </property>

  <property name="jobFactory">
    <bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory"/>
  </property>

  <property name="jobDetails" ref="jobDetails"/>

  <property name="triggers" ref="jobTriggers"/>

  <!--
    Name of the property in the scheduler context the Spring application context
    will be exposed through.
  -->
  <property name="applicationContextSchedulerContextKey" value="applicationContext"/>
</bean>

<!--
  jobDetails list contains all jobs that will be added to the QuartzDesk scheduler.
-->
<bean id="jobDetails" class="java.util.ArrayList">
  <constructor-arg>
    <list>
      <ref bean="yourJob1"/>
    </list>
  </constructor-arg>
</bean>

<!--
  jobTriggers list contains all triggers that will be added to the QuartzDesk scheduler.
-->
<bean id="jobTriggers" class="java.util.ArrayList">
  <constructor-arg>
    <list>
      <ref bean="yourTrigger1"/>
      ...
    </list>
  </constructor-arg>
</bean>


<bean id="yourJob1" class="org.quartz.impl.JobDetailImpl">
  <property name="jobClass" value="your job fqcn"/>
  <property name="group" value="someJobGroup"/>
  <property name="name" value="YourJob1"/>

  <property name="description"
            value="Job that does this and that..."/>

  <property name="durability" value="true"/>

  <property name="jobDataMap">
    <bean class="org.quartz.JobDataMap">
        <constructor-arg>
          <map>
            <entry key="param1" value="value1"/>
            ...
          </map>
        </constructor-arg>
    </bean>
  </property>
</bean>


<bean id="yourTrigger1"
      class="org.quartz.impl.triggers.CronTriggerImpl">
  <property name="name" value="YourTrigger1"/>
  <property name="group" value="someTriggerGroup"/>
  <property name="jobName" value="YourJob1"/>
  <property name="jobGroup" value="someJobGroup"/>
  <property name="description" value="CRON trigger."/>
  <!-- ss mm hh day-of-month month day-of-week year -->
  <property name="cronExpression" value="some cron trigger expression"/>
</bean>

这将创建一个Quartz调度程序bean,其中包含指定的作业和触发器列表。应用程序启动时,调度程序将自动启动。

您会注意到上述应用上下文代码段对某些值使用属性占位符。您需要使用有效值扩展这些占位符。有关详细信息,请参阅Quartz文档。在大多数情况下,您可以使用默认值。