Quartz in Spring memo: part 3

这一部分记录一下如何在spring里面配置persistent schedule。

首先job class,有两种方法,一个是直接实现quartz的StatefulJob接口,另外一个还是使用spring的MethodInvokingJobDetailFactoryBean,但是要把concurrent属性设成true。

trigger和part 1里面的非persistent的保持一致。需要注意的是,如果quartz版本 >= 1.6,是不能使用spring提供的SimpleTriggerBean或者CronTriggerBean的。这一点在spring的SimpleTriggerBean和CronTriggerBean的java doc里面有提到。这时候需要使用quartz本身的SimpletTrigger或者CronTrigger。

然后就是SchedulerFactoryBean,要配置dataSource。这个dataSource就是spring里面配置的供dao层使用的dataSource。ScheduleFactoryBean配置的xml如下:

   1: <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
   2:     <property name="triggers">
   3:         <list>
   4:         </list>
   5:     </property>
   6:     <property name="dataSource" ref="dataSource"/>
   7:     <property name="configLocation" value="classpath:quartz.properties"/>
   8: </bean>

还有几个重要的步骤。

  1. 建quartz的数据库表。可以在quartz的包里面找到对应数据库的初始化sql。
  2. 在classpath下面创建一个quartz.properties文件。因为dataSource已经在spring里面配置过了,所以在quartz.properties里面不需要再重复配置。里面需要配置的是threadPool,jobStore等等。这个properties·文件需要在scheduleFactory的spring配置文件里面配置。quartz.properties sample如下:
  3.    1: org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
       2: org.quartz.threadPool.threadCount = 10
       3: org.quartz.threadPool.threadPriority = 5
       4: org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
       5:  
       6: org.quartz.jobStore.misfireThreshold = 60000
       7:  
       8: org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
       9: org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate

Related posts:

  1. Quartz in Spring memo: part 5
  2. Quartz in Spring memo: part 4
This entry was posted in java, memo and tagged , , . Bookmark the permalink.
blog comments powered by Disqus