Due to the JVM’s just in time and support for interpreting runtime and overheated code compilation features, there is no need for a purely static language like C/C ++, which requires compilation before execution. JVM language is to support script execution, as long as compiled into class for JVM recognition, can be executed, after the execution of overheating (HotSpot) then compiled into machine code, so as to improve the development and operation efficiency.
Application scenario of code heat replacement solution
- Code replacement or bugFix for stateful services
Because the application holds user information or context information, restarting the application will result in data loss in memory, so code fever is more suitable for this type of service.
For example, if a service is provided to communicate with the user, some context information of the user’s conversation is stored in the memory (it costs extra to store in NoSQL). If a bugFix is made but the code is not heated, the service will have to be stopped and restarted, resulting in the service being unavailable for a period of time and the user context information will be lost.
- Configuration or business code hotfix
ShowCode
- Java
A Javac compiler or other compiler that supports compiling Java is required. The following are the open source tools that encapsulate Javac and their implementation hotfixes
JOOR
OpenHFT Java Runtime Compiler
The brief flow chart is as follows
import net.openhft.compiler.CompilerUtils;
import org.joor.Reflect;
import java.util.concurrent.TimeUnit;
public class TestCompiler {
// singleton, runnable with volatile modifier, requiring other threads (for example, threads listening for script changes) to replace the reference
public static TestCompiler testCompiler = new TestCompiler();
// Assume business code
public volatile Runnable bizRunnable = () -> System.out.println(this.getClass().getSimpleName());
static String className = "com.eahau.example.compiler.TestCompilerRunnable";
static String sourceCode = "package com.eahau.example.compiler; \n" +
"\n" +
"public class TestCompilerRunnable implements Runnable {\n" +
" @Override\n" +
" public void run() {\n" +
" TestCompiler.testCompiler.bizRunnable = () -> System.out.println(this.getClass().getSimpleName()); \n" +
" }\n" +
"}";
/** * testJOOR. */
static void testJoor(a) {
Runnable runnable = Reflect.compile(className, sourceCode).create().get();
runnable.run();
}
/** * test JRC: Java Runtime Compiler. */
static void testJRC(a) throws Exception { Class<? > aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, sourceCode); Object o = aClass.newInstance();if (o instanceofRunnable) { ((Runnable) o).run(); }}static void bizStart(a) {
// The simulated business thread has been processing business
new Thread(() -> {
for(; ;) {try {
testCompiler.bizRunnable.run();
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException ignore) {
}
}
}).start();
}
public static void main(String[] args) throws Exception { bizStart(); testJoor(); testJRC(); }}Copy the code
- Groovy
Groovy requires a GroovyClassLoader to load, and Groovy relies on maven repositories
import groovy.lang.GroovyClassLoader;
import java.util.concurrent.TimeUnit;
public class TestGroovy {
public static TestGroovy testGroovy = new TestGroovy();
public volatile Runnable bizRunnable = () -> System.out.println(this.getClass().getSimpleName());
static void bizStart(a) {
// The simulated business thread has been processing business
new Thread(() -> {
for(; ;) {try {
testGroovy.bizRunnable.run();
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException ignore) {
}
}
}).start();
}
static void testGroovyCompiler(a) throws Exception {
GroovyClassLoader groovyClassLoader = newGroovyClassLoader(); Class<? > parsedClass = groovyClassLoader.parseClass("package com.eahau.example.compiler\n" +
"\n" +
"class TestGroovyRunnable implements Runnable {\n" +
" @Override\n" +
" void run() {\n" +
" TestGroovy.testGroovy.bizRunnable = { System.out.println(this.getClass().simpleName) }\n" +
" }\n" +
"}");
Object o = parsedClass.newInstance();
if (o instanceofRunnable) { ((Runnable) o).run(); }}public static void main(String[] args) throws Exception { bizStart(); testGroovyCompiler(); }}Copy the code
Write relatively simple, welcome to add comments. Please point out any errors.