Spring Task Timing Task

Keywords: Spring Java xml

The Spring framework has its own Task Executor and Task Scheduler interfaces.

Task task scheduling configuration: spring-tasks.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="     
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd   
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd   
        default-lazy-init=">    
 
         <context:annotation-config></context:annotation-config>  
        <!--  spring Scanning Annotation Configuration   -->  
        <context:component-scan base-package="com.levi.test.task"></context:component-scan>
               
       <!--Turn on this configuration. spring Ability to recognize@Scheduled annotation   -->  
       <task:annotation-driven mode="proxy" scheduler="testScheduler"></task:annotation-driven>  
       <task:scheduler id="testScheduler" pool-size="10"></task:scheduler>  
 
</beans>

Task Scheduling Implementation Code: TestTask.java

package com.levi.test.task;
 
import java.text.SimpleDateFormat;;
import java.util.Calendar;
import java.util.Date;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
@Component("TestTask")
public class TestTask {
     
    //Execution at 4:40 a.m. every day
    @Scheduled(cron = "0 40 4 * * ?")
    public void TestTask() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    Date factInPlaceDate = new Date();
 
        Calendar beforeCd = Calendar.getInstance();  
    beforeCd.setTime(factInPlaceDate);  
    beforeCd.add(Calendar.MONTH, -1);  
    Date beforeFactDate = beforeCd.getTime(); 
    String beforeMonthDate = sdf.format(beforeFactDate);
    System.out.println("The actual arrival date is reduced by one month:" + beforeMonthDate);
         
    Calendar afterCd = Calendar.getInstance();  
    afterCd.setTime(factInPlaceDate);  
    afterCd.add(Calendar.MONTH, +1);  
    Date afterFactDate = afterCd.getTime(); 
    String afterMonthDate = sdf.format(afterFactDate);
    System.out.println("Actual arrival date plus one month:" + afterMonthDate);
   }  
}
In spring The cronin expression of seven parameters is not supported in 4.x, which requires six parameters. The cron expression is in the following format:

{second} {minute} {time} {date (specific day)} {month} {week}
  • Seconds: Mandatory, the allowable range of values is 0-59. Special symbols supported include
     
    
    ,
    -*/ It means that a task will be triggered in a specific second, - that it will be triggered in a period of time, * that it will be triggered in every second, / that it will be triggered at any time, and that it will be triggered at any interval of time.
  • Points: Mandatory items, the allowable range of values is 0-59, supporting special symbols and seconds, meaning analogy
  • When: mandatory, the allowable range of values is 0-23, supporting special symbols and seconds, meaning analogy
  • Date: Must be filled in. The allowable range of values is 1-31. The special symbols supported are more than seconds?, which means that they are mutually exclusive with {week}, meaning that {date} is meaningless if {week} is explicitly specified to trigger, so as to avoid conflict and confusion.
  • Month: Mandatory. The allowable range of values is 1-12 (JAN-DEC). Special symbols supported are the same as seconds, meaning analogy
  • Week: Must be filled in, the allowable range is 1-7 (SUN-SAT), 1 represents Sunday (the first day of a week), and so on, 7 represents Saturday, supporting symbols are more than seconds?, the meaning is mutually exclusive with {date}, that is, if the {date} trigger is specified explicitly, it means {week} is meaningless.

For example, the following cron expression:

// The meaning of the expression is to trigger a task every half minute.
30 * * * * ?

Posted by Crackhead on Sun, 07 Jul 2019 14:19:32 -0700