Sometimes, when you're making minor changes to your code, you want to immediately see how they will behave in a working application without shutting down the process. As we all know, hot swap can be achieved through debugging. But in some cases, the debugging port is restricted due to permission control, then you will not be able to achieve HotSwap through debugging.
The IntelliJ IDEA plugin introduced in this article can help implement hot swap on a remote server, and the operation is simple and fast. The plugin in this article is based on Arths redefine command.
- The first way, “IntelliJ IDEA(Top Left Corner)” -> "Prefrences" -> “plugins” -> "Browase Repositories" -> search for ArthasHotSwap
- The second way, Download the latest version of the installation package from Releases, then open IDEA and click, “IntelliJ IDEA(Top Left Corner)” -> "Prefrences" -> “plugins” -> “install pulgin from disk” -> "choose the installation package".
Usually you can use it directly without configuration. In some special cases, you need to config "the absolute path of java command" or "specify the full path calss name of Java process" according to the application configuration. Configuration: “IntelliJ IDEA(Top Left Corner)” -> "Prefrences" -> “Tools” -> “ArthasHotSwap”。
HotSwap uses bytecode files, so first, we need to compile related projects, in the future, we can only compile the modified files to save time.
Find the files(.java or .class) that need to be modified by HotSwap, right click in the IDEA, choose “ArthasHotSwap” and click “Swap this class”. When the plugin is successfully executed in the background, the plugin will copy the command required for HowSwap to the pasteboard.
Next you need to log in to your remote server, then paste the command form the clipboard and execute it with administrator privileges. If the first execution fails, please execute again. Due to the need for Alibaba Cloud OSS as a medium to transfer bytecode files, your remote server needs to be able to access to the Alibaba Cloud Server.
Due to VM design, HotSwap has the following limitations:
-
it is only available if a method body is modified. Changing signatures is not supported.
-
adding and removing class members is not supported.
-
if the modified method is already in the call stack, the changes will take effect only after the program exits the modified method. Until that moment, the method body remains unchanged, and the frame is marked as obsolete.
Arthas is a Java diagnostic tool open-sourced by Alibaba middleware team. Arthas redefine is realized on the basis of Instrumentation API. The plugin uploads bytecode files to the remote server with the help of OSS, and reload classes changed with Arthas redefine.
- Find the corresponding class file according to the source file selected by users;
- Use AES-128 to encrypt class file,output the encoding of base64;
- Upload encrypted class file to OSS, and return the access address of OSS;
- To render the script of HotSwap, and upload the script to OSS, return the access address of OSS;
- Assemble the command of HotSwap and automatically copy it to the pasteboard.
- Log in to the remote server, paste the command and execute it, next you just need to wait for the command execution to complete;
- Download the script of HotSwap from OSS, and execute it automatically;
- Create a workspace;
- Check whether OpenSSL is installed. if not, install it;
- Download class file encrypted by ES-128;
- Use OpenSSL ENC to decrypt the encrypted file to get class binary file;
- Install Arthas;
- Create a pipe to communicate with Arthas and start Arthas;
- Arthas selects Java process for attaching, and the first one is selected by default, which can also specified by the use;
- Arthas executes the command of redefine to replace the class;
- Prints the result of HotSwap.