Java Thermal Deployment Principles

Source: Internet
Author: User

1. What is hot deployment?

For Java applications, a hot deployment is a Java class file that is updated at run time. 2. What is the use of thermal deployment? The app can be updated without restarting the app. For example, a computer can replace a USB flash drive without restarting it. OSGi is also popular because of its modularity and thermal deployment. 3. What is the principle of thermal deployment? To know the principle of hot deployment, you must understand the Java class loading process. A Java class file to the virtual machine object, go through the following procedure. First, through the Java compiler, the Java file is compiled into a class byte code, the class loader reads the class bytecode, and then converts the classes into instances, the instance newinstance can be generated objects. The class loader ClassLoader function, which is the conversion of a class bytecode to an instance of a class. In a Java application, all instances are loaded by the ClassLoader. Generally in the system, classes are loaded by the system's own ClassLoader, and for the same fully qualified Java class (such as Com.csiar.soc.HelloWorld), it can only be loaded once and cannot be unloaded.     That's when the problem comes, what if we want to unload the Java class and replace the updated version of the Java class? Since in the ClassLoader, the Java class can only be loaded once and cannot be uninstalled. Is it possible to change the ClassLoader directly? The answer is yes, we can customize the ClassLoader and rewrite the ClassLoader Findclass method. To achieve a hot deployment, you can divide the following three steps: 1, destroy the custom ClassLoader2, update class file 3, create a new ClassLoader to load the updated class class file. The sample code is as follows:
1  PackageCom.csair.soc.hotswap;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 /**6 * Customize the ClassLoader and override the Findclass method7  */8  Public classMyclassloaderextendsclassloader{9 @OverrideTen       PublicClass<?> Findclass (String name)throwsclassnotfoundexception{ One             Try{ AString fileName = name.substring (Name.lastindexof (".") +1) + ". Class" ; -InputStream is = This. GetClass (). getResourceAsStream (fileName); -                  byte[] B =New byte[Is.available ()]; the Is.read (b); -                  returnDefineClass (name, B, 0, B. length); -}Catch(IOException e) { -                  Throw Newclassnotfoundexception (name); +            } -      } +}
class files that need to be updated:
1  Package Com.csair.soc.hotswap; 2  Public class HelloWorld {3       Public void say () {4            System. Out.println ("Hello World V1"); 5      }6 }
Under the project root directory, generate the V2 version of Helloworld.class, as follows.
1  Package Com.csair.soc.hotswap; 2  Public class HelloWorld {3        Public void say () {4            System. Out.println ("Hello World V2"); 5      }6 }
Test the main program
1  PackageCom.csair.soc.hotswap;2 3 ImportJava.io.File;4 ImportJava.lang.reflect.Method;5 6  Public classHotSwap {7       Public Static voidMain (string[] args)throwsException {8 Loadhelloworld ();9             //Reclaim resources, release Helloworld.class files so that they can be replacedTen System. GC (); OneThread. Sleep (1000);//waiting for resources to be recycled AFile fileV2 =NewFile ("Helloworld.class"); -File fileV1 =NewFile ( -"Bin\\com\\csair\\soc\\hotswap\\helloworld.class" ); theFilev1.delete ();//Remove V1 version -Filev2.renameto (fileV1);//Update V2 version -System. Out.println ("Update success!"); - Loadhelloworld (); +      } -  +       Public Static voidLoadhelloworld ()throwsException { AMyclassloader Myloader =NewMyclassloader ();//Custom class Loaders atclass<?> Class1 =Myloader -. Findclass ("Com.csair.soc.hotswap.HelloWorld");//class Instance -Object obj1 = Class1.newinstance ();//to create a new object -method = Class1.getmethod ("Say"); -Method.invoke (OBJ1);//execution method say -System. Out.println (Obj1.getclass ());//Object inSystem. Out.println (Obj1.getclass (). getClassLoader ());//class loader for object -      } to}
Output Result:Hello World V1class Com.csair.soc.hotswap.HelloWorld[email protected]Update success!Hello World V2class Com.csair.soc.hotswap.HelloWorld[email protected]as a result, you can see that the HelloWorld class was updated successfully without restarting the application. The above is just the simplest principle of thermal deployment practice, the actual situation will be more complex. The most important concept of OSGi is the Application module (bundle), for each bundle, has its own classloader, when the need to update the bundle, the bundle and its class loader replace, you can implement the module's hot replacement.
Resources for in-depth understanding of Java virtual machines in-depth discussion Java ClassLoader http://www.ibm.com/developerworks/cn/java/j-lo-classloader/

Java Thermal Deployment Principles

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.