Quartz 框架快速入门(四)

本文详细介绍了如何在Spring框架下利用Quartz实现定时任务,包括项目搭建、配置文件调整、Web环境集成及启动流程,旨在简化定时任务的开发过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Quartz 框架快速入门(四)

     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.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()); 
    }
}

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 >

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.. " );
    }
}

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 >

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

作者:洞庭散人

出处:http://phinecos.cnblogs.com/     

本博客遵从Creative Commons Attribution 3.0 License ,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值