preface

It’s another Sunday, and I’m still using a power adapter to warm my hands, so let’s talk about heat in this chapter.

Ahh ahh…..

If you are in a Spring Boot project, you may add the dependency spring-boot-devtools. When you modify the code, the Spring Boot project will restart automatically. Of course, there are some drawbacks, such as it doesn’t work, or it takes a long time to change the code, so it’s better to manually click the restart button, and if Tomcat is configured, it may not work at all (as I tested).

If the start time of the project is not very long, then it is tolerable, but can you stand it for more than ten seconds or longer? You may have begun to smell. In this case, is there any way to solve this problem? You can modify the code and manually click Hot Swap Classes. You can also update the class without restarting the project, but it has the advantage of launching it as debug.

Then we are in another way, after all, all roads lead to Rome, after continuous research, finally I decided on a plan, that is to change the Tomcat source…… Well, there might be some IDEA plugins, but I didn’t try them out because it was rumored to be for a fee, so I ended up spending some time modifying the Tomcat source code to make it support this functionality fundamentally.

Tomcat does not need to restart Tomcat for hot loading, but it does need to restart the project to update the class. It is very troublesome to restart the code after modifying it during the development phase. Therefore, we discuss hot replacement, which means that the modified code can be replaced without restarting the project. Make it work in one second. How about that? Sounds exciting?

Yes, at least I am, but if you start your Spring Boot project using the main method, then I can’t do anything about it, as long as you can configure a Tomcat in IDEA and run it in Tomcat, then everything is fine, as shown in the following figure.

This launch principle IDEA is in the/user /. IntelliJIdea2019.3 / system/tomcat folder, create a Unnamed_ project name under inside there will be some tomcat related configuration files, the following figure:

JMX generates a JMX account and password, which will be regenerated each time Tomcat is started. You can use the jConsole tool to debug the connection. The project information is configured through the Context tag, whose docBase attribute points to the class path.

If you are familiar with Tomcat, you should be familiar with these configurations. The next step is to modify the catalina.base path to point to this folder. In this way, Tomcat applications under Webapps will be invalid, and the configuration information under conf will be invalid.

If you are careful, you will notice that Tomcat prints this information when it is started.

10 - Jan - 2021 14:04:28. 452 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. The log Server. Server Version: Apache Tomcat / 9.0.33 14:04:28 10 - Jan - 2021, 456 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. The log server build: Mar 11 2020 09:31:38 UTC 10. - Jan - 2021 14:04:28 457 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log Server version number (: 14:04:28 9.0.33.0 10 - Jan - 2021. 457 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log OS Name: Linux 14:04:28 10 - Jan - 2021. 457 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log OS. Version: 4.15.0 10 - Jan - 2021-30 deepin - generic 14:04:28. 457 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log architecture: 10 - Jan - 2021 14:04:28 amd64. 457 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log Java environment variables: / home/HouXinLin/apps/Java/JDK/jdk1.8.0 _241 / jre 14:04:28 10 - Jan - 2021. 457 information [main] Org. Apache. Catalina. Startup. VersionLoggerListener. Log the JVM version: 1.8.0 comes with _241-10 - Jan - 2021 14:04:28 b07. 457 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log JVM. Suppliers: Oracle Corporation 14:04:28 10 - Jan - 2021. 457 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log CATALINA_BASE: / home/HXL /. IntelliJIdea2019.3 / system/tomcat/Unnamed_auto - 10 - Jan - 2021 14:04:28 signin. 458 information [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: / home/HouXinLin/apps/tomcat/tomcat2 / apache tomcat - 10 - Jan - 2021 14:04:28 9.0.33. 460 information [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: . - Djava. Util. Logging. Config file = / home/HXL /. IntelliJIdea2019.3 / system/tomcat/Unnamed_auto - signin/conf/logging properties 10 - Jan - 2021 14:04:28. 460 information [main] org. Apache. Catalina. Startup. VersionLoggerListener. Log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManagerCopy the code

His principle is simply introduced, now it’s our Tomcat, first put a video.

As you can see, the real-time update is completed within 1 second, but it can only take effect after modifying the method body. If the method is added or deleted, it cannot be updated and can only be restarted.

The benefit is fast, fast, very fast, very, very fast, sessions don’t get lost, nothing gets lost!

The principle of decryption

Fortunately, I have studied the Tomcat source code before, otherwise it may not be able to implement, to understand the principle, you need to master the Tomcat project startup process, and the most important Web application class loader, this is the key, Tomcat will generate independent class loader for each application. Called ParallelWebappClassLoader, he is in the boot method of StandardContext is instantiated, each StandardContext means a project application.

In JDK5, Java Agent introduced a NB package called java.lang.Instrument, which can dynamically modify the system class at runtime.

His techniques are summarized in the next chapter, and here’s how to use them.

use

Download Tomcat

I built it based on Tomcat9, and since I need tools.jar in JDK /lib, I will add the size of Tools. jar (17M) to the package, using the same way as the original Tomcat. The lowest JDK version should require 8.

It is not recommended to run Tomcat online, which is suitable for the development and debugging phase. The official version should be used online.

Link: houxinlin.com/apache-tomc…

Then go back to IDEA, click Configure Tomcat, and then select Tomcat. Be sure to select mine. I won’t talk about the rest of the steps.

The following logs are displayed after the startup.

10-Jan-2021 14:21:00.731Message [Attach Listener] Com.hxL.agentMain Tomcat hot replacement is started10-Jan-2021 14:21:00.754Information [Attach Listener] com) HXL) AsmRedefine. Complete initClassLoader class loader is initialized, [found1] a10-Jan-2021 14:21:00.755Information [Attach Listener] com) HXL) AsmRedefine. [/ ROOT] To addItem mapping [/home/HouXinLin/projects/java/Idea/Java-Project/auto-signin/target/classes]Copy the code

If you have any questions, please leave a comment in the comment section.

update

One of my friends mentioned jRebel in the comments section. I tried it and found it was quite useful. I can also replace the new method and I will add this function to Tomcat in the future