Home > Java > javaTutorial > How to handle Springboot asynchronous tasks and scheduled tasks

How to handle Springboot asynchronous tasks and scheduled tasks

PHPz
Release: 2023-05-25 20:05:12
forward
2018 people have browsed it

1 Preface

In Springboot, asynchronous tasks and scheduled tasks are frequently encountered ways to deal with problems. In order to make good use of these two configurations and not interfere with normal business, It needs to be configured asynchronously. How to set up a reasonable asynchronous processing thread is the core and key.

2 Asynchronous task settings

Using asynchronous tasks in projects is a frequently encountered way to solve problems. It is usually to reduce the waiting time of requests and perform asynchronous background processing of business processing, which can improve user experience and increase system throughput.

Opening asynchronous tasks in springboot is also very simple, only requires two steps:

  • 1 Open @EnableAsync annotation.

  • 2 Add @Async to the method that requires asynchronous processing.

It should be noted that the asynchronous execution method can have a return value, but it must be Future, here it is submitted in the multi-threaded submit way Task, how to get the results of processing.

At this point, the configuration and use of asynchronous tasks are over. Asynchronous tasks also use multi-threaded thread pool technology. By default, SimpleAsyncTaskExecutor is used to implement it. However, how can one’s own destiny be left in the hands of others? I always feel that it is not practical. How can asynchronous tasks be implemented? How to use a custom thread pool to implement it? This is a good question, and there is an answer, that is AsyncConfigurer. This is an interface. You need to implement the getAsyncExecutor method to obtain a thread pool. If you need to capture exception information, then implement a method getAsyncUncaughtExceptionHandler.

The specific code is as follows:

How to handle Springboot asynchronous tasks and scheduled tasks

The asynchronous task execution results are as follows, which can illustrate the thread pool of the asynchronous task The configuration has taken effect:

How to handle Springboot asynchronous tasks and scheduled tasks

3 Scheduled task configuration

Scheduled tasks can be said to be very commonly used configurations in project development, as business functions Compensation exists, which plays a vital role. The use of scheduled tasks can be divided into fixed time and crontab expressions. In terms of implementation tools, there are spring built-in @Schedule, is widely used in single projects and does not require the help of other platforms and additional configurations. It is enough for simple tasks, but for distributed systems, it is a bit inadequate. At this time, quartz, elastic-job and xxl-job were born, among which xxl-job was particularly outstanding. Here, I will only introduce the use of @Schedule:

  • 1 Globally enable the @EnableScheduling annotation.

  • 2 Add @Schedule annotation to the task that needs to be executed, and specify the execution method, whether it is fixed execution or using cron expression.

In actual project development, crontab expressions are still widely used:

How to handle Springboot asynchronous tasks and scheduled tasks

and Just like the execution of asynchronous tasks, the execution of scheduled tasks also has its own asynchronous task thread pool. The interface that needs to be implemented here is SchedulingConfigurer. Just implement its configureTasks method:

How to handle Springboot asynchronous tasks and scheduled tasks

After all configuration files are completed, you can see that the thread pool configuration of the scheduled task has taken effect after running:

How to handle Springboot asynchronous tasks and scheduled tasks

The above is the detailed content of How to handle Springboot asynchronous tasks and scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template