chaojiwanwan 2011-06-10
一,首先下载quartz-1.6.0.jar架包,到lib目录下
二,写你自己定时器业务方法
package com.lbnet.lzx.timing;
import org.quartz.JobExecutionContext;
importorg.quartz.JobExecutionException;
importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;import com.lbnet.lzx.service.IOrderService;
public class OrderNoRefact extends QuartzJobBean{
privatestaticBeanFactoryfactory=newClassPathXmlApplicationContext("applicationContext-*.xml");
@Override
protectedvoidexecuteInternal(JobExecutionContextarg0)
throwsJobExecutionException{
//TODOAuto-generatedmethodstub
IOrderServiceorderdao=(IOrderService)factory.getBean("OrderServiceImpl");
System.out.println("订单超时!");
orderdao.updateOrderTimeOver();
}
}
三。在spring文件中进行配置配置定时任务
<bean id="SchedulerTask3" class="org.springframework.scheduling.quartz.JobDetailBean">
<propertyname="jobClass">
<value>com.lbnet.lzx.timing.OrderNoRefact</value>
</property>
</bean>配置定时时间
<beanid="SchedulerTaskTrigger3"class="org.springframework.scheduling.quartz.CronTriggerBean">
<propertyname="jobDetail"ref="SchedulerTask3"/>
<propertyname="cronExpression">
<value>00/300/1**?</value>
</property>
</bean>
开启定时任务<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<propertyname="triggers">
<list>
<refbean="SchedulerTaskTrigger3"/>
</list>
</property>
</bean>四。一些定时参数意义
0 0/30 0/1 * * ? 代表每天每30分钟运行一次
0 0/3 0/1 * * ? 代表每天每3分钟运行一次
0 11 23 * * ? 代表每天晚上23:11运行一次
五。如果重启报异常
org.apache.commons.collections.SetUtils.orderedSet(Ljava/util/Set;)Ljava/util/Set;
请把commons-collections-2.1.1.jar,checkstytle-all.jar删掉,然后加入commons-collections.jar包就OK了
==============================================
Spring调度任务的运行时间可通过TriggerBean的属性进行配置,一般情况下不是使用SimpleTriggerBean就是使用
CronTriggerBean,这两个TriggerBean配置说明如下:
1) SimpleTriggerBean的配置:
<bean id="trigger.jobdetail"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail"><ref bean="factory" />
</property>
<property name="startDelay">
<!-- 10 seconds 单位是毫秒 默认是10秒钟-->
<value>10000</value>
</property>
<property name="repeatInterval">
<!-- repeat every 600 seconds 单位是毫秒 默认是10分钟-->
<value>60000</value>
</property>
</bean>
定时任务所在的bean的配置
<bean id="factory"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="dailyjob.processor" />
</property>
<property name="targetMethod">
<value>process</value>
</property>
</bean>
2) CronTriggerBean的配置:
这里设置每隔一分钟执行一次。SimpleTriggerBean只能指定工作执行的频率,不能指定工作执行的具体时间。
比方说要求在每天的晚上10点执行某个具体的工作,那么SimpleTriggerBean就不能满足了,这时需要使用另外
一个bean:CronTriggerBean。
<bean id="Detail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="mainServer"/></property>
<property name="targetMethod"><value>excute</value></property>
</bean>
设置触发器
<bean id="HRTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail"> <ref bean="Detail"/></property>
<property name="cronExpression"> <value>0/10 * * * * ?</value> </property>
</bean>
<!--启动触发器-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 添加触发器 -->
<ref local="HRTrigger" />
</list>
</property>
</bean>