Concept, design and implementation of hot replacement in Java classes (1)

Source: Internet
Author: User

    Hot replacement of classes is the basic technology in the design of the Java online upgrade system. From the examples given in this article, building an online upgrade system is not only a technical problem, but also involves many management factors, for example, how to manage and deploy the online and non-online updates in the system to reduce system management and maintenance costs.

    For many key businesses or a large Java System, if system services must be suspended for system upgrade, the system availability will be greatly affected, and the system management and maintenance costs will also be increased. Therefore, if you can easily upgrade the system without stopping the system service, the above problems can be well solved.

    Javaclassloader Technology Analysis

    To build an online upgrade system, an important technology is to implement hot replacement of Java classes-that is, to upgrade and replace classes (objects) without stopping running systems. Java classloader is the basis for implementing this technology.

    In Java, the class instantiation process is divided into two parts: class loading and class instantiation. Class loading is divided into explicit loading and implicit loading. When you use the new keyword to create a class instance, it actually implicitly contains the class loading process. PairFor explicit loading of classes, class. forname is commonly used. In fact, they all call the loadclass method of the classloader class to complete the actual loading of the class. Directly calling the loadclass method of classloader is another uncommon explicit loading technology.

    Figure 1. Java class loader hierarchy

    Classloader has certain levels and rules when loading classes. In Java, there are four types of class loaders: bootstrapclassloader, extclassloader, appclassloader, and user-defined classloader. The four loaders are responsible for loading classes of different paths and form a class loading hierarchy.

    Bootstrapclassloader is at the top of the class loader hierarchy and is responsible for sun. boot. class. class loading in the path. The default value is the jar package specified by the core API under the JRE/lib directory or the-xbootclasspath option. Extclassloader is loaded in the Java. Ext. dirs directory. The default value is the jar package under the JRE/lib/EXT directory or the directory specified by-djava. Ext. dirs. The loading path of appclassloader is Java. Class. path. The default value is the value set in the environment variable classpath. You can also specify the classpath type. You can customize classloader to customize the class loading process based on your needs, and perform dynamic and real-time loading of specified classes at runtime.

    The hierarchical relationship diagram 1 shows the four types of loaders. Generally, these four types of loaders form a parent-child relationship, and the top layer is the parent loader of the lower layer. During class loading, the system first checks from the bottom up whether the specified class has been loaded. If the specified class has been loaded, the system directly returns the reference of the class. If the specified class has not been loaded to the highest level, it will try to load from top to bottom until the User-Defined Class Loader fails. If it still fails, an exception will be thrown. Java class loading process 2 is shown.

    Figure 2. Java class addition Process

    Each class loader has its own namespace. For the same class loader instance, only one class with the same name can exist and can be loaded only once. No matter whether the class is changed or not, the next time it needs to be loaded, it simply returns the loaded class reference from its own cache.

    By default, the application classes we write are loaded through appclassloader. When we use the new keyword or class. forname to load a class, the class to be loaded is loaded by the class loader that calls the new or class. forname class (which is also appclassloader. To implement hot replacement of Java classes, you must first implement the coexistence of instances of different versions of classes with the same name in the system. We can see from the above introduction that, to realize the coexistence of different versions of the same class, we must use different class loaders to load different versions of the class. In addition, in order to bypass the established loading process of Java classes, we need to implement our own class loaders and fully control and manage the loading process of classes.

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.