Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a method at runtime via a hot swap mechanism

Assume we have a trivial Java program that consists 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 it's compiled and run, output will be this:

Today is a nice day with a number 0

Today is a nice day with a number 1

Today is a nice day with a number 2

Today is a nice day with a number 3

...

My question: does there exist (or is there on the horizon) some way to swap 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 the new version like this:

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

is there something similar to Erlang's hot code swapping where I can do this:

  1. run original program
  2. write modified version
  3. using a command line program, connect to running JVM and replace existing method

so that, during runtime, this will happen:

Today is a nice day with a number 15000

Today is a nice day with a number 15001

Today is an even nicer day with a number 15002

Today is an even nicer day with a number 15003

...

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

Note: I know that technologies like bytecode manipulation (cglib), aspectJ, jRebel, JMX, hotswapping of methods in Java EE etc. exist, but they aren't what I'm thinking of. Think of Erlang.

like image 371
darioo Avatar asked Dec 29 '10 10:12

darioo


People also ask

What is hot swap in Java?

Java IDEs and VMs support a feature called HotSwapping. It allows you to update the version of a class while the virtual machine is running, without needing to redeploy the webapp, restart, or otherwise interrupt your debugging session. It's a huge productivity boost if you're editing the body of a method.

What is hot swapping feature?

Hot swap is a key feature of USB devices that allows the addition of peripheral hardware without having to shut down the system. So, when a component in a server fails, the redundant unit automatically takes over. Flawed or failed hardware can be replaced without disruption.

What is hot swap in Android?

Hot swapping occurs when the code within an existing method implementation is changed. The new method implementation is used next time it is called by the app.

How do I enable HotSwap in Intellij?

Reload all files Recompilation happens automatically if the Build project before reloading classes option is enabled in Settings/Preferences | Build, Execution, Deployment | Debugger | HotSwap. If this option is disabled, you need to recompile the files before reloading (Build | Recompile Ctrl+Shift+F9 ).


1 Answers

You can either use the open-source HotSpot VM or the commercial JRebel IDE plugin to easily achieve your goal (view comparison table here).

like image 99
Arun Avatar answered Sep 20 '22 09:09

Arun