Table of contents
Problem Description:
When writing the interface, I encountered a problem. The front end required to return the result directly before running other codes.
problem analysis:
Because of the tight budget, I used Netease to send emails this time, that is, use your account to send emails to other people. There are also some problems, that is, it is impossible to send a large number of email messages in a short time, so I added a timer. device.
The limit is to send once every half a minute. When the background receives the array sent by the front end, the data in the array can only be executed once every half a minute, that is, the email sent to several people will be sent to the next person after half a minute. situation.
So this creates a problem, you need to wait for these timers to finish executing before you can get the results returned by the interface.
And this will also cause blocking waiting, which is time-consuming.
problem solved:
Add asynchronous successful resolution, that is, the main process directly returns a result, and the other process executes the time-consuming one.
Implementation:
1. Add the annotation @EnableAsync to the startup class.
2. Add the @Async annotation to the method of the service layer.
3. Just call this method normally in the controller layer.
analysis Summary:
The @Async annotation can be added to a class to indicate that all methods in the class are executed asynchronously, or it can be added to a method to indicate that the method is executed asynchronously.