The failure reason

  • The @enableasync annotation is not added to the Application startup class to EnableAsync.
  • Adding async annotations to a method in the same class as the current caller will invalidate async;

An async annotation may cause loop dependencies in the application

Solutions:

  • Moving async annotation methods into a new Service class is a quick fix;
  • Of course, you can also use the @lazy annotation to handle classes that cause loop dependencies;

Why is it empty to fetch HttpServletRequest from RequestContextHolder in the @Async annotation method?

Since @async annotated methods are thrown into the thread pool by SpringBoot, a new thread is started; If the RequestContextHolder uses ThreadLocal to hold the request instance, the RequestContextHolder will not be able to retrieve the request in a new thread. If you’re not familiar with ThreadLocal, check it out and ask about it a lot during interviews.

So how can I get a request in async?

It is recommended that you pass Request as a parameter to an asynchronous method when calling it

There will be a large number of interview materials and architect must-see books waiting for you to choose, including Java foundation, Java concurrency, micro services, middleware and more information waiting for you to take oh.