after JDK1.5, Java.lang.instrument.Instrumentation is provided, that is, the Java Agent mechanism can implement redefinition and retransform of classes. redefinition Correspondenceinstrumentation.redefineclasses () can implement hot replacements for classes, but unfortunately the functionality is limited.
The redefinition change method bodies, the constant pool and attributes. The redefinition must not add, remove or rename fields or methods, change the signatures of methods, or change inheritance . These restrictions maybe belifted in the future versions.
recently encountered an open source project spring-loaded, looked at the official introduction document: found that it features more powerful than the JDK.
Spring Loaded is a JVM agent for reloading class file changes whilst a JVM is running. It transforms classes at Loadtime to make them amenable to later reloading. Unlike ' Hot code replace ' which only allows simple changes once a JVM was running (e.g. changes to method bodies), Spri Ng Loaded allows you to add/modify/delete methods/fields/constructors. The annotations on Types/methods/fields/constructors can also be modified and it's possible to Add/remove/change Valu Es in enum types.
through your own attempts to discover the use of the spring-loaded project, you can actually implement a hot deployment of Java applications. The following describes how to introduce spring-loaded into a project. We can run the following code and then modify the A.say () method to see if it can be changed dynamically without restarting the JVM.
Package Test;import Demo. A;public class testpremain{//-javaagent:springloaded-1.2.0.release.jar-noverifypublic static void Main (String[] args ) throws Exception{a A = new A (); while (true) {A.say (); Thread.Sleep (3000);}}
In order to use spring-loaded for thermal deployment, we only need to add the following startup parameters when starting the JVM
-javaagent:springloaded-1.2.0.release.jar-noverify
If you are booting through eclipse, you can set it in run Confiuration
Next we look at how to use spring-loaded in Tomcat to implement a hot deployment of a war package. Place the downloaded Springloaded-1.2.0.release.jar in the%tomcat_home%/bin/directory, and then modify the Catalina.bat in the directory
Set Java_opts=-javaagent:springloaded-1.2.0.release.jar-noverify
This completes the installation of the spring-loaded, detects the webapp deployed under Tomcat, and implements the hot deployment of the application without restarting Tomcat.
Use spring-loaded open source projects to implement hot deployment of Java programs and Web applications