quartz主要是三個部分:Scheduler Job Trigger,其中,Job 負(fù)責(zé)定義需要執(zhí)行的任務(wù),Trigger 負(fù)責(zé)設(shè)置調(diào)度策略,Scheduler 將二者組裝在一起,并觸發(fā)任務(wù)開始執(zhí)行。Scheduler 可以通過SchedulerFactory來生成.
用spring封裝后的quartz更簡單,下面是一個具體的配置例子:
在其中job,用targetObject來指定任務(wù)類,可以
是任意的spring
bean,targetMethod指定任務(wù)執(zhí)行的方法.Tigger一般用兩種,CronTriggerBean和
SimpleTriggerBean,一個triggerBean中可以配置多個job,一個Scheduler中可以配置多個trigger.
-
- <bean id="siteSearchJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject">
- <ref bean="websiteSearchEngine"/>
- </property>
- <property name="targetMethod">
- <value>runWebsiteSearcher</value>
- </property>
- </bean>
-
- <bean id="siteSearchSimpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="siteSearchJobDetail"/>
- </property>
-
-
- <property name="cronExpression" value="* * * */15 * ?" />
- </bean>
-
- <bean id="siteSearchSfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="siteSearchSimpleTrigger"/>
- </list>
- </property>
-
- <property name="autoStartup">
- <value>false</value>
- </property>
-
- <property name="quartzProperties">
- <props>
- <prop key="org.quartz.scheduler.instanceName">DefaultQuartzScheduler</prop>
- <prop key="org.quartz.scheduler.rmi.export">false</prop>
- <prop key="org.quartz.scheduler.rmi.proxy">false</prop>
- <prop key="org.quartz.scheduler.wrapJobExecutionInUserTransaction">false</prop>
- <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
- <prop key="org.quartz.threadPool.threadCount">1</prop>
- <prop key="org.quartz.threadPool.threadPriority">5</prop>
- <prop key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">true</prop>
- <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
- <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
- </props>
- </property>
- </bean>
-
<!-- quartz定時調(diào)度執(zhí)行 網(wǎng)站自動發(fā)現(xiàn) begin -->
<bean id="siteSearchJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="websiteSearchEngine"/>
</property>
<property name="targetMethod">
<value>runWebsiteSearcher</value>
</property>
</bean>
<bean id="siteSearchSimpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="siteSearchJobDetail"/>
</property>
<!--每15天執(zhí)行一次 -->
<property name="cronExpression" value="* * * */15 * ?" />
</bean>
<bean id="siteSearchSfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="siteSearchSimpleTrigger"/>
</list>
</property>
<property name="autoStartup">
<value>false</value>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceName">DefaultQuartzScheduler</prop>
<prop key="org.quartz.scheduler.rmi.export">false</prop>
<prop key="org.quartz.scheduler.rmi.proxy">false</prop>
<prop key="org.quartz.scheduler.wrapJobExecutionInUserTransaction">false</prop>
<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
<prop key="org.quartz.threadPool.threadCount">1</prop>
<prop key="org.quartz.threadPool.threadPriority">5</prop>
<prop key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">true</prop>
<prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
<prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
</props>
</property>
</bean>
<!-- quartz定時調(diào)度執(zhí)行 網(wǎng)站自動發(fā)現(xiàn)end -->
下面是一個simpleTigger的例子:
- <bean id="mergeSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
- <property name="jobDetail">
- <ref bean="mergeJobDetail"/>
- </property>
- <property name="startDelay">
- <!--5*1000毫秒-->
- <value>5000</value>
- </property>
- <property name="repeatInterval">
- <value>300000</value>
- </property>
- </bean>
|