In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what to do when SpringBoot uses Async annotations to fail. I believe most people don't know much about it, so share this article for your reference. I hope you will get a lot after reading this article. Let's learn about it together.
Cause Analysis and solution of Async Annotation failure (spring Asynchronous callback) @ Async in Spring
In Java applications, interactive processing is realized synchronously in most cases, but when dealing with third-party systems, it is easy to cause slow response. Before, most of them used multi-threads to complete such tasks. In fact, @ Async has been built in after spring 3.x to solve this problem perfectly.
Sometimes the @ Async annotation fails during use
(for the same reason that @ Transactional annotations sometimes fail).
The following defines a Service:
Two asynchronous execution methods, test03 () and test02 (), are used to simulate time-consuming operations that may occur in the project, and then the test () method calls the two time-consuming methods:
Define Controller:
Execute the method and return the result:
The result of the method execution is obviously not in line with our expectations, and the output order of the method indicates that the two asynchronous methods test02 () and test03 () are executed synchronously, that is, the @ Aysnc annotation is invalid!
The reason for the failure is that we call the test02 () and test03 () methods directly in the test () method, which is equivalent to the calls of this.test02 () and this.test03 (), that is, the real invocation of the test02 () and test03 () methods is called by the TestService object itself, while the @ Async and @ Transactional annotations essentially use dynamic proxies, and it should be the TestService proxy object that calls the test02 () and test03 () methods. In fact, when initializing the Spring container, the Spring container will "replace" the class object containing AOP annotations with a proxy object (simply understood this way), then the reason for the invalidation of the annotation is obvious, because the method is called by the object itself rather than the proxy object, because it has not gone through the Spring container, then the solution will follow this way of thinking.
Many bloggers on the Internet say that the solution is to extract the method to be executed asynchronously into a single class, which can indeed solve the problem of asynchronous annotation failure. The principle is that when you extract the method executing asynchronism into a class separately, this class must be managed by Spring, and other Spring components will definitely be injected into it when they need to be called. At this time, what is actually injected is the proxy class. In fact, there are other solutions that do not have to be extracted into a separate class.
Solution one
Get your own proxy object to call an asynchronous method through the context in TestService
In fact, our injected objects assign member variables to the current Spring component from the Spring container. Because TestService uses AOP annotations, in fact, TestService actually exists its proxy object in the Spring container.
As a result, the asynchronous method executes asynchronously:
Solution number two
Open the cglib proxy and obtain the Spring proxy class manually
Add the following to the startup class
Use AopContext.currentProxy () to get the current proxy class:
In order to prove that the object in the Spring container is the current proxy class object, a sentence is specially output:
Running result:
OK, the problem is solved perfectly!
The application.properties configuration is as follows:
# java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to' true' to make it available.# add @ EnableAspectJAutoProxyspring.aop.auto=true# enable CGLIB Agent spring.aop.proxy-target-class=truespringboot @ Async possible reasons for failure
1. Other functions in the current class call functions annotated with @ Async
2. There is polymorphism in the current class, and the method name is the same.
3. No @ EnableAsync is added to the startup class
The above is all the contents of the article "what to do when SpringBoot uses Async annotations to fail". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.