Spring定时器的两种实现方式

 

有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz。

1.Java Timer定时 java

首先继承java.util.TimerTask类实现run方法 spring

import java.util.TimerTask; 
public class EmailReportTask extends TimerTask{      
@Override   
    public void run() {    }     
}


 

timerTask属性告诉ScheduledTimerTask运行哪一个。86400000表明24个小时 ide

启动Spring定时器 this

Spring的TimerFactoryBean负责启动定时任务 spa

 
  1. <bean class="org.springframework.scheduling.timer.TimerFactoryBean">   
  2. <property name="scheduledTimerTasks">   
  3.    <list><ref bean="scheduleReportTask"/>list>   
  4. property>   
  5. bean>   
  6. scheduledTimerTasks里显示一个须要启动的定时器任务的列表。   
  7. 能够经过设置delay属性延迟启动   
  8. <bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">   
  9. <property name="timerTask" ref="reportTimerTask" />   
  10. <property name="period">   
  11. <value>86400000value>   
  12. property>   
  13. <property name="delay">   
  14. <value>3600000value>   
  15. property>   
  16. bean>   

这个任务咱们只能规定每隔24小时运行一次,没法精确到某时启动
code


2.Quartz定时器xml

首先编写服务类:继承

 

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

 

编写调度类,须要继承QuartzJobBean :get

 

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;
    }

    

}

io

 

 

 编写配置文件

须要说明的是,咱们有两种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"/>
   
<!-- 建立调度任务 使用单独编写的调度类QuartzJob    -->
   
<bean id="reportJbo" class="org.springframework.scheduling.quartz.JobDetailBean">
     
<property name="jobClass">
       
<value>QuartzTest.QuartzJob</value>
     
</property>
     
<property name="jobDataAsMap">
       
<map>

         <!--采用jobDataAsMap方式进行courseService注入-->
         
< entry  key ="courseService" >
           
< ref  bean ="courseService" />
          
</ entry >
       
</ map >
     
</ property >
   
</ bean >

   
<!--  建立调度任务 使用已有的service类方法,不须要单独编写调度类QuartzJob 
   <bean id="reportJbo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     <property name="targetObject">
      <ref bean="courseService"/>
     </property>
     <property name="targetMethod">
        <value>start</value>
     </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 >  
        
<!--  1.秒 0-59
             2.分钟 0-59
             3.小时 0-23
             4.月份中的日期 1-31
             5.月份 1-12或者Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
             6.星期中的日期 1-7或者MON,TUE,WED,THU,FRI,SAT,SUN.
        
-->
      
</ 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的方法

 

<bean id="reportJbo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     <property name="targetObject">
      <ref bean="courseService"/>
     </property>
     <property name="targetMethod">
        <value>start</value>
     </property>
   </bean>
相关文章
相关标签/搜索