Quartz is used in Spring to implement task scheduling

  • 2020-06-07 04:32:03
  • OfStack

Used in the preface: Spring Quartz there are two ways, 1 kind is inheritance specific base class: org. springframework. scheduling. quartz. QuartzJobBean, another one is not required, (it is recommended to use the second). These are described below.

1, the working class inheritance org. springframework. scheduling. quartz. QuartzJobBean

Step 1: Define the job class

java code


import java.text.SimpleDateFormat;
import java.util.Date;

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

public class Job1 extends QuartzJobBean{
  
  // The value of this parameter is given by xml Configuration sent over 
  private int timeout;
  
  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }

  @Override
  protected void executeInternal(JobExecutionContext content) throws JobExecutionException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.format(new Date()) + "job1 perform " + " This is a xml to timeout The assignment " + timeout);
  }
}

Step 2 Configure JobDetailBean in spring

spring. xml configuration code


<bean id = "job1" class="org.springframework.scheduling.quartz.JobDetailBean">
   <!--  Here I'm pointing to the homework class  -->
   <property name="jobClass" value="com.ccg.job.Job1" />
   <property name="jobDataAsMap">
     <map>
       <!--  Write parameters here that can be passed to the parameters defined in the job class  --> 
       <entry key="timeout" value="10"></entry>
     </map>
   </property>
 </bean>

Step 3 Configure the triggering mode

There are two types of job triggers for Quartz, respectively

org. springframework. scheduling. quartz. SimpleTriggerBean, according to the frequency of 1 set to perform a task

org. springframework. scheduling. quartz. CronTriggerBean, support cron expression, you can specify a time, can also according to the frequency of execution

The first SimpleTriggerBean, for example, executes every two seconds, is configured as follows:


<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 
   <property name="jobDetail" ref="job1" /> 
   <property name="startDelay" value="10000" /><!-- The dispatch factory is instantiated after passing through 10 The schedule starts in seconds -->
   <property name="repeatInterval" value="2000" /><!-- every 2 Second scheduling 1 time -->
 </bean>

The second CronTriggerBean, for example, is executed at 12 o 'clock every day. The xml configuration is as follows:


<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
   <property name="jobDetail" ref="job1" />
   <property name="cronExpression" value="0 0 12 * * ?" /> <!--  Every day, 12 Day to Day mission  -->
 </bean>

The Cron expression format is introduced last.

Step 4 Configure the scheduling factory

spring. xml configuration code is as follows:


<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
    <list>
    <ref bean="simpleTrigger"/>
  <!-- <ref bean="cronTrigger"/> -->
    </list>
  </property>
</bean>

Step 5 Starts the application to see the execution of the task scheduling.

2. The job class does not need inheritance. It is just a normal java class

The main class is org springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean, below is the code:

Step 1 Job class


import java.text.SimpleDateFormat;
import java.util.Date;

public class Job2 {
  
  public void run(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.format(new Date()) + " Here is the job2 The implementation of the ");
  }
}

Step 2 Configure job2 in ES90en.es91EN


<bean id = "job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" >
  <bean class="com.ccg.job.Job2" />
  </property>
  <property name="targetMethod" value="run"></property>
  <property name="concurrent" value="false" /><!--  Jobs are not concurrent scheduled  --> 
</bean>

The targetMethod execution job class points to the methods to be executed in the job class

Step 3 Configure the triggering mode, again there are two SimpleTrggerBean and one CronTrggerBean

The first configuration, xml, is as follows :(executes every 2 seconds)


<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 
   <property name="jobDetail" ref="job2" /> 
   <property name="startDelay" value="10000" />
   <property name="repeatInterval" value="2000" />
 </bean>

The second configuration, xml, is as follows :(executed daily at 12:00)


<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
   <property name="jobDetail" ref="job2" /> 
   <property name="cronExpression" value="0 0 12 * * ?" /> 
 </bean>

Step 4 Configure the scheduling factory


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

To use CronTriggerBean you need to replace simpleTrigger with simpleTrigger

Finally, start the service to see the execution of the task schedule.

Attachment: Cron expression

The Cron expression is a string separated by 5 or 6 Spaces and divided into 6 or 7 fields, with each field representing a meaning. Cron has two syntax formats:


<bean id = "job1" class="org.springframework.scheduling.quartz.JobDetailBean">
   <!--  Here I'm pointing to the homework class  -->
   <property name="jobClass" value="com.ccg.job.Job1" />
   <property name="jobDataAsMap">
     <map>
       <!--  Write parameters here that can be passed to the parameters defined in the job class  --> 
       <entry key="timeout" value="10"></entry>
     </map>
   </property>
 </bean>
0

The characters that can appear in each field are as follows:

Seconds: Can appear ", - * /" four characters, valid range of 0-59 integers Minutes: Can appear ", - * /" four characters, valid range of 0-59 integers Hours: Can appear ", - * /"4 characters, valid range of 0-23 integers DayofMonth: can appear ", - * / ? L W C"8 characters, integer valid from 0 to 31 Month: Can appear ", - * /"4 characters, valid in integer range of 1-12 or ES143en-ES144en DayofWeek: can appear ", - * / ? L C #"4 characters, valid between integers 1-7 or both ES148en-ES149en. 1 means Sunday, 2 means Monday, and so on Year: can appear with 4 characters ", - * /", valid from 1970 to 2099

Related articles: