SpringBoot regular tasks chapter

A. Annotation-based @Scheduled default is single-threaded, when open multiple tasks, opportunities to perform tasks affected by a task execution time.

1, create a timer

Use SpringBoot to create a scheduled task-based annotation is very simple, just a few lines of code to complete.  code show as below:

Copy the code
@Component 
@Configuration //. 1. The main configuration classes for marking, the effect of both the Component. 
@EnableScheduling // 2. opening timing task 
public class SaticScheduleTask { 
    // add the scheduled task. 3. 
    @Scheduled (the cron = "* * * * 0/5?") 
    // or directly specified time interval, for example: 5 seconds 
    // @Scheduled (fixedRate = 5000) 
    Private void configureTasks () { 
        System.err.println ( "time to perform static scheduled tasks:" + LocalDateTime.now ()); 
    } 
}
Copy the code

 

Cron expression argument, respectively:

  • Sec (0 to 59), for example, every 5 seconds represents 0/5
  • Min (0 - 59)
  • When (0 to 23)
  • Day day (0 to 31), to be calculated
  • Month (0 to 11)
  • Of the week (1-7 may fill or SUN / MON / TUE / WED / THU / FRI / SAT)

@Scheduled: In addition to supporting the flexible cron parameter expression, but also supports simple delay operation, e.g. fixedDelay, fixedRate milliseconds to fill in the appropriate.

 

2, start the test

Start the application, you can see the console print out the following information:

Here Insert Picture Description 

Obviously, using @Scheduled comment is very convenient, but the drawback is that when we adjusted the execution period of time, you need to restart the application to take effect, which is somewhat inconvenient. To be effective with immediate effect, you can use the interface to complete the scheduled task.

 

II. Annotation-based multi-threaded regular tasks set

1, create multi-threaded task timer

Copy the code
// @ Component annotation neutral compared to those for class annotated; 
// opposite in the persistence layer, control layer and service layer, respectively @ Repository, @ Service and @Controller hierarchical classes annotated 
@Component 
@ EnableScheduling // 1. open the timer task 
@EnableAsync // 2. open multithreaded 
public class MultithreadScheduleTask { 

        @Async 
        @Scheduled (FIXEDDELAY = 1000) // 1 second intervals 
        public void First () throws InterruptedException { 
            System.out.println ( " The first task begins timing: "+ LocalDateTime.now () toLocalTime ( ) +." \ r \ n threads:. "+ Thread.currentThread () getName ()); 
            System.out.println (); 
            the Thread.sleep (1000 * 10); 
        } 

        @Async 
        @Scheduled (FIXEDDELAY = 2000) 
        public void SECOND () {
            System.out.println ( "Start the second timer task:" + LocalDateTime.now () toLocalTime ( ) + "\ r \ n threads:" + Thread.currentThread () getName ()..); 
            The System.out. the println (); 
        } 
    }
Copy the code

Note:  Here's @Async notes is critical

 

2, start the test

After starting the application, view the console:
Here Insert Picture Description 

As can be seen from the console, a first and a second timed task timed tasks independently of each other;

Also, since opened a multi-threaded execution time of a task does not limit itself by its execution time, it should be noted that the operation could lead to duplication of data anomalies.

 

Reference Taken: https: //www.cnblogs.com/mmzs/p/10161936.html

Guess you like

Origin www.cnblogs.com/cxscode/p/11847350.html