Spring的scheduling.quartz包中对Quartz框架进行了封装,使得开发时不用写任何QuartSpring的代码就可以实现定时任务。Spring通过JobDetailBean,MethodInvokingJobDetailFactoryBean实现Job的定义。后者更加实用,只需指定要运行的类,和该类中要运行的方法即可,Spring将自动生成符合Quartz要求的JobDetail。
在上一篇文章《Quartz 框架快速入门(三)》中我们将示例迁移到Web环境下了,但使用的是Quartz的启动机制,这一篇中我们将让Web服务器启动Spring,通过Spring的配置文件来进行任务的调度
1,创建一个Web项目,加入spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar ,commons-logging.jar这几个包.
2,创建一个类,在类中添加一个方法execute,我们将对这个方法进行定时调度.
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 Log logger = LogFactory.getLog(HelloWorld. class ); // 日志记录器
public HelloWorld()
{
}
public void execute()
{
logger.info( " Kick your ass and Fuck your mother! - " + new Date());
}
}
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 Log logger = LogFactory.getLog(HelloWorld. class ); // 日志记录器
public HelloWorld()
{
}
public void execute()
{
logger.info( " Kick your ass and Fuck your mother! - " + new Date());
}
}
2. Spring配置文件applicationContext.xml 修改如下:
<?
xml version="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/beans http://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 >
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://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的配置文件就可以了,代码如下:
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( " Test start. " );
ApplicationContext context = new ClassPathXmlApplicationContext( " applicationContext.xml " );
// 如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
// context.getBean("startQuertz");
System.out.print( " Test end.. " );
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
System.out.println( " Test start. " );
ApplicationContext context = new ClassPathXmlApplicationContext( " applicationContext.xml " );
// 如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
// context.getBean("startQuertz");
System.out.print( " Test end.. " );
}
}
4,然后将Web.xml修改如下,让tomcat在启动时去初始化Spring:
<?
xml version="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 >
< 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,测试结果如下图所示: