In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to solve the problem of abnormal troubleshooting of slow start of spring-boot project. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Problem background
A spring boot development project, spring boot version is 1.5.7, carrying spring version is 4.1.3. Development feedback, suddenly can not start up locally, the appearance feature is that when running on the local IDEA, the process gets stuck and does not exit, and the log of the relevant components is not output when the application is started. The symptoms are shown below:
Analysis of problems
Because there is no useful log information, you cannot troubleshoot the problem at the log level. But if there is no output log like this, in general, the startup process inside the program must be stuck somewhere, which can only be known by printing the current thread stack information. In general, in a server environment, we use the jstack tool in the java toolkit to view it, such as jstack pid (applying the java process).
However, when developing locally in IDEA, IDEA has a built-in tool that can directly view the online text information of the current application, such as:
Notice that the arrow below points to a camera-like icon, so the picture means to print a snapshot of the current thread. After clicking, the thread context information on the right appears. You can see that there are many threads. We mainly focus on the main thread, and the thread status is indeed waiting. Then click on the main thread pointed to by the arrow, and you can see the following:
Main@1 prio=5 tid=0x1 nid=NA waitingjava.lang.Thread.State: WAITINGat sun.misc.Unsafe.park (Unsafe.java:-1) at java.util.concurrent.locks.LockSupport.park (LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt (AbstractQueuedSynchronizer.java:836) at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly (AbstractQueuedSynchronizer.java:997) at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly (AbstractQueuedSynchronizer.java:1304) at java.util.concurrent.CountDownLatch.await (CountDownLatch .java: 231) at org.springframework.boot.autoconfigure.BackgroundPreinitializer.onApplicationEvent (BackgroundPreinitializer.java:63) at org.springframework.boot.autoconfigure.BackgroundPreinitializer.onApplicationEvent (BackgroundPreinitializer.java:45) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener (SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener (SimpleApplicationEventMulticaster.java:158) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent (SimpleApplicationEventMulticaster.java:139) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent (SimpleApplicationEventMulticaster.java:127) at Org.springframework.boot.context.event.EventPublishingRunListener.finished (EventPublishingRunListener.java:115) at org.springframework.boot.SpringApplicationRunListeners.callFinishedListener (SpringApplicationRunListeners.java:79) at org.springframework.boot.SpringApplicationRunListeners.finished (SpringApplicationRunListeners.java:72) at org.springframework.boot.SpringApplication.handleRunFailure (SpringApplication.java:745) at org.springframework.boot.SpringApplication.run (SpringApplication.java:314) at org.springframework.boot.builder.SpringApplicationBuilder.run (SpringApplicationBuilder.java:134)-locked (a java.util.concurrent.atomic.AtomicBoolean At org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext (BootstrapApplicationListener.java:175) at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent (BootstrapApplicationListener.java:98) at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent (BootstrapApplicationListener.java:64) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener (SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener (SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent (SimpleApplicationEventMulticaster.java:139) at org.springframework. Context.event.SimpleApplicationEventMulticaster.multicastEvent (SimpleApplicationEventMulticaster.java:127) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared (EventPublishingRunListener.java:74) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared (SpringApplicationRunListeners.java:54) at org.springframework.boot.SpringApplication.prepareEnvironment (SpringApplication.java:325) at org.springframework.boot.SpringApplication.run (SpringApplication.java:296) at cn.keking.project.customerManagement.KekingCustomerManagement.main (KekingCustomerManagement.java:36)
You can see that the thread is blocked by CountDownLatch.await (), and then look at the following line, where the code block is as follows:
Private static final CountDownLatch preinitializationComplete = new CountDownLatch (1); @ Overridepublic void onApplicationEvent (SpringApplicationEvent event) {if (event instanceof ApplicationEnvironmentPreparedEvent) {if (preinitializationStarted.compareAndSet (false, true)) {performPreinitialization ();}} if (event instanceof ApplicationReadyEvent | | event instanceof ApplicationFailedEvent) {try {preinitializationComplete.await ();} catch (InterruptedException ex) {Thread.currentThread (). Interrupt ();}
This is a class of a secure initialization resource in spring boot, and the code is shown to listen for SpringApplicationEvent events, and to be sure, its logic executes to preinitializationComplete.await (); in this case, the thread is blocked. Normally, spring triggers the ApplicationEnvironmentPreparedEvent event to complete the initialization of resources. Let's not delve into why Spring did this, but mainly look at why it is stuck here through program logic, in preinitializationComplete.await (); make a breakpoint on the line and take a look at the information in the event object, as shown below:
It turns out that event is an exception event object that failed to initialize the Spring context. The object contains specific exception information, as indicated by the arrow, and key exception information such as:
NoSuchMethodError: "org.springframework.util.ObjectUtils.
UnwrapOptional (Ljava/lang/Object;) Ljava/lang/Object; "
Hypothetical problem
Through the above analysis, basically located the real reason behind the appearance of Spring boot application startup stuck, but also located the abnormal information.
The NoSuchMethodError exception occurs because the method cannot be found when the method is called. It usually appears in two related jar packages, but the version does not match, that is, it is often said that the jar version dependency conflicts. After looking at it, ObjectUtils is a class in the spring-core package. This unwrapOptional method is not really available in the current 4.1.3 version, and this method has been added to the spring-core-5.x version. Since there was no problem with the previous dependency, why does the spring context call the 5.x version of the method now?
So let's assume that a recent developer has added new dependencies to pom.xml, causing this problem.
Verify carefully.
It is easy to find the problem, because the code is maintained by git management, so you can check the recent submission record of the pom.xml file. After checking, you do find that there are recent changes to pom.xml and a dependency has been added:
Org.springframework spring-context 5.1.6.RELEASE
There is also the issue of Maven dependency priority. The dependency directly added in pom.xml takes precedence over the pom.xml dependency in other jar, that is, even if the spring boot1.5.7 comes with Spring-context.4.1.3, after this is specified, the application finally depends on version 5.1.6. For specific Maven dependencies, please refer to my blog post "about the use of Maven, do you know all this?" ". Combined with the previous analysis, it is almost due to the addition of this dependency caused by the problem, spring-context.5.1.6 with spring-core.4.1.3 must have a problem ah. Remove this dependency directly, then boot the system and everything is fine, and the log prints the information that Spring loads the online text.
Problem summary
The key to locating this problem is to understand the knowledge of the thread stack in java and troubleshoot the problem through thread snapshots without enough exception logs. After locating the problem, anomalies such as NoSuchMethodError need ordinary experience to assume the real cause of the problem, and then trace the source to identify the root cause of the problem. The essence of finding problems must be this kind of step-by-step thinking. For example, if you go directly to the search engine and say, "Spring boot app startup is stuck," you won't find anything, but when you find out it's due to a jar conflict. Go to search engines to search:
NoSuchMethodError: "org.springframework.util.ObjectUtils.
UnwrapOptional (Ljava/lang/Object;) Ljava/lang/Object; "".
There will be a lot of content, it is easy to solve the problem.
This is the end of this article on "how to solve the problem of abnormal troubleshooting of slow start of spring-boot project". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.