Dynamic execution method and system of Java class, terminal equipment and storage medium

By uploading the Java class to be executed in a Java application and loading dynamically with a custom class loader, and setting the execution time interval in combination with the task scheduler, the problem of republishing traditional Java applications when facing business rules changes is solved. , achieves the effect of quickly responding to business changes and improving system flexibility.

CN119396488AInactive Publication Date: 2025-02-07GONGQINGCHENG ZHONGTAOLIAN SUPPLY CHAIN SERVICE CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411454777.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-17
Publication Date
2025-02-07
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

When traditional Java applications face changes in business rules, they need to republish the entire application, which makes it time-consuming and easy to introduce errors, affecting the stability and availability of the system.

Method used

A dynamic execution method of Java class is proposed. By uploading the Java class to be executed to the file server, and dynamically loading it using a custom class loader, and setting the execution time interval with the task scheduler, it realizes dynamic loading and execution of Java classes.

Benefits of technology

It realizes rapid response to business changes without restarting or redeploying of applications, improves the system's ability to respond to market changes, and enhances the flexibility and efficiency of business operations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119396488A_ABST
    Figure CN119396488A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of Java class execution, and provides a Java class dynamic execution method and system, terminal equipment and a storage medium, and the method comprises the steps: uploading a Java class to be executed to a file server; creating a task scheduler for the Java class to be executed, wherein an execution time interval of the Java class and a name of the Java class to be executed are configured in the task scheduler; after each execution time interval, the task scheduler calls the self-defined class loader according to the Java class name, creates an instance corresponding to the Java class in the file server and a reflection object of a method in the Java class to be executed, and executes the method in the Java class to be executed according to the instance and the reflection object, so that a new Java class can be uploaded at any time, and a timed task is set; and the whole application does not need to be released again, so that the response capability to business change is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of Java class execution, and in particular to a Java class dynamic execution method, system, terminal equipment and storage medium. Background Art

[0002] In the current software development environment, Java backend services are widely used in various business scenarios, especially in applications such as e-commerce platforms that need to flexibly respond to market changes. Traditional Java applications usually need to re-release the entire application when faced with changes in business rules. This process is time-consuming and prone to errors, affecting the stability and availability of the system.

[0003] For example, in promotion management, the promotion rules for different brands and products may change frequently, requiring the system to respond quickly to these changes. However, existing scheduled task scheduling systems usually rely on static configurations, making it difficult to dynamically load new business logic, resulting in the need to redeploy applications to adapt to new promotion rules. This model limits the ability of e-commerce platforms to quickly adapt to market changes and reduces the flexibility and efficiency of business operations. Therefore, how to achieve dynamic loading and execution of Java classes, especially without restarting or redeploying applications, has become an urgent problem to be solved. Summary of the invention

[0004] In view of the above-mentioned defects, the purpose of the present invention is to propose a dynamic execution method, system, terminal device and storage medium of Java classes, which can upload new Java classes and set scheduled tasks at any time without republishing the entire application, thereby improving the responsiveness to business changes.

[0005] To achieve this object, the present invention adopts the following technical solutions:

[0006] A dynamic execution method of a Java class, comprising:

[0007] Upload the Java class to be executed to the file server;

[0008] Creating a task scheduler for the Java class to be executed, wherein the task scheduler is configured with the execution time interval of the Java class and the name of the Java class to be executed;

[0009] After each execution time interval, the task scheduler calls the custom class loader according to the Java class name and creates an instance corresponding to the Java class in the file server and a reflection object of the method in the Java class to be executed, and executes the method in the Java class to be executed according to the instance and the reflection object.

[0010] Preferably, setting a task scheduler for a Java class to be executed includes:

[0011] Get the task scheduler instance;

[0012] Creating a task for the Java class to be executed, and setting a unique task identifier and a first task group, wherein the task identifier is used to avoid duplicate task creation, and the first task group is used to classify and manage tasks;

[0013] Create a task trigger, and set a unique trigger identifier, a second task group, and an execution time interval for the task trigger, wherein the trigger identifier is used to avoid repeated creation of the trigger, and the second task group is used to classify and manage the task triggers;

[0014] A task scheduler instance associates tasks with task triggers.

[0015] Preferably, the first task group and the second task group are the same group.

[0016] Preferably, calling the custom class loader according to the Java class name and the access address and creating an instance corresponding to the Java class includes:

[0017] Convert the Java class name to be executed into a URL address;

[0018] Access the file server according to the URL address to obtain the Java class information to be executed;

[0019] Read the Java class information through the input stream, and then write the Java class information into the byte array through the output stream;

[0020] Convert a byte array to an instance of the corresponding Java class.

[0021] Preferably, the method further includes manually controlling the task scheduler to stop task scheduling and thereby stop executing the method in the Java class to be executed.

[0022] Preferably, the execution time interval is set by a Cron expression.

[0023] A Java class dynamic execution system, the Java class dynamic execution method as described above, comprises:

[0024] Information upload module, used to upload the Java class to be executed to the file server;

[0025] A scheduler creation module is used to create a task scheduler for the Java class to be executed, wherein the task scheduler is configured with the execution time interval of the Java class and the name of the Java class to be executed;

[0026] The method execution module is used for, after each execution time interval, the task scheduler calls the subclass loader according to the Java class name and creates an instance corresponding to the Java class in the file server and a reflection object of the method in the Java class to be executed, and executes the method in the Java class to be executed according to the instance and the reflection object.

[0027] A terminal device comprises: a memory, a processor and a program stored in the memory and executable on the processor, wherein the program is configured to implement the steps of the dynamic execution method of a Java class as described above.

[0028] A storage medium stores a dynamic execution program of a Java class, and when the dynamic execution program of the Java class is executed by a processor, the steps of the dynamic execution method of the Java class described above are implemented.

[0029] One of the above technical solutions has the following advantages or beneficial effects:

[0030] By uploading the Java class to be executed to the file server and dynamically loading it using a custom class loader, new functions can be flexibly introduced at runtime, allowing the system to quickly expand or replace functional modules without redeployment. The task scheduler is used to set the execution time interval, so that the system can automatically execute tasks on demand. With the help of Cron expressions, users can easily set complex scheduling rules to meet the needs of various time arrangements, enhancing the operability of the system. By calling the methods in the class to be executed through the reflection mechanism, the specified method can be flexibly executed at runtime, supporting the dynamic selection and execution of different methods, and is suitable for a variety of application scenarios. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.

[0032] Figure 1 is a flow chart of a dynamic execution method of a Java class provided by an embodiment of the present invention;

[0033] Figure 2 It is a schematic diagram of the structure of a dynamic execution system of a Java class provided by an embodiment of the present invention;

[0034] Figure 3 It is an overall flow chart of the dynamic execution method of a Java class provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0035] Embodiments of the present invention are described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and cannot be understood as limiting the present invention.

[0036] In this application, the terms "comprises", "comprising" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprising a ..." does not exclude the presence of other identical elements in the process, method, article or device comprising the element.

[0037] In the current software development environment, Java backend services are widely used in various business scenarios, especially in applications such as e-commerce platforms that need to flexibly respond to market changes. Traditional Java applications usually need to re-release the entire application when faced with changes in business rules. This process is time-consuming and prone to errors, affecting the stability and availability of the system.

[0038] For example, in promotion management, the promotion rules for different brands and products may change frequently, requiring the system to respond quickly to these changes. However, existing scheduled task scheduling systems usually rely on static configurations, making it difficult to dynamically load new business logic, resulting in the need to redeploy applications to adapt to new promotion rules. This model limits the ability of e-commerce platforms to quickly adapt to market changes and reduces the flexibility and efficiency of business operations. Therefore, how to achieve dynamic loading and execution of Java classes, especially without restarting or redeploying applications, has become an urgent problem to be solved.

[0039] Therefore, a dynamic execution method of Java class is proposed, such as Figure 1 and Figure 3 As shown, a preferred embodiment of the present invention comprises the steps of:

[0040] S1: Upload the Java class to be executed to the file server;

[0041] S2: Creating a task scheduler for the Java class to be executed, wherein the task scheduler is configured with the execution time interval of the Java class and the name of the Java class to be executed;

[0042] S3: After each execution time interval, the task scheduler calls the custom class loader according to the Java class name and creates an instance corresponding to the Java class in the file server and a reflection object of the method in the Java class to be executed, and executes the method in the Java class to be executed according to the instance and the reflection object.

[0043] In step S1, the Java class file is uploaded to the file server so that the code is stored in a central location and the application does not need to hold a local copy of all classes, which facilitates version control and updates. The local file is transferred to the remote server through a network protocol (such as HTTP or FTP). The file server can be a dedicated file storage service or cloud storage, allowing different applications to access the updated code at any time. During the upload process, identity authentication and permission control can be implemented to protect the security and integrity of the file and prevent unauthorized access.

[0044] In step S2, the task scheduler is an independent component that is triggered at preset time intervals (such as every minute, every hour, etc.). It can be implemented using different libraries (such as Quartz or Spring Scheduling). Users can configure the task scheduler to specify the Java class to be executed, the execution frequency and other parameters, so that the execution logic of the task can be dynamically adjusted. The scheduler can run in an independent thread to ensure the smoothness of the main program and can handle multiple tasks concurrently.

[0045] In step S3, Java's class loading mechanism allows developers to create their own class loaders to control where classes are loaded from. Custom class loaders can dynamically load Java classes from a specified URL or file path, so that code loading no longer depends on static compilation. Java's reflection API provides a way to access class information at runtime. Through reflection, you can create instances of classes, call their methods, and access private properties, providing flexibility for dynamically executing methods in classes. After each set time interval, the scheduler uses a custom class loader to load the specified Java class, obtains the class method through reflection, and calls the method. Even if the logic of the Java class changes, the scheduler can execute the latest code in real time.

[0046] By uploading the Java class to be executed to the file server and using a custom class loader to dynamically load it, new functions can be flexibly introduced at runtime, allowing the system to quickly expand or replace functional modules without redeployment. The task scheduler is used to set the execution time interval, so that the system can automatically execute tasks on demand. Through the task scheduler, users can easily set complex scheduling rules to meet the needs of various time arrangements, enhancing the operability of the system. By calling the methods in the class to be executed through the reflection mechanism, the specified method can be flexibly executed at runtime, supporting the dynamic selection and execution of different methods, and is suitable for a variety of application scenarios.

[0047] Preferably, setting a task scheduler for a Java class to be executed includes:

[0048] Get the task scheduler instance;

[0049] Specifically, the task scheduler is responsible for the registration, management, and scheduling of tasks. A scheduler instance (Scheduler) can be created through the scheduler factory (SchedulerFactory), which will be used for the management of subsequent tasks and triggers. Among them, SchedulerFactory is a factory class that provides methods for creating scheduler instances. Its main function is to generate Scheduler objects based on configurations, and its configurations may include data sources, thread pool settings, etc. After the Scheduler is created, the Scheduler is the entry point for all operations related to task scheduling, which allows users to add, delete, and control the execution of tasks. The Scheduler uses a thread pool to manage concurrently executed tasks.

[0050] Creating a task for the Java class to be executed, and setting a unique task identifier and a first task group, wherein the task identifier is used to avoid duplicate task creation, and the first task group is used to classify and manage tasks;

[0051] A task is the basic unit managed by the scheduler, which represents the specific operation that needs to be performed. When creating a task, the detailed information of the task will be defined, including the Java class (Job) to be executed by the task, and some metadata. In this embodiment, JobDetail is used as the object of the task. JobDetail is an object that describes the task, including the name, group, execution class and other necessary information of the task. JobDetail is responsible for persisting the status information of the task and ensuring that the definition of the task is unique in the scheduler. The unique task identifier is a string that is used to uniquely identify the task to avoid duplicate registration. Its existence enables the scheduler to quickly find, update or delete specific tasks. The first task group organizes related tasks into a logical group for easy management and scheduling. Through grouping, it is more convenient to perform batch operations on a group of tasks, such as pausing or resuming.

[0052] Create a task trigger, and set a unique trigger identifier, a second task group, and an execution time interval for the task trigger, wherein the trigger identifier is used to avoid repeated creation of the trigger, and the second task group is used to classify and manage the task triggers;

[0053] Specifically, the trigger controls the execution time and frequency of the task, and is the time controller of the scheduling system. By defining the trigger, you can control when the task is scheduled for execution. In this embodiment, Trigger is used as the task trigger, which contains the conditions and time settings for triggering the task. The trigger will notify the scheduler to execute the task under specific conditions. The trigger identifier is similar to the task identifier, ensuring that each trigger is unique, which helps the scheduler to quickly locate and manage triggers. The second task group is similar to the first task group. The grouping of triggers allows a group of triggers to be managed, so that the scheduler can handle different trigger rules more flexibly. The execution time interval is a parameter that defines the frequency of task execution. It can be a fixed time interval (for example, execution every 5 minutes) or a complex time rule (for example, execution every Monday at 9 am).

[0054] A task scheduler instance associates tasks with task triggers.

[0055] Binding tasks and triggers together enables the scheduler to execute the corresponding tasks according to the definition of the trigger. This is a key step in combining the execution logic of the task with the time scheduling logic. In this embodiment, scheduleJob is used to register the task (JobDetail) and the task trigger (Trigger) to the scheduler. Through this step, the scheduler can combine the rules of the task and the trigger to achieve automatic scheduling.

[0056] Furthermore, the first task group and the second task group are the same group.

[0057] Tasks and triggers share the same group, which simplifies management when operating in the scheduler. For example, all tasks and triggers in the group can be paused, resumed, or deleted at the same time. Putting tasks and triggers in the same group helps to clearly define the relationship between them. The scheduler can easily find the corresponding trigger when executing a task, thus ensuring that the task is executed at the right time. If you need to add new tasks or triggers later, just add them to the same group, which is easy to maintain and expand without complex configuration.

[0058] Preferably, calling the custom class loader according to the Java class name and the access address and creating an instance corresponding to the Java class includes:

[0059] Convert the Java class name to be executed into a URL address;

[0060] For example, converting a class name (such as com.example.MyClass) into a file path and protocol (such as http: / / server.com / classes / com / example / MyClass.class) for access over the network.

[0061] Access the file server according to the URL address to obtain the Java class information to be executed;

[0062] Through the constructed URL, HTTP or other network protocols are used to send requests to the file server. Typical HTTP GET requests can be implemented through Java's HttpURLConnection class. After the file server responds, it returns the Java class information to be executed.

[0063] Read the Java class information through the input stream, and then write the Java class information into the byte array through the output stream;

[0064] Input stream (such as InputStream) is used to read the Java class information to be executed. In the present embodiment, BufferedInputStream is used to improve the reading performance. The data read is a byte stream, so it is necessary to store it in a byte array for subsequent processing. Output stream (such as ByteArrayOutputStream) writes the byte stream read into the byte array in the internal memory, and this array will be used as the input for loading the Java class. This mode allows byte data to be processed in the internal memory without having to interact directly with the file system, thereby improving flexibility and speed.

[0065] Convert a byte array to an instance of the corresponding Java class.

[0066] Using Java's reflection mechanism, such as the ClassLoader class, dynamically load the byte array, and then through a custom class loader (such as ByteArrayClassLoader), you can override the findClass method. Take the byte array as input and call the defineClass method to convert it into a Class object. This process includes steps such as verifying the bytecode format and linking classes to ensure security and effectiveness. After successful loading, you can create an instance of the class using newInstance() or the newInstance() method of the Constructor class to generate an object at runtime.

[0067] It also includes manually controlling the task scheduler to stop task scheduling and thus stop executing the method in the Java class to be executed.

[0068] Allows manual control of task start and stop, improves system flexibility, manages task execution at runtime, ensures task reliability and timely stop.

[0069] The execution time interval is set by Cron expression.

[0070] Specifically, a Cron expression is a string format used to specify the execution time rules of a scheduled task. It consists of 6 or 7 parts, representing seconds, minutes, hours, days, months, weeks, and years (the year part is optional). Each part is separated by a space, and the following special characters and rules can be used: "*" means matching all values ​​of the field. For example, using "*" in the minute field means every minute. "," is used to separate multiple values. For example, using 1,3,5 in the week field means Monday, Wednesday, and Friday. "-" indicates a range. For example, using 8-12 in the hour field means 8 to 12 o'clock. " / " is used to specify the step size. For example, using * / 5 in the minute field means every 5 minutes. Since Cron expressions use a unified format, it is very simple to modify the task scheduling time. You only need to update the expression without rewriting the scheduling logic. Many scheduling tools and frameworks (such as Quartz and Spring Scheduler) support Cron expressions, which are convenient for use in different technology stacks.

[0071] like Figure 2 As shown, a Java class dynamic execution system is applied to the above Java class dynamic execution method, including:

[0072] Information upload module, used to upload the Java class to be executed to the file server;

[0073] A scheduler creation module is used to create a task scheduler for the Java class to be executed, wherein the task scheduler is configured with the execution time interval of the Java class and the name of the Java class to be executed;

[0074] The method execution module is used for, after each execution time interval, the task scheduler calls the subclass loader according to the Java class name and creates an instance corresponding to the Java class in the file server and a reflection object of the method in the Java class to be executed, and executes the method in the Java class to be executed according to the instance and the reflection object.

[0075] This embodiment implements a dynamic execution method and implementation process of a Java class. Please refer to the above embodiments and will not be described in detail here.

[0076] In addition, an embodiment of the present invention further proposes a terminal device, which includes a memory, a processor, and a program stored in the memory and executable on the processor, wherein the program is configured to implement the steps of the dynamic execution method of the Java class as described above.

[0077] Since the program is configured to implement the steps of the dynamic execution method of the Java class as described above, the program at least has all the beneficial effects brought about by all the technical solutions of all the aforementioned embodiments, which will not be described one by one here.

[0078] In addition, an embodiment of the present application further proposes a computer-readable storage medium, on which a dynamic execution program of a Java class is stored. When the dynamic execution program of the Java class is executed by a processor, the steps of the dynamic execution method of the Java class described above are implemented.

[0079] Since all the technical solutions of all the aforementioned embodiments are adopted when the dynamic execution program of this Java class is executed by the processor, it has at least all the beneficial effects brought by all the technical solutions of all the aforementioned embodiments, which will not be described one by one here.

[0080] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "illustrative embodiments", "examples", "specific examples", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.

[0081] Although the embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the claims and their equivalents.

Claims

1. A method for dynamic execution of a Java class, characterized in that: include: Upload the Java class to be executed to the file server; Creating a task scheduler for the Java class to be executed, wherein the task scheduler is configured with the execution time interval of the Java class and the name of the Java class to be executed; After each execution time interval, the task scheduler calls the custom class loader according to the Java class name and creates an instance corresponding to the Java class in the file server and a reflection object of the method in the Java class to be executed, and executes the method in the Java class to be executed according to the instance and the reflection object.

2. The dynamic execution method according to claim 1, characterized in that: Setting up the task scheduler for a Java class to be executed involves: Get the task scheduler instance; Creating a task for the Java class to be executed, and setting a unique task identifier and a first task group, wherein the task identifier is used to avoid duplicate task creation, and the first task group is used to classify and manage tasks; Create a task trigger, and set a unique trigger identifier, a second task group, and an execution time interval for the task trigger, wherein the trigger identifier is used to avoid repeated creation of the trigger, and the second task group is used to classify and manage the task triggers; A task scheduler instance associates tasks with task triggers.

3. The dynamic execution method according to claim 2, characterized in that: The first task group and the second task group are the same group.

4. The dynamic execution method according to claim 1, characterized in that: Calling a custom class loader based on the Java class name and access address and creating an instance of the Java class includes: Convert the Java class name to be executed into a URL address; Access the file server according to the URL address to obtain the Java class information to be executed; Read the Java class information through the input stream, and then write the Java class information into the byte array through the output stream; Convert a byte array to an instance of the corresponding Java class.

5. The dynamic execution method according to claim 1, characterized in that: It also includes manually controlling the task scheduler to stop task scheduling and thus stop executing the method in the Java class to be executed.

6. The dynamic execution method according to claim 1, characterized in that: The execution time interval is set by Cron expression.

7. A Java class dynamic execution system, using the Java class dynamic execution method according to any one of claims 1 to 6, characterized in that: include: Information upload module, used to upload the Java class to be executed to the file server; A scheduler creation module is used to create a task scheduler for the Java class to be executed, wherein the task scheduler is configured with the execution time interval of the Java class and the name of the Java class to be executed; The method execution module is used for, after each execution time interval, the task scheduler calls the subclass loader according to the Java class name and creates an instance corresponding to the Java class in the file server and a reflection object of the method in the Java class to be executed, and executes the method in the Java class to be executed according to the instance and the reflection object.

8. A terminal device, characterized in that: The terminal device comprises: a memory, a processor, and a program stored in the memory and executable on the processor, wherein the program is configured to implement the steps of the dynamic execution method of a Java class as claimed in any one of claims 1 to 6.

9. A storage medium, characterized in that The storage medium stores a dynamic execution program of a Java class, and when the dynamic execution program of the Java class is executed by the processor, the steps of the dynamic execution method of the Java class as described in any one of claims 1 to 6 are implemented.

Citation Information

Patent Citations

  • Timing task management method and device, equipment and storage medium

    CN113742044A

  • File generation method and page configuration method and device

    CN114398023A

  • Timing task management method and device, equipment and storage medium

    CN115934288A

  • Lightweight distributed timed task scheduling system, method and device and medium

    CN117648166A