当前位置:网站首页>I'm stupid. When completable future is used with openfegin, it even reports an error
I'm stupid. When completable future is used with openfegin, it even reports an error
2022-07-27 18:50:00 【ikyrxbxfas】
cause
In an iterative requirement , You need to record user behavior , Thinking about recording user behavior, this part of business logic can be modified to execute asynchronously , I used CompletableFuture Of runAsync() Method to implement asynchronous , Originally, the local environment did not report an error , But when it was sent to the development environment server, it reported an error .
Error reporting pseudo code
CompletableFuture.runAsync(()->{
// Drop out user The service saves user behavior
userClient.saveUserBehavior();
});
Copy code Error messages and the environment in which they are used
Environmental Science :jdk It uses openjdk11,SpringCloud The version is 2020.0.3
java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.condition.OnPropertyCondition
Copy code Problem solving process
Because there is no error in the local environment , But the development environment is wrong , So I didn't merge when I first checked the code , Whether the code is not submitted to the development environment . But after rechecking , It is found that the code has been merged and submitted to the development branch . Then start to analyze the cause of the error , When you see this mistake , I believe you, like me, will think that the first reaction is Jar Package conflict , But carefully check the dependent Spring Version of , Find out Spring Version of the does not conflict . Thinking about whether it is the code I changed that has changed something wrong ? I'm in a panic , I just changed the code to asynchronous , So I changed the code back to synchronization , As expected, there is no problem .

Try to find the cause of the problem
Although the problem has been solved , But I'm a curious programmer , Why is it CompletableFuture Drop out Fegin The interface will report an error , What is the cause ? With this question , I started in GitHub I went shopping . I thought I was not the only one who used it like this , So many students should also encounter such problems . Sure enough github It didn't disappoint me , There is really something I want on it .( So programmers should be more Github Go fishing )
In this issues in , I found the answer ,issues The submitter of is the same as the problem I encountered , Even the error information is so similar , I immediately felt happy . In general Fegin Interfaces are lazy , Only when we first use this Fegin Interface will be right Fegin Interface to initialize , But if ForkJoinWorkerThread Use in Fegin Interface , Will appear ClassNotFoundException. Use other common JVM There will be no problem with thread pool .

Let's take a look Spring Cloud The team's answer to this question
Spring Cloud The answer given by the team is :

Roughly speaking, in parallel flow ( Multithreading ) Call in FeignBlockingLoadBalancerClient When it comes to ClassNotFoundException.
But after local testing , Only in use ForkJoinPool Call in the thread pool Fegin The interface will report an error , If we were ThreadPoolExecutor Customize the thread pool or use jdk Thread pool provided ( for example FixedThreadPool) Call in Fegin The interface will not report errors , By coincidence CompletableFuture The default thread pool in is also through ForkJoinPool Thread pool . Is there really such a coincidence ? These two developers confirmed that in ForkJoinPool Use in Fegin This error will appear in the interface , But the others JVM Thread pool can work normally .


Then why is there no problem testing in the local environment , But publish to dev The environment is having problems ?
With this question, I checked the differences between the development environment and the local environment , Discover the JDk The version is 11, however dev Used in the environment JDK yes Openjdk11. Then I replace it locally jdk The version is openJdk, Conduct further tests locally .
Sure enough, the code reported an error again .
java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.condition.OnPropertyCondition
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(Unknown Source) ~[na:na]
at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
at java.base/java.lang.Class.forName(Unknown Source) ~[na:na]
Copy code So this question Spring Cloud Has the team solved it ?

unfortunately , There is no solution .Spring Cloud The team believes that the priority of this problem is not high , And not recommended in CompletableFuture Call in Fegin Interface , It also said that if someone in the community is willing to submit code for this problem ,Spring Cloud The team will also carefully review the code , If passed, the code will also be merged .
ok , It seems that I can only use custom TreadPool To achieve asynchrony ( You can also use it mq).
summary
Only in openJDK11 This problem only occurs in version ( I also tried it when I got home JDK8, There's no exception ), And if it's in ForkJoinPool Call in Fegin Interface , Then there will be ClassNotFoundException. because Spring Cloud The team believes that the priority of this problem is not high , It doesn't solve the problem . although CompletableFuture It really works , But if you need to be in CompletableFuture Call in Fegin Interface , Then it is suggested to define a custom thread pool , Don't use the default ForkJoinPool.
Use the following api,CompletableFuture We will use our incoming thread pool to execute tasks .
public static CompletableFuture<Void> runAsync(Runnable runnable,
Executor executor) {
return asyncRunStage(screenExecutor(executor), runnable);
}
Copy code So if you need to call asynchronously Fegin Interface , You need to pay more attention , Avoid trampling .
If there are any questions , Feel free to leave a comment in the comments section below . Last , Originality is not easy. , If this article helps you , Then give a compliment before you go .
边栏推荐
- Solve the problem of JSP cascading
- uniapp运行到手机(真机调试)
- Conflict between blur event and click event in input box
- TS learning notes interface
- mysql视图基本操作
- C basic concepts list description suggestions collection
- 瑞吉外卖笔记
- ERROR 1366 (HY000): Incorrect string value: ‘\xE8\xB5\xB5\xE9\x9B\xB7‘ for column ‘s_ name‘ at row 1
- js中的函数与DOM获取元素和事件属性的使用
- EN 1155 building hardware swing door opener - CE certification
猜你喜欢

JS to realize simple form verification and select all functions

地图找房的实例

Knowledge map - Jieba, pyhanlp, smoothnlp tools to realize Chinese word segmentation (part of speech)

2021.8.9 note request

百度地图技术概述,及基本API与WebApi的应用开发

2021.7.30 note index

Login page tablelayout

【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

Uni app traversal array rendering data vertical rendering

What does the number of network request interface layers (2/3 layers) mean
随机推荐
TS study notes class
Mybtis-Plus常用的内置方法
Log4j epic loopholes, big companies like jd.com have been recruited
The hero of the aircraft war comes out with bullets
微信小程序微信支付概述
What if MySQL database forgets its password???
Hbuilder submission code
Aircraft collision detection
MySQL code database creation parking management system foreign key
2021.7.30 note index
MySQL查询列必须和group by字段一致吗?
V-bind and V-for
机器学习分类任务效果评估指标大全(包含ROC和AUC)
Conflict between blur event and click event in input box
What does the number of network request interface layers (2/3 layers) mean
MySQL learns the relationship between Day2 Sorting Query / aggregation function / grouping query / paging query / constraint / multiple tables
Complete set of machine learning classification task effect evaluation indicators (including ROC and AUC)
Infrared hyperspectral survey
浴室带除雾化妆镜触摸芯片-DLT8T10S
The combination of text and words perfectly explains the implementation process of MySQL logical backup