preface

Choosing a programming language depends not only on the features provided by the language itself, but also, crucially, on its ecology. Ecology determines the true efficiency of development and how tall a giant it can stand on.

Java, on the other hand, has an ecosystem that no other language can match, not only when it is programmed, but when it is run, it is also the most observable and debuggable language in the world. This article will show that in one aspect.

This article introduces a development method that allows you to write code that works as soon as JS does, and to make calls at a distance that JS does not.

Applicable people

All Java development, especially Spring Boot-based developers. The following scenarios are developed in Spring Boot by default.

Applicable scenario

  • Scenario where xxL-Job or other scheduled tasks need to be scheduled by the task center
  • Developing RPC interface such as Dubbo, it is difficult to construct the scenario of request protocol test interface
  • When developing a consuming MQ message queue, you need to construct the message content remotely

Using a combination of Arthas + JRebel, each of the above scenarios can be invoked directly, bypassing all external system dependencies and constructing parameters at will.

Results demonstrate

Quick call method

Quick call xxL-job:

Call other Spring beans

Can be modified at any time effective at any time

Create a test method and build test parameters

How?

JRebel Plugin For IDEA

JRebel is an amazing tool that does true code hot loading and eliminates most reboots.

We know that code is hot-loaded and hot-updated, usually by using the classLoader’s retransformClass and redefineClass. For example, Skywalking implements the proxy by using the retransformClass to change the contents of the class before it is loaded. Tomcat and Spring Boot hot loads are the latter. But redefineClass has a fatal flaw. All changes can only be made to existing methods and attributes.

JRebel, on the other hand, went the other way, and here’s How it works: How does JRebel work? You can check it out if you are interested, but there are few implementation details in the article. JRebel circumvents the limitations of Java itself and implements its own. Not only that, but in a container like Spring, it’s not just a matter of adding new classes, but also registering them as beans. So JRebel is also responsible for managing the Spring framework’s process of loading beans. A lot of work has to be done. It is fair to say that in this field, there is no substitute.

The JRebel IDEA plug-in can be installed and activated on the Internet. JRebel does charge, but currently there is no channel for individual users to pay for it.

Tips1: Remember to select “On ‘Update’ action under Spring Boot as” Update classes and Resources “in the project boot configuration:

Tips2: JRebel MybatisPlus Extension is required if MyBatis Plus is used in the project:

Now we can add/modify classes, methods, variables, HTTP interfaces, MyBatis XML and so on without having to restart the project repeatedly.

Can methods be invoked dynamically?

JRebel does just about everything dynamically, Java class loading makes it clear that everything can be dynamic, so can it dynamically call methods? Why do methods have to be called layer by layer?

When we were developing, we often encountered a lot of debugging trouble, in addition to THE HTTP interface, such as Dubbo interface, XXL-Job scheduled tasks, deep functions in the program, are very inconvenient to trigger. Sometimes it takes ten minutes to validate an SQL condition because the hierarchy is so deep.

So I started looking for a way to do this, JRebel doesn’t have a universal IDEA, is there an IDEA plugin to do this? There is no.

In the process of searching, I gradually realized the nature of the problem. Calling a method sounds simple, but the most troublesome thing is to do it in the right context, such as Spring’s, otherwise the calling code itself will just get a bunch of Npes. In other words, we need to find concrete instances to implement the corresponding method.

Arthas

Arthas is a divine Java diagnostic tool that elevates Java observability to incredible levels in one stroke. Arthas has many functions. Please click here for a new article on Arthas.

In a way that Arthas itself may not have anticipated, it can also be used to improve efficiency in development. Just now we wanted to call the method dynamically, Arthas provides two ways to do this:

  1. vmtool

We can use vmTool to query objects from the JVM and use that object to execute methods. Usually, this object is what we want. However, the actual measurement conflicts with the IDEA during development, and an error will be reported. Normally deployed projects can use this approach.

  1. ognl

Ognl is an expression that operates on objects. In the article Arthas gets the target object that Spring is proxied to, we mentioned that we can use it to call methods in Spring, as long as we provide a variable that gets the SpringContext.

combined

Here are a few things that will make the development process infinitely smoother:

Arthas IDEA plug-ins

Arthas has too many commands, we can install this plugin in IDEA to automatically generate the commands. After installing the plug-in, you can refer to setting the spring Context to provide the address of the Spring context and configure it in the plug-in.

Arthas Spring Boot Starter

Having Arthas start with the project eliminates the hassle of starting the Arthas selection process each time. If you do not want to start Arthas online, add -dspring.arthas. enabled=false to the start parameter.

See the document: arthas.aliyun.com/doc/spring-…

Embedded Web Browser

IDEA a small plug-in, built in the IDE easy browser. We’ll use this to open the Arthas console instead of bouncing back and forth between the browser and IDE.

Set two shortcut keys to make it a step faster

  • Idea Arthas replicates the command to call spring methods:

  • Idea Reload project action:

The final result

Now that we have the overall efficiency kit, we can write code without restarting through JRebel, call methods to copy commands through the Arthas Idea Plugin, open a small built-in browser window, and paste in the Arthas console. Is the method wrong? After the change, click update, and then return to the small window to call once, at any time to verify. The code no longer needs an HTTP interface; the function is the entry point.

Later I will record a full development process Demo to show more intuitively the efficiency of this development method, as well as its freedom and fun.