Hi, I’m Jack

Recently, I encountered a particularly strange problem in the project: the operation of regularly synchronizing data in the program could be successfully executed by unit test, but the main program could not be successfully executed by directly starting it (Spring Boot framework, Java language, IDEA development and junit testing framework).

guess

I have not encountered similar problems before. According to this phenomenon, the guess is that the two startup should have different parameters, or the loading class is different or the loading order is different. But in theory, there seems to be no difference between what Spring loads during startup, so it was a bit confusing at first

Compare the differences

Then try to start the process of troubleshooting the problem step by step

As mentioned in the previous article, there is a more efficient way to troubleshoot problems that seem strange: If there are correct situations, including successful scenarios for accidental problems and correct results produced by different operating modes, we can start with the difference between the scheme that produces problems and the correct scenario to analyze the location of the cause of the problem

Preliminary progress

The synchronization method save() is located in debug mode

But into the unknown number of layers of calls, and did not find the problem. The save() method was executed once when the main program started and never executed again. Unit tests, on the other hand, work every time

Log print

When the save() method executes, the exception is caught, the exception log is printed, and the success log is printed

try{ save(data); }catch(Exception e){log.error(" failed to perform, {}", LLDB etMessage()); } log.info(" executed successfully: {}",data);Copy the code

As a result, no logs are printed. There is no failure log, and there is no success log. That is to say, the main program in the startup process, in the first execution of the save method, there is an exception

But the specific occurrence of what abnormal, temporarily do not know

1. Adjust the log level

What exactly is unusual? The default log level is INFO, so I changed the log level to DEBUG

#slf4j sets the log level logging.level.root=debugCopy the code

Start the unit test first, and find that there are almost no redundant error logs

Then start the main program, found that there is indeed an error, and in the print, should be constantly retry the effect

javax.management.InstanceNotFoundException: org.springframework.boot:type=Admin,name=SpringApplication
Copy the code

Because there is no other way, so directly with this error as a breakthrough to analyze

Second, analyze the phenomenon

Let’s start with a wave:

The main class of Spring Boot, SpringApplication, could not be found

Is there something that wants to load Spring Boot before it has completed class initialization, autowiring, etc.

Following this guess, a bold idea popped into my head, which was caused by the difference in startup parameter configuration

As a result, I looked at IDEA respectively for the main program and unit test startup parameter configuration is any different

1. Unit test startup configuration

2. Start configuration of the main program

As you can see, the main program starts with two more configurations than the unit test starts by default, which may be the cause of the problem!

Currently, for our original problem, both have been found so far

Third, problem solving

Therefore, the following solution steps are adopted:

1) Select Run in the IDEA toolbar and select Edit Configurations in the list that opens

2) Under Spring Boot on the left of the open panel, select the Application to be run

3) Uncheck Enable Launch Optimization and Enable JMX Agent on the right

4) Restart the service.

try{ save(data); }catch(Exception e){log.error(" failed to perform, {}", LLDB etMessage()); } log.info(" executed successfully: {}",data);Copy the code

Restart the service and discover that the error no longer exists and that the save() method has finished executing

A successful execution log is printed

Aha, then the problem is perfectly solved!

Currently, the problem is that after Boot optimization and JMX agents are enabled, they will load Spring Boot before Spring Boot is fully started. The order changes and some classes or configurations fail to load properly

Turn them off and the problem is solved!

Four,

Ok, the problem is not difficult, the root cause of the problem has not really been found out, but it is also a more strange knowledge

Review the way we solved it: guess, locate, guess, log level adjustment, analyze, validate

1) First look for specific phenomena through superficial phenomena: the save() method only enters once, and problems occur in the process of method execution

2) Then consider what might be the cause of this problem, and guess that there may be a problem in the process. But no log is printed, in other words, a log is printed and cannot be seen at the current log level

3) So, turn down the log level and try to compare the difference in the printing of the log on startup

Once we switched to debug logs, we immediately noticed the difference: when we started using the main program, we printed an extra exception stack message, and then the cause of the problem fell into this exception message

4) Turn to the solution of another obvious problem

By resolving this exception, you solve the problem itself

This problem is mainly to record the use of IDEA for Spring Boot framework Java program development, there will be default Boot parameter configuration, resulting in strange problems (of course, other frameworks or using the editor may have similar problems); I also want to share with you a solution to a similar problem to avoid wasting too much valuable time

In addition, I want to emphasize again the importance of mastering the log printing level. It is true that you can quickly locate the problem through debugging, but the premise is that you have a clear understanding of the current program execution process and execution logic

When the program is more complex, it is more efficient to adjust the log level to locate problems

Well, that’s it. Learn a little every day, and time will show you how strong you are

Welcome to pay attention to the public account, together with continuous learning ~