package  QuartzTest;

import  java.util.Date;

public   class  CourseService  {
   
public void start(){
       System.out.println(
new Date().getSeconds());
   }

}

编写调度类,需要继承QuartzJobBean :
 
package  QuartzTest;

import  org.quartz.JobExecutionContext;
import  org.quartz.JobExecutionException;
import  org.springframework.scheduling.quartz.QuartzJobBean;


public   class  QuartzJob  extends  QuartzJobBean  {    
    
    
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        courseService.start();
    }


    
private CourseService courseService;

    
public CourseService getCourseService() {
        
return courseService;
    }


    
public void setCourseService(CourseService courseService) {
        
this.courseService = courseService;
    }

    

}

 
 编写配置文件
需要说明的是,我们有两种trigger,分别是simple和cron模式,simple方式和timertask类似,采用设置interval方式进行调度,而cron可以特有的语法很详细的定制调度执行时间,具体描述在配置文件的注释中
 
xml version="1.0" encoding="UTF-8" ?>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"  >
< beans >
   
< bean  id ="courseService"  class ="QuartzTest.CourseService" />
   

   
< bean  id ="reportJbo"  class ="org.springframework.scheduling.quartz.JobDetailBean" >
     
< property  name ="jobClass" >
       
< value > QuartzTest.QuartzJob value >
     
property >
     
< property  name ="jobDataAsMap" >
       
< map >
        
         
< entry  key ="courseService" >
           
< ref  bean ="courseService" />
          
entry >
       
map >
     
property >
   
bean >

   

   

   
< bean  id ="simpleReportTrigger"  class ="org.springframework.scheduling.quartz.SimpleTriggerBean" >
      
< property  name ="jobDetail" >
        
< ref  bean ="reportJbo" />
      
property >
      
< property  name ="repeatInterval" >
        
< value > 1000 value >
      
property >
   
bean >
 
   

   
< bean  id ="cronReportTrigger"  class ="org.springframework.scheduling.quartz.CronTriggerBean" >
      
< property  name ="jobDetail" >
        
< ref  bean ="reportJbo" />
      
property >
      
< property  name ="cronExpression" >
        
< value > 02 20 21 7 6 ? 2007 value >  
        

      
property >
   
bean >
 
   

   
< bean  id ="start"  class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
     
< property  name ="triggers" >
       
< list >
         
< ref  bean ="cronReportTrigger" />
       
list >
     
property >
   
bean >
beans >

Spring还为我们提供了更简单的加载调度的方式,也就说我们在已经有业务方法CourseService时不需要再额外编写调度类QuartzJob,可以直接配置service的方法
 
    
     
    

    
        start