Two implementations of the Spring timer in JAVA

  • 2020-04-01 04:11:38
  • OfStack

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


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

Defined in the Spring

.

Configure the Spring timer


<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">  
<property name="timerTask" ref="reportTimerTask" />  
<property name="period">  
<value>86400000value>  
property>  
bean> 

The timerTask attribute tells ScheduledTimerTask which to run. 86400,000 represents 24 hours

Start the Spring timer

Spring's TimerFactoryBean is responsible for starting the timing task


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

ScheduledTimerTasks displays a list of timer tasks to be started.

The delay property can be set to delay startup    


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

This task can only be scheduled to run every 24 hours, and cannot be started precisely at a certain time

2. Quartz timer

First, inherit the QuartzJobBean class to implement the executeInternal method


import org.quartz.JobExecutionContext;  
import org.quartz.JobExecutionException;  
import org.springframework.scheduling.quartz.QuartzJobBean;  
public class EmailReportJob extends QuartzJobBean{  
protected void executeInternal(JobExecutionContext arg0)  
throws JobExecutionException {  
...  
}  
}

Defined in Spring


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

Instead of declaring an EmailReportJob Bean directly, we declare a JobDetailBean. This is a feature of Quartz. The JobDetailBean, a subclass of Quartz's org.quartz.jobdetail, requires that a Job object be set through the jobClass attribute.

Another special feature in JobDetail using Quartz is that the courseService property of EmailReportJob is set indirectly. The jobDataAsMap property of JobDetail accepts a Map, including the various properties set to jobClass, when. When the JobDetailBean is instantiated, it injects the courseService Bean into the courseService attribute of the EmailReportJob.

Start timer

Quartz's org.quartz.trigger class describes when and how often an Quartz job runs. Spring provides two triggers, SimpleTriggerBean and CronTriggerBean.

The SimpleTriggerBean is similar to the scheduledTimerTasks. Specify how often the work is executed, mimicking the scheduledTimerTasks configuration.


<bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
<property name="jobDetail" ref="reprotJob" />  
<property name="startDelay">  
<value>360000value>  
property>  
<property name="repeatInterval">  
  <value>86400000value>  
property>  
bean>

StartDelay is also 1 hour late to start

The CronTriggerBean specifies the exact running time of the work


<bean id="cronReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
<property name="jobDetail" ref="reprotJob" />  
<property name="cronExpression">  
<value>0 0 6 * * ?value>  
property>  
bean> 

The attribute cronExpression tells when to fire. The most mysterious is the cron expression:

The planning of Linux systems is usually done by cron. A cron expression has at least six (and possibly seven) time elements separated by Spaces. From left to right:

4. Date in month (1-31) 5. Date in month (1-12 or jan-dec)6. Date in week (1-7 or sun-sat) 7. Year (1970-2099)
Each element displays a specified value (such as 6), an interval (9-12), a list (9, 11, 13), or a wildcard (*). Because the elements 4 and 6 are mutually exclusive, you should set a question mark (?) To indicate which field you do not want to set, "/" if the combination of values represents the number of repeats (10/6 means 6 repeats every 10 seconds).

Start timer


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

The triggers property accepts a set of triggers.

Well, this is the end of the content of this article, write a good bar, there are shortcomings, welcome each warrior to put forward valuable advice.


Related articles: