Posted on 2012-06-04 16:25
yuhaibo736 阅读(1473)
评论(0) 编辑 收藏
1、下载Quartz java包copy到WEB-INF/lib下
2、先写一个抽象类继承QuartzJobBean
package org.yhb.task;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
*
* @author yuhb create on 2012-6-4
*
*/
public abstract class AbstractTask extends QuartzJobBean {
protected final Log log = LogFactory.getLog(this.getClass());
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
this.execute();
}
protected abstract void execute();
}
然后写一个类继承上面的抽象类
package org.yhb.task;
public class MyTask extends AbstractTask {
@Override
protected void execute() {
System.out.println("测试定时任务!!!");
}
}
3、编写beans-quarz.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- ============================= Spring Quartz ================================ -->
<bean id="myTask" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="org.yhb.task.MyTask" />
<property name="jobDataAsMap">
<map>
<entry key="timeout">
<value>60</value>
</entry>
</map>
</property>
</bean>
<!-- 每隔2分钟触发一次-->
<bean id="myTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myTask" />
</property>
<property name="cronExpression">
<value>0 0/2 * * * ?</value>
</property>
</bean>
<!-- ######## 总调配中心 ########## -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="myTaskTrigger" />
</list>
</property>
</bean>
</beans>
4、在web.xml中进行配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-quarz.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
完成上面四项后,启动服务器就执行定时任务了。