Spring Boot timed + multi-threaded execution

Spring Boot scheduled task implemented in many ways, I perform regular tasks by way of notes in a mini-project.

Specific tasks performed by multi-threaded execution, single-threaded implementation of a 1-hour task, multithreaded 5 minutes to complete.

Improve efficiency more than 10 times, 10 times or more to enhance efficiency, improve the efficiency of more than 10 times.

The important thing to say three times!

This article is not specific insight into the principles, if we want to achieve similar functionality, only need to do the following to 1-5.

As to why it is on the map, not directly attached to the code. Directly on the code is missing typesetting, can be required to stay mailbox, I sent you.

 

(1) Timing task of achieving

   Notes @Scheduled method requires an increase in the regular implementation of the above (cron = "01 00 17 * *?") // [seconds] [minutes] [h] [day] [month] [weeks] [years].

    Wherein cron Linux similar wording execution timing of the execution of the task indicates 17:00 daily.

Internal (2) Timing task executing multi-threaded portion

    Part of the execution of multiple threads need to redefine a class, and then inject the variables of the class. As I here defines a class DoBackupTask, in this class perform tasks by multiple threads.

 

 

 (3) multi-threading to achieve some

On multi-threaded execution method, increasing the comment @Async ( "taskExecutor"), the parameter is the name of the thread pool

 

 

 (4) thread pool implementation

Thread Pool I define a class that defines a method asyncExecutor in the class. An object of the method of generating ThreadPoolTaskExecutor, setting the number of threads, thread pool parameter names.

 

 (5) When the above steps were done, however, and no effect. Maybe you missed the most critical step: on application startup class, increase annotation

@EnableScheduling enable multi-threaded
@EnableAsync asynchronous execution

If you still do not know the specific location, to find @SpringBootApplication this annotation, put together on it.

Guess you like

Origin www.cnblogs.com/siweihz/p/12127731.html