Two implementations of Spring timers

There are two popular Spring timer configurations: Java's Timer class and OpenSymphony's Quartz.

1. Java Timer timing

First inherit the java.util.TimerTask class to implement the run method

[java] view plaincopyprint?
import java.util.TimerTask;  
public class EmailReportTask extends TimerTask{       
@Override    
    public void run() { }      
}    



The timerTask property tells ScheduledTimerTask to run which. 86400000 represents 24 hours

. Start Spring Timer

Spring's TimerFactoryBean is responsible for starting timing tasks

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">  
<property name="scheduledTimerTasks">  
   <list><ref bean="scheduleReportTask" />list>  
property>  
beans>  

You can delay the startup by setting the delay property  
<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">  
<property name="timerTask" ref="reportTimerTask" />  
<property name="period">  
<value>86400000value>  
property>  
<property name="delay">  
<value>3600000value>  
property>  
bean>  
We can only specify that this task will run once every 24 hours and cannot be started exactly at a certain time.

2.Quartz timer


First write Service class:

[java] view plaincopyprint?
<span style="color:#000000;"></span><span style="color:#00ff;">import</span><span style="color:# 000000;">java.util.Date;   
 
</span><span style="color:#00ff;">public</span><span style="color:#000000;"> </span><span style="color:#00ff;">class</span><span style="color:#000000;"> CourseService </span><span id="_72_152_Open_Text"><span style="color:#000000;">{ 
   </span><span style="color:#00ff;">public</span><span style="color:#000000;"> </span><span style="color:#00ff;">void</span><span style="color:#000000;"> start()</span><span id="_96_150_Open_Text"><span style="color:#000000;">{ 
       System.out.println(</span><span style="color:#00ff;">new</span><span style="color:#000000;"> Date().getSeconds()); 
   }</span></span><span style="color:#000000;"> 
}</span></span><span style="color:#000000;"> 
</span> 


编写调度类,需要继承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;
    }
   

}




编写配置文件
It should be noted that we have two types of triggers, namely simple and cron modes. The simple mode is similar to timertask, and the interval is set for scheduling, while cron can customize the scheduling execution time in detail with its unique syntax, which is described in the configuration file. <? 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"/>
   <!-- Create scheduling tasks using a separate scheduling class QuartzJob -->
   <bean id="reportJbo " class="org.springframework.scheduling.quartz.JobDetailBean">
     <property name="jobClass">
       <value>QuartzTest.QuartzJob</value>
     </property>
     <property name=" jobDataAsMap">
       <map>
         <!--Use jobDataAsMap for courseService injection-->
         <entry key="courseService">
           <ref bean="courseService"/>
          </entry>
       </map>
     </property>
   </bean>

   <!-- Create scheduling tasks using existing service class methods, no need Write scheduling class QuartzJob separately
   <bean id="reportJbo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     <property name="targetObject">
      <ref bean="courseService"/>
     </property>
     <property name ="targetMethod">
        <value>start</value>
     </property>
   </bean>
    -->
   <!-- Configure scheduling tasks, simple mode -->
   <bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
      <property name="jobDetail">
        <ref bean="reportJbo"/>
      </property>
      <property name="repeatInterval">
        <value>1000</value>
      </property>
   </bean>

   <!-- configuration Scheduling tasks, complex custom modes, the day of the month and the week cannot be set at the same time -->
   <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. Seconds 0-59
             2. Minutes 0-59
             3. Hours 0-23
             4. Day of the month 1-31
             5. Month 1-12 or Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
             6. Mid-week date 1-7 or MON,TUE,WED,THU,FRI,SAT,SUN.
        -->
      </property>
   </bean>

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




Spring also provides us with a simpler way to load scheduling, which means that we don't need to write additionally when we already have a business method CourseService The scheduling class QuartzJob can directly configure the method of service

[java] view plaincopyprint?
<bean id="reportJbo" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
     <property name="targetObject"> 
      <ref bean="courseService"/> 
     </property> 
     <property name="targetMethod"> 
        <value>start</value> 
     </property> 
   </bean> 
<!-- ============== start=============== -->
<bean id="findAppSaveTimeEnd" class="com.skyon.psbc.cas.job.app.FindAppSaveTimeEnd" lazy-init="false" singleton="false" >
<property name="appTaskService" ref="appTaskService"></property>
</bean>
<bean id="findAppTimeEnd" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
        <ref bean="findAppSaveTimeEnd"/>
    </property>
    <property name="targetMethod">
        <value>executeJob</value>
    </property>
</bean>
<bean id="findAppTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
        <ref bean="findAppTimeEnd"/>
    </property>
    <property name="cronExpression">
        <value>0 0 0 * * ?</value>
    </property>
</bean>
<!-- ============== end=============== -->

<!-- === If the total management class will be lazy-init ='false'Then the container will start the scheduler =========== -->
<bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
    <ref bean="findAppTime"/>
</list>
</property>
</bean>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326975561&siteId=291194637