Dynamic code hot replacement method based on Java and related device

By generating module metadata and using the Java class loader mechanism to customize the hierarchical class loader, combined with an automatic rollback mechanism, the problem of needing to restart after modifying a Java program is solved. This achieves efficient and stable dynamic hot replacement of Java code, improving development efficiency and application stability.

CN120973399APending Publication Date: 2025-11-18YANCHI ZHONGYING CHUANGNENG NEW ENERGY CO LTD +2
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511060670.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-30
Publication Date
2025-11-18

AI Technical Summary

Technical Problem

Modifications to existing Java programs require an application restart to take effect, leading to service interruptions and impacting user experience. Furthermore, the JVM's built-in hot-swap functionality is limited and cannot meet the needs of dynamic code updates.

Method used

By generating module metadata and using the Java class loader mechanism to customize the hierarchical class loader, combined with an automatic rollback mechanism, code replacement can be achieved without restarting the application, only replacing and modifying a portion of the code, and rolling back to a stable version in case of exceptions.

Benefits of technology

It enables efficient and stable dynamic hot replacement of Java code, avoiding service interruptions and improving software development efficiency and application stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120973399A_ABST
    Figure CN120973399A_ABST
Patent Text Reader

Abstract

The invention discloses a Java-based dynamic code hot replacement method and a related device, and the method comprises the steps: in a CI / CD stage, generating metadata of each module, packaging the metadata of each module, and transmitting the packaged metadata; and the hot replacement engine receives the metadata of each module and performs code replacement operation according to the metadata of each module, and the method and the related device can realize efficient and stable dynamic hot replacement of Java codes.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application belongs to the technical field of computer software development, and relates to a Java-based dynamic code hot replacement method and a related device. BACKGROUND

[0002] In the traditional software development process, when the code of a Java program is modified, the entire application program usually needs to be restarted to make the modification take effect. Whether it is a Web system or a desktop program, this "shutdown and restart" method not only consumes time, but also may cause service interruption during the running of the application program, affecting user experience. For example, in some business scenarios with high real-time requirements, such as online transaction systems, financial data processing systems, etc., service interruption may cause serious economic losses.

[0003] Although the Java Virtual Machine (JVM) itself supports a certain degree of class replacement, the built-in hot replacement function is very limited and can only make some simple modifications to the method body. The key parts such as method signature cannot be changed, which is far from meeting the actual development needs of dynamic code updating. Therefore, it is of great practical significance to develop a system and method that can efficiently and stably perform dynamic hot replacement of Java code without restarting the application. SUMMARY

[0004] The application aims to overcome the shortcomings of the prior art and provides a Java-based dynamic code hot replacement method and a related device, which can efficiently and stably perform dynamic hot replacement of Java code.

[0005] To achieve the above-mentioned purpose, the application discloses a Java-based dynamic code hot replacement method, which comprises:

[0006] In the CI / CD stage, the metadata of each module is generated, and the metadata of each module is packaged and sent out;

[0007] The hot replacement engine receives the metadata of each module and performs code replacement operation according to the metadata of each module.

[0008] The Java-based dynamic code hot replacement method further improves in that:

[0009] Further, the process in which the hot replacement engine receives the metadata of each module and performs code replacement operation according to the metadata of each module is:

[0010] The hot replacement engine receives the metadata of each module;

[0011] The hot replacement engine utilizes a class loader mechanism of Java to customize a hierarchical class loader;

[0012] The hierarchical class loader is utilized to perform code replacement operation according to the metadata of each module.

[0013] Further, the application further comprises:

[0014] An automatic rollback mechanism is constructed, and the replacement operation is performed through the automatic rollback mechanism.

[0015] Further, the process of constructing the automatic rollback mechanism and performing the replacement operation through the automatic rollback mechanism is as follows:

[0016] The automatic rollback mechanism is constructed, and the current code package is automatically backed up before the code replacement operation according to the metadata of each module; when an exception is detected during the code replacement operation according to the metadata of each module, the replacement operation is withdrawn according to the automatic rollback mechanism, and the previous stable version is rolled back.

[0017] The application discloses a dynamic code hot replacement system based on Java, which comprises:

[0018] The sending module is used for generating metadata of each module in a CI / CD stage, and the metadata of each module is packaged and sent out.

[0019] The replacement module is used for receiving the metadata of each module by the hot replacement engine, and performing code replacement operation according to the metadata of each module.

[0020] The dynamic code hot replacement system based on Java further improves in that:

[0021] Further, the process of receiving the metadata of each module by the hot replacement engine and performing code replacement operation according to the metadata of each module is as follows:

[0022] The hot replacement engine receives the metadata of each module.

[0023] The hot replacement engine utilizes a class loader mechanism of Java to customize a hierarchical class loader;

[0024] The hierarchical class loader is utilized to perform code replacement operation according to the metadata of each module.

[0025] Further, the application further comprises:

[0026] An automatic rollback mechanism is constructed, and the replacement operation is performed through the automatic rollback mechanism.

[0027] Further, the process of constructing the automatic rollback mechanism and performing the replacement operation through the automatic rollback mechanism is as follows:

[0028] An automatic rollback mechanism is constructed, and before the code replacement operation according to the metadata of each module is performed, the current code package is automatically backed up, and when an exception is detected during the code replacement operation according to the metadata of each module, the rollback operation is performed according to the automatic rollback mechanism to roll back to the last stable version.

[0029] The application discloses a computer device, comprising a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the steps of the Java-based dynamic code hot replacement method when executing the computer program.

[0030] The application discloses a computer readable storage medium, which stores a computer program, and the computer program implements the steps of the Java-based dynamic code hot replacement method when executed by a processor.

[0031] The application has the following beneficial effects:

[0032] The Java-based dynamic code hot replacement method and related device have the following beneficial effects: in the CI / CD stage, metadata of each module is generated, and the metadata of each module is packaged and sent out, the hot replacement engine receives the metadata of each module, and performs code replacement operation according to the metadata of each module to realize Java code replacement.

[0033] Further, before the code replacement operation according to the metadata of each module is performed, the current code package is automatically backed up, and when an exception is detected during the code replacement operation according to the metadata of each module, the rollback operation is performed according to the automatic rollback mechanism to roll back to the last stable version, so that the problem of system error caused by the exception is avoided, and the efficiency and stability of the dynamic hot replacement are improved. BRIEF DESCRIPTION OF DRAWINGS

[0034] The accompanying drawings, which form a part of the present application, are used to provide further understanding of the present application, and the illustrative embodiments of the present application and their descriptions are used to explain the present application, and do not constitute improper limitations on the present application. In the drawings:

[0035] Figure 1 The method flowchart of the present application. DETAILED DESCRIPTION

[0036] The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the scope of protection of the present application.

[0037] In the description of the present application, it should be understood that the terms "comprising" and "including" indicate the presence of the described features, integers, steps, operations, elements, and / or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof.

[0038] It should also be understood that the terms used in the present specification and the appended claims are intended to be interpreted in their broadest possible manner, and are intended not to be limited to their most restrictive, or literal meanings. As used in the specification and appended claims, the singular forms "a," "an" and "the" are intended to include plural references unless the context clearly dictates otherwise.

[0039] It should further be understood that the term "and / or" as used in the specification and the appended claims, means any one or more of the associated listed items, as well as all possible combinations of the items, and includes the possibilities of A and / or B, where A and B are both present, A and / or B, where A is present alone, and B and / or A, where B is present alone. In addition, the character " / " in the present application generally represents a "or" relationship between the objects before and after it.

[0040] It should be understood that, although the terms first, second, third, etc. can be used herein to describe various ranges or the like, these ranges are not to be limited by these terms. These terms are only used to distinguish one range from another. For example, a first range could be termed a second range without departing from the scope of the example embodiments.

[0041] Depending on the context, the word "if" as used herein can be interpreted to mean "when" or "while" or "in response to the determination" or "in response to the occurrence." Similarly, the phrase "if determined" or "if [a stated condition or event] is detected" can be interpreted to mean "upon being determined," or "in response to determining," or "upon detecting [the stated condition or event]," or "in response to detecting [the stated condition or event]."

[0042] In order to make the purposes, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, rather than all the embodiments. The components of the embodiments of the present application described and shown in the drawings herein can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the claimed present application, but only represents selected embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work are within the scope of protection of the present application.

[0043] Various structural schematic diagrams according to the disclosed embodiments of the present application are shown in the drawings. These diagrams are not drawn to scale, in which some details are exaggerated for the purpose of clear expression, and some details can be omitted. The shapes of various regions, layers and their relative sizes and positional relationships shown in the drawings are only exemplary, and in actuality, there can be deviations due to manufacturing tolerances or technical limitations, and regions / layers with different shapes, sizes and relative positions can be additionally designed by those skilled in the art according to actual needs.

[0044] Embodiment one

[0045] With reference to Figure 1 The Java-based dynamic code hot replacement method described in the present application includes the following steps:

[0046] 1) In the CI / CD stage, the metadata of each module is generated, and the metadata of each module is packaged and sent out, wherein the dependency relationship between each module and the classes that are modified are recorded, so that the metadata of each module is generated in the CI / CD stage.

[0047] 2) The hot replacement engine receives the metadata of each module and performs code replacement operation according to the metadata of each module. Wherein the hot replacement engine utilizes the class loader mechanism of Java to customize a hierarchical class loader, which mainly undertakes two key tasks: one is to isolate the namespace of old classes and new classes to prevent replacement confusion caused by class name conflicts; the other is to load only the classes that have been changed, and reuse the classes that have not been modified to improve the replacement efficiency.

[0048] 3) Considering that abnormal situations may occur during the dynamic code hot replacement process, the present application designs an automatic rollback mechanism, which automatically backs up the current code package before each code replacement operation. When an exception is detected during the replacement process, the system can perform a rollback operation to return to the previous stable version.

[0049] 4) To realize the automation of code changes, the application is constructed with a CI / CD module. The CI / CD module is integrated with common continuous integration / continuous deployment tools (such as Jenkins, GitLab CI, etc.) on the one hand, facilitating the compilation, testing and deployment of the code; on the other hand, after the code is submitted, it can automatically trigger the generation of module metadata and start the hot replacement process.

[0050] 5) Due to the subtle differences in class loading mechanism, memory model, etc. of different versions of JVM, in order to enable the application to support various versions of JVM, the application is designed with a JVM compatibility module. The module uses reflection and bytecode tools (such as ASM, Javassist) to dynamically handle the differences between different JVM versions.

[0051] It should be noted that the application solves the problem in the prior art that the application needs to be restarted after the code is modified to make the modification effective, and the JVM's built-in hot replacement function is limited, realizes updating the code logic without restarting the application, and can perform incremental replacement, replacing only the modified code part, thereby improving the software development efficiency and the stability of the application.

[0052] Embodiment Two

[0053] The Java-based dynamic code hot replacement system provided by the application comprises:

[0054] A sending module is configured to generate metadata of each module in the CI / CD stage, and send the metadata of each module after being packaged.

[0055] A replacement module is configured to receive the metadata of each module by the hot replacement engine, and perform a code replacement operation according to the metadata of each module.

[0056] In this embodiment, the process that the hot replacement engine receives the metadata of each module and performs a code replacement operation according to the metadata of each module is as follows:

[0057] The hot replacement engine receives the metadata of each module.

[0058] The hot replacement engine uses the class loader mechanism of Java to customize a hierarchical class loader.

[0059] The hierarchical class loader is used to perform a code replacement operation according to the metadata of each module.

[0060] In this embodiment, the application further comprises:

[0061] An automatic rollback mechanism is constructed, and the automatic rollback mechanism is used to perform a replacement operation.

[0062] In the embodiment, the process of performing the replacement operation by the automatic rollback mechanism is as follows:

[0063] The automatic rollback mechanism is constructed, and before the code replacement operation is performed according to the metadata of each module, the current code package is automatically backed up. During the code replacement operation process of the metadata of each module, when an exception is detected, the replacement operation is withdrawn according to the automatic rollback mechanism, and rolled back to the last stable version.

[0064] The division of the modules in the embodiments of the present application is illustrative, and is only a logical functional division. In actual implementation, another division manner can be used. In addition, each functional module in each embodiment of the present application can be integrated in one processor, or can be physically separated, or two or more modules can be integrated in one module. The integrated module can be realized in the form of hardware or in the form of a software functional module.

[0065] Embodiment three

[0066] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the Java-based dynamic code hot replacement method are implemented, for example, including: in the CI / CD stage, generating metadata of each module, and sending the metadata of each module after being packaged; the hot replacement engine receives the metadata of each module, and performs a code replacement operation according to the metadata of each module; the process of the hot replacement engine receiving the metadata of each module and performing a code replacement operation according to the metadata of each module is as follows: the hot replacement engine receives the metadata of each module; the hot replacement engine uses the class loader mechanism of Java to customize a hierarchical class loader; the hierarchical class loader is used to perform a code replacement operation according to the metadata of each module. The memory can include a memory, for example, a high-speed random access memory, and can also include a non-volatile memory, for example, at least one disk memory; the processor, the network interface, and the memory are connected to each other through an internal bus, which can be an industry standard architecture bus, a peripheral component interconnect standard bus, an extended industry standard structure bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. The memory is used to store programs, specifically, the programs can include program codes, and the program codes include computer operation instructions. The memory can include a memory and a non-volatile memory, and provides instructions and data to the processor.

[0067] Embodiment four

[0068] The computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement steps of the Java-based dynamic code hot replacement method, for example, including: in a CI / CD stage, generating metadata of each module, and sending the metadata of each module after being packaged; the hot replacement engine receives the metadata of each module, and performs a code replacement operation according to the metadata of each module; the process that the hot replacement engine receives the metadata of each module and performs the code replacement operation according to the metadata of each module is: the hot replacement engine receives the metadata of each module; the hot replacement engine uses a class loader mechanism of Java to customize a hierarchical class loader; and the hierarchical class loader is used to perform the code replacement operation according to the metadata of each module. Specifically, the computer readable storage medium includes but is not limited to, for example, volatile memory and / or non-volatile memory. The volatile memory can include random access memory (RAM) and / or cache memory, etc. The non-volatile memory can include read-only memory (ROM), hard disk, flash memory, optical disc, magnetic disc, etc.

[0069] Those skilled in the art will appreciate that embodiments of the application can be supplied as a method, a system, or a computer program product. Therefore, the application can take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the application can take the form of a computer program product implemented on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROMs, optical storage, etc.) containing computer usable program code.

[0070] The application is described with reference to flowcharts and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, as well as combinations of flows and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing apparatus to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing apparatus produce an apparatus that implements the functions specified in the flowcharts and / or block diagrams. Figure 1 The functions specified in a flow or multiple flows and / or blocks Figure 1 The apparatus that implements the functions specified in a flow or multiple flows and / or blocks.

[0071] These computer program instructions can also be stored in a computer readable storage medium that can direct the computer or other programmable data processing apparatus to work in a specific manner, so that the instructions stored in the computer readable storage medium produce a product including instruction apparatus, which implements the functions specified in the flowcharts and / or block diagrams. Figure 1one or more processes and / or blocks Figure 1 the function specified in the one or more blocks.

[0072] These computer program instructions can also be loaded into computer or other programmable data processing devices, so that a series of operation steps are performed on the computer or other programmable data processing devices to generate computer-implemented processes, thus the instructions executed on the computer or other programmable data processing devices provide processes for implementing the flow Figure 1 one or more processes and / or blocks Figure 1 the function specified in the one or more blocks.

[0073] Other embodiments of the present application will be apparent to those skilled in the art from consideration of the specification and practice of the application disclosed herein. It is intended that the present application cover any and all variations of the present application which come within the scope of the present application. It is intended that the specification and examples be considered exemplary only, with the true scope and spirit of the application being indicated by the following claims.

[0074] It is to be understood that the application is not limited to the precise construction described and as shown in the attached drawings, and that various modifications and changes can be made by those skilled in the art without departing from the scope of the present application. The scope of the application is limited only by the claims that follow.

[0075] The above description is only preferred embodiments of the present application, not any limitation thereto, any simple modification, change and equivalent structural change made according to the technical essence of the present application to the above embodiments are still within the protection scope of the present application.

Claims

1. A Java-based dynamic code hot-swapping method, characterized in that, include: During the CI / CD phase, metadata for each module is generated, and the metadata for each module is packaged and sent out. The hot-swap engine receives the metadata of each module and performs code replacement operations based on the metadata of each module.

2. The Java-based dynamic code hot-swapping method according to claim 1, characterized in that, The hot-swap engine receives the metadata of each module and performs code replacement operations based on the metadata of each module as follows: The hot-swap engine receives metadata from each module; The hot-swapping engine utilizes Java's class loader mechanism to define a custom hierarchical class loader; The hierarchical class loader is used to perform code replacement operations based on the metadata of each module.

3. The Java-based dynamic code hot-swapping method according to claim 1, characterized in that, Also includes: Build an automatic rollback mechanism to perform replacement operations.

4. The Java-based dynamic code hot-swapping method according to claim 3, characterized in that, The process of constructing an automatic rollback mechanism and performing replacement operations through the automatic rollback mechanism is as follows: An automatic rollback mechanism is built. Before performing code replacement operations based on the metadata of each module, the current code package is automatically backed up. During the code replacement operation based on the metadata of each module, if an anomaly is detected, the automatic rollback mechanism is used to perform a rollback operation to revert to the previous stable version.

5. A Java-based dynamic code hot-swap system, characterized in that, include: The sending module is used in the CI / CD stage to generate metadata for each module, package the metadata of each module, and send it out. The replacement module is used by the hot replacement engine to receive the metadata of each module and perform code replacement operations based on the metadata of each module.

6. The Java-based dynamic code hot-swapping system according to claim 5, characterized in that, The hot-swap engine receives the metadata of each module and performs code replacement operations based on the metadata of each module as follows: The hot-swap engine receives metadata from each module; The hot-swapping engine utilizes Java's class loader mechanism to define a custom hierarchical class loader; The hierarchical class loader is used to perform code replacement operations based on the metadata of each module.

7. The Java-based dynamic code hot-swapping system according to claim 5, characterized in that, Also includes: Build an automatic rollback mechanism to perform replacement operations.

8. The Java-based dynamic code hot-swapping system according to claim 7, characterized in that, The process of constructing an automatic rollback mechanism and performing replacement operations through the automatic rollback mechanism is as follows: An automatic rollback mechanism is built. Before performing code replacement operations based on the metadata of each module, the current code package is automatically backed up. During the code replacement operation based on the metadata of each module, if an anomaly is detected, the automatic rollback mechanism is used to perform a rollback operation to revert to the previous stable version.

9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the Java-based dynamic code hot-swap method as described in any one of claims 1-4.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by the processor, it implements the steps of the Java-based dynamic code hot-swap method as described in any one of claims 1-4.