Use spring-loaded open source projects to implement hot deployment of Java programs and Web applications

Source: Internet
Author: User

After JDK1.5, Java.lang.instrument.Instrumentation is provided, that is, the Java Agent mechanism can implement redefinition and retransform of classes.redefinition corresponds to instrumentation.redefineclasses () to enable hot substitution of classes, but unfortunately the function is limited.
1 2 3 4 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 is lifted 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. ?
1 2 3 4 5 6 7 Spring Loaded is a JVM agentforreloadingclassfile 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), Spring Loa Ded 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 values I Nenumtypes.

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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Packagetest; Importdemo.   A Publicclasstestpremain {//-javaagent:springloaded-1.2.0.release.jar-noverify Publicstaticvoidmain (String[] ar           GS) Throwsexception {A A =newa ();             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?
1 -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. Will beDownloadSpringloaded-1.2.0.release.jar into the%tomcat_home%/bin/directory, and then modify the Catalina.bat in that directory?
1 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.