Make writing a habit together! This is the 14th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.
Bai Di zhao ci clouds, thousands of miles jiangling a day also.
1 introduction
In Springboot, asynchronous tasks and scheduled tasks are common troubleshooting modes. To make full use of these two configurations and not interfere with normal services, you need to configure them asynchronously. How to set up a reasonable asynchronous processing thread is its core and key.
2 Asynchronous task Settings
Asynchronous tasks are often used in projects to solve problems. In order to reduce the waiting time for requests, asynchronous background processing can improve user experience and increase system throughput.
Starting asynchronous tasks in SpringBoot is also very simple, requiring only two steps:
- 1 open
@EnableAsync
Annotation. - 2 Add to the method that requires asynchronous processing
@Async
.
It is important to note that asynchronously executed methods can have a return value, but it must be a Future, here and multithreaded submit way to submit the task, get the result of the processing method.
At this point, the configuration and use of asynchronous tasks is complete. Asynchronous tasks also use multi-threaded thread pools, which are used by defaultSimpleAsyncTaskExecutor
How can asynchronous tasks be implemented using custom thread pools? This is a good question, and there is an answer to itAsyncConfigurer
. This is an interface that needs to be implementedgetAsyncExecutor
Method to obtain a thread pool, if the need to fetch exception information, in the implementation of a methodgetAsyncUncaughtExceptionHandler
Can. The specific code is as follows:
If the execution result of the asynchronous task is as follows, the thread pool configuration of the asynchronous task has taken effect:
3 Configure scheduled tasks
Scheduled task can be said to be a very common configuration in project development, which exists as compensation for business functions and plays a very important role. The use of scheduled task can be divided into fixed time and crontab expression. As for the implementation tool, there is @schedule which comes with Spring, which is widely used in single projects. This is good enough for simple tasks without the need for other platforms and additional configurations, but not enough for distributed systems. Quartz, Elastice-Job, xxl-Job, etc. Xxl-job is particularly good. Here, just to cover the use of @schedule:
- 1 Global Enable
@EnableScheduling
Annotation. - 2 Add the task to be performed
@Schedule
Annotations and specify the execution method, whether fixed or adoptedcron
Expression.
Crontab expressions are widely used in actual project development. Here are the generated addresses of crON expressions for reference:
https://www.bejson.com/othertools/cron
Copy the code
Similar to asynchronous task execution, scheduled task execution also has its own asynchronous task thread pool. The interface to be implemented here isSchedulingConfigurer
, to achieve itsconfigureTasks
Methods can be:After all the configuration files are ready, the thread pool configuration of the scheduled task takes effect after running:
4 summarizes
This article describes the configuration of asynchronous task processing and timed task asynchronous configuration, in the subsequent articles will continue to share springBoot configuration related content, welcome to pay attention to.