Change the method at runtime via the hot swap mechanism

Suppose we have a trivial Java program consisting of just one class:

public class HelloWorld { private static void replacable(int i) { System.out.println("Today is a nice day with a number " + i); } public static void main(String[] args) throws Exception { for(int i = 0; i < 100000; ++i) { replacable(i); Thread.sleep(500); } } 

After compiling and running, the output will be as follows:

Today is a good day with number 0

Today is a good day with number 1

Today is a good day with number 2

Today is a good day with number 3

...

My question is: is there (or is there on the horizon) a way to exchange the replacable method at runtime? Something like writing another version of HelloWorld with a new version of replacable , compiling it, and then the old version in an already running JVM?

So, if I write a new version as follows:

 private static void replacable(int i) { System.out.println("Today is an even nicer day with a number " + i); } 

there is something similar to replacing Erlang hot code where I can do this:

  • run the original program
  • write a modified version
  • using the command line, connect to the running JVM and replace the existing method

so at runtime this will happen:

Today is a good day with number 15000

Today is a good day with number 15001

Today is even nicer day with number 15002

Today is an even more pleasant day with number 15003

...

Suppose the above program is standalone, runs in the standard Java SE environment, there is nothing else in the classpath, so it's almost a Hello Hello style program.

Note. I know that technologies such as bytecode manipulation ( cglib ), aspectJ , jRebel , JMX , there is a hot swap of methods in Java EE, etc., but that’s not what I was thinking about. Think of Erlang.

+54
java runtime hotswap
Dec 29 '10 at 10:38
source share
8 answers

You can use the open source HotSpot virtual machine or the commercial JRebel IDE plugin to easily achieve your goal (see the comparison table here ).

+38
Jan 03 2018-11-11T00:
source share

You can do this through class loaders. For example, if you are familiar with Servlet containers, such as tomcat, that reload pages as they change during the development process. Here is a great explanation for creating dynamic code in java . This not only explains loading, but also compiling the source on the fly. You should be able to apply the concepts covered by any code reloading strategy that you want to use.

+15
Dec 29 '10 at 20:13
source share

I have used this hotswap ant task in many projects. The java target application can be launched using Ant, Eclipse, the command line, or any other tool if it is running in debug mode when the corresponding port is opened. The linked page contains instructions on how to do this with Ant.

Any number of classes can be hot if the changes are not structural. A change in body is usually reduced to a general condition. You can disable code by running ant script through the shell or Eclipse.

At work, I use a script that hotswaps code automatically changes by comparing timestamps in class files. This is similar to the sample on the project page, which shows a simple example that only hotswaps changed classes.

Note: JPDA is used here .

+9
Jan 03 2018-11-11T00:
source share

A bit old post, but hopefully someone finds this useful:

I found a relatively new Hotswap agent , well documented and feature rich (and best of all , open source ).

+3
Jun 21 '15 at 15:56
source share

You can do this through the JPDA (Java Platform Debugger Architecture) interface: http://download.oracle.com/javase/1.4.2/docs/guide/jpda/enhancements.html#hotswap

It is not automatic, as is the case with Erlang - the JVM does not control the class path for changes to the class files, and then reloads and re-links them if changed, for fairly obvious reasons (Java was designed for a web deployment that you don't want to poll Http URL for changes).

+2
Dec 29 '10 at 10:45
source share

What about OSGi? A hot swap is "built-in" into the specification - I would think that this would be one of the possible solutions.

+1
Jan 04 2018-11-11T00:
source share

You can use the strategy development template, so you have an object for management, not a method, and a protocol for communicating with the program to tell him to use this class name as a class of the Strategy object.

+1
Jan 04 2018-11-11T00:
source share

I wrote an article on this topic. Please refer to this or this .

0
Dec 04 '18 at 10:06
source share



All Articles