Arthas is an open source Java diagnostics tool on Alibaba that allows you to monitor and inspect Java applications, but also provides useful Java hot updates.

Java hot update refers to updating and replacing code without restarting the project. It can be used to update Java programs non-stop, especially for Java projects that take a lot of time to start.

Arthas is very simple to use, it provides a Jar package that you just need to download locally and run to use it properly.

Arthas function description

Arthas can help you when you are stuck with a problem like the following (official) :

  1. From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
  2. Why didn’t the code I changed execute? Did I not commit? Got the branch wrong?
  3. If you encounter a problem, you cannot debug it online. Can you only re-publish it by logging?
  4. There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!
  5. Is there a global view of the health of the system?
  6. Is there any way to monitor the real-time health of the JVM?
  7. How to quickly locate application hot spots, generate flame map?

Arthas supports JDK 6+, Linux/Mac/ Windows, command line interaction, and rich Tab completion to further facilitate problem location and diagnosis.

Arthas use

The steps to use Arthas are as follows.

Step 1: Download Arthas

First of all, we first Arthas jars downloaded to the local, its download address is: alibaba. Making. IO/Arthas/arth…

Step 2: Start Arthas

Arthas can be started using the Java -jar arthas-boot.jar command. Arthas can be started using the Java -jar arthas-boot.jar command.

Arthas is successfully started as shown in the figure above.

Tip: When we run the java-jar arthas-boot.jar command, we first need to switch the directory to the location of the jar package to start arthas properly.

Step 3: Run Arthas

After we have started Arthas, we need to select a Java process to debug as shown in the figure above. For example, we type “4” to monitor a Java test program I wrote. The result is as follows:

When the Arthas logo appears, Arthas has loaded the Java process.

Step 4: Operate Arthas

Once Arthas has successfully loaded the Java process, we can enter the relevant commands to view the relevant information.

If we think of the local environment as a production server, we need to see if a running Java program is up to date.

Before Arthas, our usual steps went like this:

  1. Find the corresponding JAR package (or WAR package);
  2. Download the JAR package (or war package) locally;
  3. Find the corresponding class for decompression operation;
  4. Then drag the extracted class file to the Java compiler (Idea or Eclipse) to see if it is the latest code.

But if Arthas was used, we could simply decomcompile bytecode into normal Java code and then verify that it was the latest code. You only need to run the jad command as follows:

In this way, we can directly check whether the released program is the latest version.

Not only that, but we can also use Arthas to monitor the overall performance of the program, as shown below:

We can also use Arthas to view some information about the JVM, as shown below:

More Arthas please visit: the function of alibaba. Making. IO/Arthas/comm…

Hot update Java code

Suppose our original code looked like this:

package com.example;

import java.util.concurrent.TimeUnit;

public class App {
    public static void main(String[] args) throws InterruptedException {
        while (true) { // Prints a message every two seconds
            TimeUnit.SECONDS.sleep(3); sayHi(); }}private static void sayHi(a) {
        // The id that needs to be modified
        boolean flag = true;
        if (flag) {
            System.out.println("Hello,Java.");
        } else {
            System.out.println("Hello,Java Chinese community."); }}}Copy the code

We now want to change the flag variable to false to do this:

  1. Compile new Java code into bytecode using Arthas’s in-memory compilation tools;
  2. The use of ArthasredefineCommand to implement hot update.

1. Compile bytecode

First, we need to compile our new Java code into bytecode, which we can do with Arthas’s MC command, which stands for Memory Compiler.

The implementation example is as follows:

[arthas@3478]$ mc /Users/admin/Desktop/App.java -d /Users/admin/Desktop
Memory compiler output:
/Users/admin/Desktop/com/example/App.class
Affect(row-cnt:1) cost in 390 ms.
Copy the code

-d indicates the location where the compiled file is stored.

Tip: We can also use the bytecode generated by Javac app.java, which results in the same results as this step.

2. Perform hot update

Once we have the bytecode file, we can use the re-define command to implement hot updates as follows:

[arthas@51787]$ redefine /Users/admin/Desktop/com/example/App.class
redefine success, size: 1
Copy the code

It can be seen from the above results that the hot update is successfully executed. At this time, we go to the console to check the execution result, as shown below:

This indicates that hot update execution did succeed.

Arthas Hot update notes

There are some limitations to using the hot update feature. We can only use it to modify some business code inside the method, and the hot update will fail if we have any of the following:

  1. Add class attributes (class fields);
  2. Add or remove methods;
  3. Replace running methods.

The last one needs to be explained separately, if we change the above example to the following code:

package com.example;

import java.util.concurrent.TimeUnit;

public class App {
    public static void main(String[] args) throws InterruptedException {
        while (true) { // Prints a message every two seconds
            TimeUnit.SECONDS.sleep(3);
            boolean flag = true;
            if (flag) {
                System.out.println("Hello,Java.");
            } else {
                System.out.println("Hello,Java Chinese community."); }}}}Copy the code

So at this time we again for hot update operation changes the value of the flag will be failure, because we replace the running of the method, and can succeed in our normal sample code, because we are in the while loop wireless call another method, and the method is used by intermittent, so you can replace the success.

conclusion

Arthas is just a normal Java program. You can use java-jar arthas-boot.jar to start it up and then select the Java process that you want to operate on. This enables status monitoring and other operations.

In the second half of the article, we introduced Arthas’s hot updates, which essentially require only a re-define command to load a new bytecode file, but it’s important to note that hot updates don’t replace a running method, they only modify the business code inside the method. If you modify a class field or change a class method, the hot update will fail.

PS: Hot update is cool for a while, but in actual use, we should fully evaluate the safety of the production environment. If you really want to use it, it must be handled by the relevant person in charge, after all, “stability” and “safety” is the first iron law of the production environment.

Follow the public account “Java Chinese community” reply “dry goods”, obtain 50 original dry goods Top list.