Quartz 框架快速入门(四)

本文介绍如何在Spring框架中整合Quartz实现定时任务调度。通过配置Spring Bean定义Job和触发器,无需编写额外的Quartz代码。文章提供了一个完整的示例,包括配置文件、Web.xml设置及测试结果。
Springscheduling.quartz 包中对 Quartz 框架进行了封装,使得开发时不用写任何 QuartSpring 的代码就可以实现 定时任务。 Spring 通过 JobDetailBeanMethodInvokingJobDetailFactoryBean 实现 Job 的定义。后者更加实用,只需指定要运行的类,和该类中要运行的方法即可, Spring 将自动生成符合 Quartz 要求的 JobDetail

在上一篇文章《Quartz 框架快速入门(三)》中我们将示例迁移到Web环境下了,但使用的是Quartz的启动机制,这一篇中我们将让Web服务器启动Spring,通过Spring的配置文件来进行任务的调度

1,创建一个Web项目,加入spring.jarquartz-1.6.0.jarcommons-collections.jarjta.jarcommons-logging.jar这几个包.

2,创建一个类,在类中添加一个方法execute,我们将对这个方法进行定时调度.

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package com.vista.quartz;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloWorld
{
private static Loglogger = LogFactory.getLog(HelloWorld. class ); // 日志记录器
public HelloWorld()
{
}
public void execute()
{
logger.info(
" KickyourassandFuckyourmother!- " + new Date());
}
}

2. Spring配置文件applicationContext.xml修改如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <? xmlversion="1.0"encoding="UTF-8" ?>
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >

<!-- 要调用的工作类 -->
< bean id ="quartzJob" class ="com.vista.quartz.HelloWorld" ></ bean >
<!-- 定义调用对象和调用对象的方法 -->
< bean id ="jobtask" class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<!-- 调用的类 -->
< property name ="targetObject" >
< ref bean ="quartzJob" />
</ property >
<!-- 调用类中的方法 -->
< property name ="targetMethod" >
< value > execute </ value >
</ property >
</ bean >
<!-- 定义触发时间 -->
< bean id ="doTime" class ="org.springframework.scheduling.quartz.CronTriggerBean" >
< property name ="jobDetail" >
< ref bean ="jobtask" />
</ property >
<!-- cron表达式 -->
< property name ="cronExpression" >
< value > 10,15,20,25,30,35,40,45,50,55****? </ value >
</ property >
</ bean >
<!-- 总管理类如果将lazy-init='false'那么容器启动就会执行调度程序 -->
< bean id ="startQuertz" lazy-init ="false" autowire ="no" class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
< property name ="triggers" >
< list >
< ref bean ="doTime" />
</ list >
</ property >
</ bean >
</ beans >

3,先在控制台中对上面的代码进行测试,我们要做的只是加载Spring的配置文件就可以了,代码如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package com.vista.quartz;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test
{
public static void main(String[]args)
{
System.out.println( " Teststart. " );
ApplicationContextcontext
= new ClassPathXmlApplicationContext( " applicationContext.xml " );
// 如果配置文件中将startQuertzbean的lazy-init设置为false则不用实例化
// context.getBean("startQuertz");
System.out.print( " Testend.. " );
}
}

4,然后将Web.xml修改如下,让tomcat在启动时去初始化Spring

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <? xmlversion="1.0"encoding="UTF-8" ?>
< web-app version ="2.4"
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value >
/WEB-INF/classes/applicationContext.xml
</ param-value >
</ context-param >

< servlet >
< servlet-name > SpringContextServlet </ servlet-name >
< servlet-class > org.springframework.web.context.ContextLoaderServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >

< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >

5,最后启动Tomcat,测试结果如下图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值