Timed task HTTP interface dynamic triggering method based on Javaagent

By dynamically modifying the bytecode of scheduled tasks at JVM startup using Javaagent technology, dynamic triggering of scheduled tasks via HTTP interfaces in a microservice architecture is achieved. This solves the problems of long testing cycles and strong code intrusion, and improves testing efficiency and system flexibility.

CN121705166APending Publication Date: 2026-03-20PICC INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511735726.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-24
Publication Date
2026-03-20

AI Technical Summary

Technical Problem

Existing scheduled task testing methods rely on manual intervention, resulting in long testing cycles, resource waste, and strong code intrusion, making it difficult to efficiently manage scheduled tasks in a microservice architecture.

Method used

By using Javaagent technology to load the agent program when the JVM starts, and using Instrumentation instances and JavassistTransformer to dynamically modify the bytecode of the target class, the HTTP interface triggering logic of the Controller class is identified and woven in, and Cron expressions are dynamically parsed to achieve real-time execution and configuration modification of scheduled tasks.

Benefits of technology

Scheduled tasks can be dynamically triggered and adjusted without modifying business code, significantly improving testing efficiency and system maintenance convenience, while reducing testing costs and development complexity.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121705166A_ABST
    Figure CN121705166A_ABST
Patent Text Reader

Abstract

The invention discloses a method for dynamically triggering a timed task HTTP (Hyper Text Transport Protocol) interface based on Javaagent, which relates to the technical field of data processing, and comprises the following steps of: loading a Javaagent program containing a Premain-Class attribute through a JVM (Java Virtual Machine) startup parameter, and loading and executing a premain method by a system class loader before an application is started; the method comprises the following steps: registering a class converter to a class loader based on an Instrumentation instance, wherein the class converter realizes dynamic modification on a target class byte code through a transform method; identifying and weaving an HTTP interface trigger logic of a Controller class, so that the Controller class can directly call an execution method of a corresponding timed task when receiving an HTTP request containing a task identifier; and dynamically analyzing and replacing the Cron expression of the target timed task according to the HTTP request parameter to realize instant execution and configuration modification of the timed task. According to the embodiment of the invention, instant triggering and dynamic configuration of the timed task can be realized without modifying a business code or restarting a service, so that the test efficiency is remarkably improved and the system maintenance cost is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data processing technology, and in particular to a method for dynamically triggering scheduled tasks via HTTP interfaces based on Javaagent. Background Technology

[0002] Java scheduled task systems, as core components of enterprise-level Java application development, are widely used in business process automation scenarios in industries such as insurance and finance. Among related technologies, a complete technical system covering task definition, time configuration, and execution control is constructed through the collaborative operation of the Spring framework's control layer, the Quartz task scheduling framework, and the Xxl-job distributed scheduling platform. Specifically, this system covers the entire process from Cron expression parsing to task triggering, including key aspects such as class loading mechanisms, bytecode execution paths, and multi-node task distribution. With the popularization of microservice architecture, JVM-based bytecode enhancement technologies (such as Javaagent) have gradually become an important means of achieving non-intrusive functional extensions. Through the dynamic class conversion capabilities of Instrumentation instances, they provide underlying support for adjusting system runtime behavior.

[0003] However, existing scheduled task testing methods, which directly modify configuration files and restart services, do not address the fundamental issue of high test trigger latency. This can extend testing cycles from hours to days, or lead to interface fragmentation in multi-framework integration scenarios. Specifically, traditional solutions require testers to master Cron expression syntax and verify task execution at specified time points. One large SAP project experienced a significant increase in maintenance costs due to the number of test interfaces ballooning to 172. Therefore, existing technologies typically develop separate HTTP interfaces for each task, but this approach suffers from strong code intrusion and a large development workload. For example, a bank system experienced a 12% increase in production environment incident rates due to frequent modifications to business code. This manual intervention-dependent testing model not only violates the interface convergence principle of microservice architecture but also causes significant resource waste in high-frequency testing scenarios such as premium calculation and claims processing in the insurance industry. A systematic solution that overcomes these three technical contradictions is urgently needed. Summary of the Invention

[0004] The main objective of this invention is to provide a method for dynamically triggering scheduled tasks via HTTP interfaces based on Javaagent.

[0005] Another objective of this invention is to propose a dynamic triggering device for a scheduled task HTTP interface based on Javaagent.

[0006] The third objective of this invention is to provide a computer device.

[0007] A fourth objective of this invention is to provide a non-transitory computer-readable storage medium.

[0008] To achieve the above objectives, a first aspect of the present invention proposes a method for dynamically triggering a scheduled task via an HTTP interface based on a Java agent, comprising: S1, Load the Javaagent agent program containing the Premain-Class attribute through JVM startup parameters. The agent program is loaded by the system class loader and the premain method is executed before the application starts. S2, Register a class converter with the class loader based on the Instrumentation instance. The class converter dynamically modifies the bytecode of the target class through the transform method. S3, identify and weave the HTTP interface triggering logic of the Controller class, so that the Controller class can directly call the execution method of the corresponding scheduled task when it receives an HTTP request containing a task identifier; S4. Dynamically parse and replace the Cron expression of the target scheduled task based on the HTTP request parameters to realize the real-time execution and configuration modification of the scheduled task.

[0009] In one embodiment of the present invention, registering the class converter with the class loader based on the Instrumentation instance includes: S21. Register a JavassistTransformer instance using the inst.addTransformer() method. The instance contains the transform(ClassLoader loader, String className, byte[]classfileBuffer) method. S22, the transform method obtains the target class definition through ClassPool.getDefault().get(className) and calls CtClass.toBytecode() to generate the modified bytecode stream.

[0010] In one embodiment of the present invention, the identification and weaving of the HTTP interface triggering logic into the Controller class includes: S31, the Controller class is determined by analyzing the class name and annotation information of the target class, wherein the annotation information includes @RestController or @Controller annotation; S32, dynamically generate an HTTP interface path conforming to the RESTful specification. The path format is / trigger / {taskIdentifier}, where {taskIdentifier} is the unique identifier of the scheduled task.

[0011] In one embodiment of the present invention, the step of dynamically parsing and replacing the Cron expression of the target scheduled task based on HTTP request parameters includes: S41, use the regular expression ^\d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{4} \d{1,2}$ to validate the Cron expression format in the HTTP request parameters; S42, the cron field value of the target class is directly modified through the Javassist bytecode manipulation library. The modification operation is completed during the class loading phase and does not require restarting the JVM.

[0012] In one embodiment of the present invention, it further includes: S5. A performance monitoring module is dynamically implanted through Javaagent. The performance monitoring module records the conversion time after each bytecode conversion and triggers an alarm mechanism when the conversion time exceeds a preset threshold.

[0013] To achieve the above objectives, a second aspect of the present invention provides a device for dynamically triggering a scheduled task HTTP interface based on a Java agent, comprising: The agent loading module is used to load a Javaagent agent program containing the Premain-Class attribute via JVM startup parameters. The agent program is loaded by the system class loader and the premain method is executed before the application starts. The class converter registration module is used to register class converters with the class loader based on the Instrumentation instance. The class converters dynamically modify the bytecode of the target class through the transform method. The HTTP interface weaving module is used to identify and weave the HTTP interface triggering logic of the Controller class, so that the Controller class can directly call the execution method of the corresponding scheduled task when it receives an HTTP request containing a task identifier. The Cron expression dynamic replacement module is used to dynamically parse and replace the Cron expression of the target scheduled task based on the HTTP request parameters, so as to realize the real-time execution and configuration modification of the scheduled task.

[0014] To achieve the above objectives, a third aspect of this application provides a computer device, including a processor and a memory; wherein the processor reads executable program code stored in the memory to run a program corresponding to the executable program code, for implementing a Javaagent-based method for dynamically triggering a scheduled task HTTP interface as described in the first aspect embodiment.

[0015] To achieve the above objectives, the fourth aspect of this application proposes a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements a method for dynamically triggering a Javaagent-based scheduled task HTTP interface as described in the first aspect embodiment.

[0016] The embodiments of the present invention have the following beneficial effects: This invention enhances bytecode by injecting a Javaagent at JVM startup, allowing for the dynamic addition of HTTP interfaces to immediately trigger and modify scheduled tasks without modifying business code. This effectively solves the problems of high latency in traditional scheduled task testing, scattered interface management, and strong code intrusion, significantly improving testing efficiency and system maintenance convenience. Attached Figure Description

[0017] The above and / or additional aspects and advantages of the present invention will become apparent and readily understood from the following description of the embodiments taken in conjunction with the accompanying drawings, wherein: Figure 1 A flowchart illustrating a method for dynamically triggering a scheduled task via an HTTP interface based on Javaagent, provided in an embodiment of the present invention; Figure 2 An architecture diagram of a method for dynamically triggering a scheduled task via an HTTP interface based on Javaagent, provided in an embodiment of the present invention; Figure 3 This is a structural diagram of a Javaagent-based timed task HTTP interface dynamic triggering device provided in an embodiment of the present invention. Detailed Implementation

[0018] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other. The present invention will now be described in detail with reference to the accompanying drawings and embodiments.

[0019] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.

[0020] The following describes, with reference to the accompanying drawings, a method and apparatus for dynamically triggering a scheduled task via an HTTP interface based on a Java agent, according to an embodiment of the present invention.

[0021] Example 1 This embodiment provides a method for dynamically triggering scheduled tasks via HTTP interfaces based on Javaagent. For example... Figure 1 As shown, the method includes the following steps: S1, Load the Javaagent agent program containing the Premain-Class attribute through JVM startup parameters. The agent program is loaded by the system class loader and the premain method is executed before the application starts.

[0022] Specifically, this step loads the Javaagent agent program containing the Premain-Class attribute via JVM startup parameters, which is the key entry point for implementing bytecode-enhanced scheduled task HTTP interface call technology. At the technical implementation level, this step utilizes the Java Instrumentation API, using `-javaagent:` during the JVM startup phase. <agent-jar-path>The proxy JAR file is loaded with parameters. The MANIFEST.MF file of this JAR file must contain the Premain-Class attribute, whose value is the fully qualified class name of the proxy class (e.g., com.example.AgentMain), and ensure that this class implements the premain method. At startup, the JVM uses the system class loader (ClassLoader.getSystemClassLoader()) to load this proxy class and calls the premain(String args, Instrumentation inst) method before executing the main method, thereby dynamically modifying the bytecode of the target class.

[0023] In this embodiment of the invention, the format of the -javaagent parameter must strictly follow the Java standard specification, its path should be a valid JAR file path, and the Premain-Class attribute must be declared in MANIFEST.MF in the form of Premain-Class:com.example.AgentMain. The args parameter of the premain method is used to receive the option parameters passed in at startup, and inst is an Instrumentation instance provided by the JVM, supporting operations such as class loading monitoring and class definition replacement. Through inst.addTransformer(new JavassistTransformer()), the agent program registers the custom class transformer with the JVM, so that it automatically triggers bytecode enhancement logic during class loading.

[0024] This step is applicable to Java projects built on frameworks such as Spring, Quartz, and XXL, and is particularly advantageous in scenarios requiring rapid testing of scheduled tasks and dynamic adjustment of execution time or triggering conditions. By loading the agent in the startup parameters, real-time control of scheduled tasks can be achieved without modifying business logic or configuration files, such as triggering task execution via an HTTP interface or dynamically modifying Cron expressions.

[0025] The technical advantage of this step lies in providing a unified access point for subsequent bytecode enhancement, ensuring that the proxy logic is loaded and initialized before application startup, thereby achieving non-intrusive enhancement of the target class. Its innovation lies in using native JVM mechanisms to dynamically control scheduled tasks, improving testing efficiency and system flexibility.

[0026] S2, Register a class converter with the class loader based on the Instrumentation instance. The class converter dynamically modifies the bytecode of the target class through the transform method.

[0027] Specifically, this step involves using Java Instrumentation technology to register a class converter with the class loader, enabling dynamic modification of the target class bytecode. This is one of the core mechanisms for implementing the scheduled task HTTP interface call function in this invention proposal. In some implementations, the class converter is registered through the addTransformer method of the Instrumentation instance, specifically using inst.addTransformer(new JavassistTransformer()), adding a custom JavassistTransformer instance as a class converter to the JVM's class loading process.

[0028] In this embodiment of the invention, JavassistTransformer implements the ClassFileTransformer interface provided by Java, and its core method is transform. This method is called during class loading and receives parameters including the class loader (ClassLoader), class name (String className), protection domain (ProtectionDomain), and raw bytecode (byte[] classfileBuffer). By parsing the class name and matching the target class (such as a class containing scheduled task logic), this transformer can dynamically modify the bytecode using the Javassist utility library to insert logic code for listening to HTTP requests and triggering scheduled task execution.

[0029] Furthermore, the input parameters of the `transform` method must conform to the JVM class loading specification, where `className` is the fully qualified class name (e.g., `com / example / TaskController`) and `classfileBuffer` is an array of raw bytecode. The transformer must ensure the integrity of the class structure during processing to avoid class loading failure due to bytecode modification. In addition, the implementation of `JavassistTransformer` uses the `handler` method of the `AbstractJavassistHandler` class to dynamically determine whether to replace the triggering logic of the scheduled task based on different HTTP request parameters, thereby achieving real-time adjustment of task execution time and frequency.

[0030] This step applies to Java projects based on mainstream task scheduling frameworks such as Spring, Quartz, or XXL. By adding the JavaAgent parameter (e.g., -javaagent:agent.jar) when the application starts, it enables real-time execution and dynamic modification of scheduled tasks without modifying business logic or configuration files. This method is particularly suitable for testing environments, significantly shortening testing cycles and improving testing efficiency.

[0031] In this embodiment of the invention, this step achieves non-intrusive enhancement of the target class, enabling the execution logic of the scheduled task to be dynamically adjusted according to external HTTP requests. This solves the problems of needing to modify configurations, restart services, and rely on cron expressions in traditional methods. Its innovation lies in utilizing the JVM's class loading mechanism and bytecode enhancement technology to achieve flexible control over task scheduling behavior, which has significant practical value and promotional significance.

[0032] Furthermore, S2 includes: S21. Register a JavassistTransformer instance using the inst.addTransformer() method. The instance contains the transform(ClassLoader loader, String className, byte[]classfileBuffer) method.

[0033] Specifically, in some implementations, registering a JavassistTransformer instance by calling the inst.addTransformer() method is one of the core steps in implementing bytecode enhancement in this technical solution. This method is part of the Java Instrumentation API and is used to inject a ClassFileTransformer into the JVM, thereby dynamically modifying its bytecode during class loading. Specifically, inst is an Instrumentation instance automatically passed in by the JVM at startup, and its addTransformer() method accepts an instance of a class that implements the ClassFileTransformer interface, such as JavassistTransformer.

[0034] After registration, a JavassistTransformer instance automatically calls its implemented `transform(ClassLoader loader, String className, byte[] classfileBuffer)` method whenever the JVM loads a class. The method's parameters include the class loader (`loader`), the fully qualified name of the class (`className`), and the raw bytecode array `classfileBuffer`. Its return value is the modified bytecode array; returning null indicates no modification. In this technical solution, the `transform` method uses the Javassist tool to instrument the target class's bytecode, for example, inserting HTTP interface call logic into a scheduled task class, thereby achieving dynamic control of task execution without modifying the source code.

[0035] Furthermore, the implementation of JavassistTransformer in this step relies on calls to the AbstractJavassistHandler class. This class provides a generic bytecode processing logic that identifies the target class through reflection or class name matching mechanisms and inserts custom logic into the class's methods. For example, HTTP request listening logic can be inserted into the entry method of a scheduled task, allowing the task to be triggered and executed through an external interface.

[0036] In practice, this step typically involves loading the agent program via the `-javaagent:agent.jar` parameter during JVM startup. The agent program then registers the JavassistTransformer in the `premain` method. This method executes earlier than the application's `main` method, ensuring that all subsequently loaded classes can be dynamically enhanced.

[0037] The technical advantage of this step is that it enables dynamic control of scheduled tasks through bytecode enhancement technology. Without modifying the original code or redeploying the application, it can achieve functions such as immediate execution and time modification of tasks, which significantly improves testing efficiency and system flexibility and reduces maintenance costs.

[0038] S22, the transform method obtains the target class definition through ClassPool.getDefault().get(className) and calls CtClass.toBytecode() to generate the modified bytecode stream.

[0039] Specifically, in this step, the transform method obtains the CtClass definition of the target class by calling ClassPool.getDefault().get(className), and further calls CtClass.toBytecode() to generate the modified bytecode stream, which is one of the core steps in implementing the JavaAgent bytecode enhancement function.

[0040] In one embodiment of this invention, `ClassPool` is a key class in the Javassist framework used to manage the class definition pool. `ClassPool.getDefault()` returns the system's default class pool instance, which is initialized when the JVM starts and can dynamically load and resolve class structures. Through the `get(className)` method, the proxy program can obtain the `CtClass` object of the target class, which represents the class's compile-time structure, allowing developers to modify the class's methods, fields, etc., without recompiling the source code. In this scheme, the target class is typically a `Controller` class in the Spring framework used to handle HTTP requests, or a class related to scheduled tasks (such as the `Job` class in Quartz or the executor class in XxlJob).

[0041] Furthermore, the `CtClass.toBytecode()` method converts the modified class structure into a bytecode stream (byte[]) that the JVM can recognize. This bytecode stream is then returned to the `transform` method of the `ClassFileTransformer` interface. The return value of this method is the enhanced class definition. The JVM uses this bytecode stream to reload the target class, thereby achieving runtime class replacement and logic enhancement. This process follows the Java Instrumentation API specification, ensuring that dynamic modifications to classes are legal and safe within the JVM lifecycle.

[0042] In this embodiment of the invention, the `className` parameter is a string representing the fully qualified class name of the class to be enhanced, such as `com.example.TaskController`. The byte array returned by the `toBytecode()` method must conform to the JVMClass file format specification (Java Virtual Machine Specification), including the correct encoding of fields such as class structure, method body, and constant pool. Furthermore, the Javassist class pool configuration must appropriately set the class search path (e.g., adding a custom class path via `ClassPool.appendClassPath()`) to ensure that the target class can be correctly loaded and resolved.

[0043] This step is widely used in Java scheduled task systems based on frameworks such as Spring, Quartz, or XxlJob. Using JavaAgent, developers can dynamically enhance the triggering logic of scheduled tasks without modifying existing code or recompiling and deploying. For example, they can immediately execute tasks via HTTP interfaces or modify the Cron expression for tasks. This method is particularly suitable for testing environments, significantly shortening testing cycles and improving testing efficiency.

[0044] Furthermore, this step achieves non-intrusive enhancement of the target class, ensuring that the system can dynamically adjust task behavior based on external requests at runtime. This solves the technical challenges of traditional scheduled task testing, such as reliance on Cron expressions, waiting for execution cycles, and complex test case writing. Its innovation lies in using bytecode enhancement technology to decouple the HTTP interface from the task scheduling logic, improving the system's flexibility and maintainability.

[0045] S3. Identify and weave the HTTP interface triggering logic of the Controller class, so that the Controller class can directly call the execution method of the corresponding scheduled task when it receives an HTTP request containing a task identifier.

[0046] Specifically, the core of this step lies in enhancing the bytecode of the Controller class in the Spring framework using Javaagent technology. This allows the corresponding scheduled task's execution method to be directly triggered upon receiving an HTTP request containing a task identifier. Based on Java Instrumentation technology, this step loads a custom Javaagent at JVM startup, enabling dynamic modification of the target class without requiring application recompilation or deployment to complete the logic weaving.

[0047] This step begins by specifying the agent's JAR file by adding `-javaagent:agent.jar` to the JVM startup parameters. The JAR file's Manifest file must have the `Premain-Class` attribute configured, pointing to the agent class containing the `premain` method. The `premain` method is invoked when the JVM starts and registers a `ClassFileTransformer` implementation class, such as `JavassistTransformer`, through the `addTransformer` method of the `Instrumentation` instance. This transformer instrumentes the target `Controller` class with bytecode during class loading, inserting logic to identify task identifiers in HTTP requests and invoke scheduled task execution methods.

[0048] In practice, JavassistTransformer identifies the target Controller class through a class name matching mechanism and modifies the class's bytecode using the Javassist utility library. Logical decision-making code is inserted into the HTTP interface methods of the Controller class to parse task identifiers (such as task_id or cron_expression) from the request parameters and dynamically call the corresponding scheduled task execution method based on the identifier. This method typically uses reflection or a predefined mapping table to bind tasks to execution logic.

[0049] In this embodiment of the invention, the task identifier is typically a string, formatted as task_id or cron_expression, and its length and encoding must conform to the standard specifications of HTTP request parameters. The path and method of the HTTP request (e.g., / trigger-task, POST) must be consistent with the enhanced method signature in the Controller class to ensure that the request can be correctly routed and processed.

[0050] This step is applicable to Java projects based on the Spring framework, especially systems that integrate task scheduling frameworks such as Quartz and XxlJob. Using this method, testers do not need to master the rules of writing Cron expressions; they can immediately trigger scheduled tasks by constructing specific HTTP requests, thus significantly improving testing efficiency and flexibility.

[0051] The technical benefits of this step are that it enables instant triggering and dynamic control of scheduled tasks, lowers the testing threshold, and reduces the problem of extended testing cycles caused by waiting for scheduled times. At the same time, by using a unified HTTP interface management method, it avoids the maintenance costs and code redundancy associated with developing separate interfaces for each task, thus improving the system's testability and maintainability.

[0052] Furthermore, S3 includes: S31, the Controller class is determined by analyzing the class name and annotation information of the target class, wherein the annotation information includes @RestController or @Controller annotation.

[0053] Specifically, in some implementations, determining whether a target class is a Controller class by analyzing its class name and annotation information is one of the key steps in implementing bytecode enhancement logic in this technical solution. The core of this step lies in using Javaagent to dynamically identify and process the target class during the class loading phase, thereby injecting the ability to call HTTP interfaces for scheduled tasks without modifying the original business logic.

[0054] In this embodiment of the invention, this step is completed through the `transform` method of the `ClassFileTransformer` interface of `Javaagent`. When the JVM loads the class, it calls this transformer, triggering the analysis of the class name and annotation information. Specifically, the agent checks whether the class is annotated with `@RestController` or `@Controller`, these two annotations are standard annotations used in the Spring framework to identify controller components. The class's metadata information is read using reflection or bytecode analysis tools (such as Javassist, ASM, etc.) to determine if it contains one of the aforementioned annotations. If the condition is met, the class is considered a `Controller` class, and its bytecode will be enhanced subsequently to inject HTTP interface call logic.

[0055] This step also involves class name matching rules and annotation recognition mechanisms. Class names typically follow Spring's naming conventions, such as class names ending with "Controller" (e.g., TaskController), but a more reliable method is through annotation recognition. During annotation recognition, the proxy parses the class's ConstantPool information, looking for symbolic references to Lorg / springframework / stereotype / Controller; or Lorg / springframework / web / bind / annotation / RestController;. This process does not rely on runtime reflection but is completed through bytecode analysis during the class loading phase, resulting in higher performance and stability.

[0056] This step is widely applicable to Java projects based on the Spring framework, especially systems integrating task scheduling frameworks such as Quartz and XXL. By identifying the Controller class, the proxy can inject HTTP interface call logic into the appropriate class, allowing testers to directly trigger the execution of scheduled tasks via HTTP requests without waiting for the time specified by the Cron expression. This is particularly important in testing environments, significantly shortening the testing cycle and improving testing efficiency.

[0057] In this embodiment of the invention, this step enables accurate identification of Spring controller components, providing a clear target for subsequent bytecode enhancement. Through annotation-based identification, the agent can be compatible with various Spring project structures, ensuring the universality and scalability of the enhancement logic. Simultaneously, this method avoids intrusive modifications to existing code, conforming to the non-intrusive enhancement principle of Javaagent, and improving the system's maintainability and deployment flexibility.

[0058] S32, dynamically generate an HTTP interface path conforming to the RESTful specification. The path format is / trigger / {taskIdentifier}, where {taskIdentifier} is the unique identifier of the scheduled task.

[0059] Specifically, this step involves dynamically generating HTTP interface paths that conform to the RESTful specification. Its core lies in using Javaagent technology to enhance the bytecode of the Controller class in the Spring framework, thereby dynamically injecting an interface path that can be triggered by an HTTP request into each scheduled task without modifying the original business logic. The path format is / trigger / {taskIdentifier}, where {taskIdentifier} is the unique identifier for the scheduled task, typically generated from the task name or a combination of the task class name and method name, ensuring the task's unique identifiability.

[0060] This step relies on the Java Instrumentation mechanism, enabling dynamic modification of the target class by loading a custom Java agent at JVM startup. Specifically, the agent registers a ClassFileTransformer in the premain method. When the target class (such as a Spring Controller class) is loaded, this transformer uses bytecode manipulation tools like Javassist to insert new methods or modify existing ones, generating RESTful-compliant `@RequestMapping` or `@GetMapping` annotations and binding them to the path ` / trigger / {taskIdentifier}`. This path receives client request parameters through the path variable `{taskIdentifier}`, thus triggering a specific scheduled task.

[0061] In this embodiment of the invention, the taskIdentifier is typically a string, and its length is recommended to be controlled within 64 bytes to conform to the general specifications of HTTP paths. This identifier must meet the URI encoding standard (RFC 3986) to ensure that special characters do not cause parsing errors during transmission. Furthermore, this interface supports standard HTTP methods (such as GET or POST) and can be configured with security parameters such as request headers and authentication mechanisms (such as JWT or OAuth2) to enhance the controllability and security of the interface.

[0062] This step is widely applicable to Java projects based on the Spring framework, especially systems integrating task scheduling frameworks such as Quartz or XXL. Developers do not need to write separate HTTP interfaces for each scheduled task; unified interface management can be achieved simply through Javaagent configuration. Testers can immediately trigger task execution by sending an HTTP request to the ` / trigger / {taskIdentifier}` path, without waiting for the time point set by the Cron expression, significantly improving testing efficiency and debugging flexibility.

[0063] The technical advantage of this step lies in its ability to enable instant triggering and remote control of scheduled tasks through a dynamic path generation mechanism, lowering the testing threshold and improving the real-time performance and controllability of task debugging. Simultaneously, this method avoids the system overhead caused by frequent configuration file modifications or service restarts in traditional approaches, exhibiting good compatibility and scalability.

[0064] S4. Dynamically parse and replace the Cron expression of the target scheduled task based on the HTTP request parameters to realize the real-time execution and configuration modification of the scheduled task.

[0065] Specifically, the core technology of this step lies in dynamically parsing and replacing the Cron expression of the target scheduled task through HTTP request parameters, thereby achieving real-time execution and configuration modification of the scheduled task. In some implementations, this functionality enhances the target class's bytecode using Javaagent technology. Specifically, a custom Javaagent is loaded when the JVM starts. This agent registers a ClassFileTransformer through the premain method, which is used to perform bytecode instrumentation on the target class (such as Spring's @Scheduled annotated methods, Quartz's Job class, or XxlJob's executor class) during class loading.

[0066] In this embodiment of the invention, when an HTTP request arrives at the system, the Controller layer receives request parameters, including a task identifier (such as a method name, class name, or task ID) and a new Cron expression. The system then passes these parameters to the runtime task scheduling module via logic injected by the Java agent. Further, the agent dynamically parses the Cron expression and verifies whether its format conforms to the Quartz or Spring standard specifications. If the format is valid, the original scheduling configuration of the target task is replaced using reflection or bytecode modification mechanisms.

[0067] Key parameters involved in this step include: task identifier, new Cron expression, current task scheduler instance (such as Scheduler or ScheduledTaskRegistrar), and execution context. In some implementations, the system records the task's original Cron expression and sets a maximum modification frequency (e.g., maxModifyInterval = 60s) to prevent frequent modifications from causing scheduler instability.

[0068] In application scenarios, this step is widely applicable to Java projects based on Spring, Quartz, or XxlJob, especially in scenarios requiring frequent testing of scheduled tasks, such as financial systems, data synchronization, and report generation. Testers do not need to master Cron expression syntax to trigger task execution or adjust the execution frequency via HTTP interface, thereby significantly improving testing efficiency.

[0069] The technical advantage of this step is that it enables dynamic scheduling and control of scheduled tasks, allows configuration updates to be completed without restarting the service, reduces testing cycles and development costs, and improves system maintainability and testing flexibility.

[0070] The method for dynamically triggering scheduled tasks via HTTP interfaces based on JavaAgent in this invention enhances bytecode by loading JavaAgent at JVM startup. This allows for dynamic control of the execution, modification, and addition of scheduled tasks without modifying business code, significantly improving the flexibility and efficiency of scheduled task testing while reducing testing costs and development / maintenance complexity.

[0071] Furthermore, S4 includes: S41, use the regular expression ^\d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{4} \d{1,2}$ to validate the Cron expression format in the HTTP request parameters.

[0072] Specifically, in some implementations, this method step validates the Cron expression format in the HTTP request parameters using the regular expression ^\d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{4} \d{1,2}$, thereby ensuring that the user-input scheduled task time rule conforms to the system's expected structural requirements. This regular expression is used to match a specific formatted Cron expression, whose structure consists of 7 fields representing minutes, hours, days, months, days of the week, years, and task identifiers, with each field separated by a space.

[0073] This regular expression validates the input string using a pattern matching mechanism. Specifically, `^\d{1,2}` indicates that the minute field has 1 to 2 digits, ranging from 0 to 59; `\d{1,2}` indicates that the hour field has 1 to 2 digits, ranging from 0 to 23; the next three `\d{1,2}` correspond to the day, month, and day of the week fields, respectively, all allowing 1 to 2 digits, with the day ranging from 1 to 31, the month from 1 to 12, and the day of the week from 0 to 6 (0 represents Sunday); `\d{4}` represents the year field, which is a 4-digit number and is typically used to limit the year range for task execution; the final `\d{1,2}$` represents the task identifier field, which is a 1 to 2-digit number used to uniquely identify a scheduled task instance. This regular expression strictly limits the length and type of the fields, preventing illegal characters or out-of-range values ​​from being misinterpreted.

[0074] In this embodiment of the invention, the regular expression defines the input format standard for Cron expressions, ensuring that the numerical range and length of each field conform to the system's parsing logic. For example, the minute field cannot exceed two digits and must be less than 60; the hour field cannot exceed two digits and must be less than 24; the day field cannot exceed two digits and must be less than 32; the month field cannot exceed two digits and must be less than 13; and the day of the week field cannot exceed one or two digits and must be less than 7. These indicators directly determine whether the system can correctly identify and schedule scheduled tasks.

[0075] This step is typically deployed in the HTTP interface of the Controller layer to receive user-submitted scheduled task configuration requests. When a user passes a Cron expression via an HTTP request, the system first uses the regular expression for format validation. If a match fails, a format error message is returned to prevent invalid configurations from causing scheduling exceptions. This mechanism is suitable for Java scheduled task systems built on frameworks such as Spring, Quartz, or XXL, and is particularly effective in test environments in avoiding test failures or scheduling delays caused by format errors.

[0076] In this embodiment of the invention, this step enhances the robustness and security of the system through a strict format validation mechanism, avoiding parsing errors or scheduling anomalies caused by illegal Cron expressions. Simultaneously, the regular expression structure is clear and easy to maintain, providing a reliable prerequisite for subsequent bytecode enhancement logic. This ensures that the logic injected via Javaagent can correctly identify and process user requests, thereby achieving dynamic control and immediate execution of scheduled tasks.

[0077] S42, the cron field value of the target class is directly modified through the Javassist bytecode manipulation library. The modification operation is completed during the class loading phase and does not require restarting the JVM.

[0078] Specifically, this step uses the Javassist bytecode manipulation library to dynamically modify the cron field value in the target class during the class loading phase, thereby enabling real-time adjustment of the scheduled task execution time without restarting the JVM. This technology is based on the Java Instrumentation mechanism and utilizes Javaagent to inject custom class conversion logic at program startup to enhance the bytecode of the target class.

[0079] In this embodiment of the invention, this step involves registering a custom ClassFileTransformer implementation class (such as JavassistTransformer) in the premain method and adding it to the JVM's class loading transformation chain by calling the Instrumentation.addTransformer() method. When the target class (such as a class containing scheduled task configuration) is loaded by the class loader, the transform method of JavassistTransformer will be triggered. This method receives parameters such as the class loader, class name, class protection fields, and raw bytecode. Using APIs provided by Javassist, such as ClassPool and CtClass, the bytecode of the target class can be read and modified, especially the value of its cron field. For example, the target field can be located using CtField, and a new cron expression string can be set using the setConstantValue() method.

[0080] It's important to note that the cron field is typically a string, and its value must conform to the standard Cron expression format, such as 0 0 * * * ? which indicates execution once per hour. In Javassist operations, it's crucial to ensure that field names, class names, and method names match the bytecode structure of the target class to avoid class loading failures due to field signature mismatches. Furthermore, the Javaagent's startup parameters must be specified in the MANIFEST.MF file via Premain-Class, and the Instrumentation instance is automatically passed in by the JVM, with its type being java.lang.instrument.Instrumentation.

[0081] This step is applicable to Java projects built on frameworks such as Spring, Quartz, or XxlJob, especially in testing environments. It allows for dynamic adjustment of task execution times, enabling immediate task triggering and verification without redeployment or configuration file modifications. For example, in integration testing, testers can pass new Cron expressions via HTTP interfaces, allowing the Javaagent to modify task scheduling times in real time, thereby accelerating test case execution.

[0082] The technical advantage of this step lies in its ability to dynamically modify the bytecode level, enabling immediate application of scheduled task configurations and significantly improving testing efficiency and system debugging flexibility. Simultaneously, since it eliminates the need to modify the source code or recompile and deploy, it reduces invasiveness to the production environment and enhances system maintainability and scalability.

[0083] The method for dynamically triggering scheduled tasks via HTTP interfaces based on JavaAgent in this invention enhances bytecode by loading JavaAgent at JVM startup. This allows for dynamic control of the execution, modification, and addition of scheduled tasks without modifying business code, significantly improving the flexibility and efficiency of scheduled task testing while reducing testing costs and development / maintenance complexity.

[0084] S5. A performance monitoring module is dynamically implanted through Javaagent. The performance monitoring module records the conversion time after each bytecode conversion and triggers an alarm mechanism when the conversion time exceeds a preset threshold.

[0085] Specifically, this step dynamically embeds a performance monitoring module using Javaagent technology. Its core lies in leveraging Java Instrumentation technology to load an agent program at JVM startup and perform bytecode enhancement on the target class during class loading, thereby enabling time-consuming monitoring and exception alerts for the bytecode transformation process. In some implementations, this performance monitoring module intercepts and processes each loaded class by implementing the `transform` method of the `ClassFileTransformer` interface. Specifically, when the class loader loads a class, `JavassistTransformer` receives parameters such as the class name, class loader, protection fields, and raw bytecode, records timestamps before and after the transformation, and calculates the transformation time.

[0086] In this step, the performance monitoring module obtains the conversion time by inserting timing logic into the transform method, such as using System.nanoTime() or System.currentTimeMillis() to obtain the time difference before and after the conversion. In practical applications, a preset threshold can be set. ,when When this occurs, an alarm mechanism is triggered, such as logging, sending a notification, or throwing an exception. This threshold can be configured based on system performance benchmarks, typically set in milliseconds, such as 50ms or 100ms, depending on the performance requirements of the target system and the hardware environment.

[0087] This technical solution is applicable to Java projects based on mainstream scheduled task frameworks such as Spring, Quartz, and XXL. It is particularly effective in enterprise-level systems with complex deployments and a large number of tasks, effectively monitoring performance bottlenecks during bytecode conversion and improving system stability and debugging efficiency. This step allows for transparent monitoring and exception handling of the bytecode conversion process without modifying existing business logic, demonstrating the innovation and practicality of this invention in non-intrusive performance optimization.

[0088] Example 2 This invention relates to a method for dynamically triggering scheduled tasks via HTTP interfaces based on Javaagent, such as... Figure 2 As shown, the method includes the following steps: By using JavaAgent bytecode enhancement technology, custom, externally accessible controllers can be enhanced without additional development costs. This supports popular Spring framework-based projects (XXL, Quartz, Spring annotations), eliminating the need to recompile and redeploy the entire application. This embodiment of the invention only requires adding specific JavaAgent parameters when starting the application; without modifying any application business code, it enables the immediate execution, modification, and addition of scheduled tasks, significantly reducing the impact on existing systems.

[0089] In this embodiment of the invention, Java Instrumentation technology is used to write an agent program that is independent of the application to monitor and assist the application running on the JVM.

[0090] First, when the VM starts, specify the proxy JAR in the command line. The `Premain-Class` attribute must be set in the manifest, with the value being the fully qualified class name of the proxy class. This proxy must also provide a `premain` method. When the proxy JAR is started from the command line, the VM loads the proxy class using the same system class loader as the `main` class (i.e., the loader obtained by `ClassLoader.getSystemClassLoader()`). The `premain()` method is executed before the `main` method. If both `premain(String args, Instrumentation inst)` and `premain(String args)` exist, the former takes precedence. The method parameter `args` is the same as the options in the command, of type `String`; `inst` is an `Instrumentation` instance automatically passed in by the VM at runtime, which can be used to obtain VM information.

[0091] In the `premain` method, an `inst.addTransformer(new JavassistTransformer())` method is executed. This line of code adds a class transformer to `inst`, used to transform the behavior of the class.

[0092] JavassistTransformer implements the ClassFileTransformer interface, which has only one transform method. The parameters include the class loader, class name, and the original bytecode stream. It returns the transformed bytecode stream. JavassistTransformer primarily obtains the AbstractJavassistHndler class from the class loader. This class has a handler method that replaces the original proxy method by modifying the bytecode based on the input parameters. This allows for different implementations depending on the HTTP request parameters, such as methods for immediately executing scheduled tasks and methods for modifying cron jobs.

[0093] Example 3 This invention also provides a Javaagent-based device for dynamically triggering scheduled tasks via HTTP interfaces, such as... Figure 3 As shown, the device 10 includes: The agent loading module 100 is used to load a Javaagent agent program containing the Premain-Class attribute through JVM startup parameters. The agent program is loaded by the system class loader and the premain method is executed before the application starts. The class converter registration module 200 is used to register a class converter with the class loader based on an Instrumentation instance. The class converter dynamically modifies the bytecode of the target class through the transform method. The HTTP interface weaving module 300 is used to identify and weave the HTTP interface triggering logic of the Controller class, so that the Controller class can directly call the execution method of the corresponding scheduled task when it receives an HTTP request containing a task identifier. The Cron expression dynamic replacement module 400 is used to dynamically parse and replace the Cron expression of the target scheduled task according to the HTTP request parameters, so as to realize the real-time execution and configuration modification of the scheduled task.

[0094] Example 4 To implement the methods of the above embodiments, the present invention also provides a computer device, which includes a memory and a processor; wherein the processor runs a program corresponding to the executable program code by reading executable program code stored in the memory, so as to implement the various steps of the methods described above.

[0095] Example 5 To implement the above embodiments, this application also proposes a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the method described in the foregoing embodiments.

[0096] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

[0097] In the description of this specification, the references to "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0098] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this invention, "a plurality of" means at least two, such as two, three, etc., unless otherwise explicitly specified.

Claims

1. A method for dynamically triggering scheduled tasks via HTTP interfaces based on Javaagent, characterized in that, include: S1, Load the Javaagent agent program containing the Premain-Class attribute through JVM startup parameters. The agent program is loaded by the system class loader and the premain method is executed before the application starts. S2, Register a class converter with the class loader based on the Instrumentation instance. The class converter dynamically modifies the bytecode of the target class through the transform method. S3, identify and weave the HTTP interface triggering logic of the Controller class, so that the Controller class can directly call the execution method of the corresponding scheduled task when it receives an HTTP request containing a task identifier; S4. Dynamically parse and replace the Cron expression of the target scheduled task based on the HTTP request parameters to realize the immediate execution and configuration modification of the scheduled task.

2. The method according to claim 1, characterized in that, The process of registering a class converter with the class loader based on an Instrumentation instance includes: S21. Register a JavassistTransformer instance using the inst.addTransformer() method. The instance contains the transform(ClassLoader loader, String className, byte[] classfileBuffer) method. S22, the transform method obtains the target class definition through ClassPool.getDefault().get(className) and calls CtClass.toBytecode() to generate the modified bytecode stream.

3. The method according to claim 1, characterized in that, The HTTP interface triggering logic identified and woven into the Controller class includes: S31, the Controller class is determined by analyzing the class name and annotation information of the target class, wherein the annotation information includes @RestController or @Controller annotation; S32, dynamically generate an HTTP interface path conforming to the RESTful specification. The path format is / trigger / {taskIdentifier}, where {taskIdentifier} is the unique identifier of the scheduled task.

4. The method according to claim 1, characterized in that, The step of dynamically parsing and replacing the Cron expression of the target scheduled task based on HTTP request parameters includes: S41, use the regular expression ^\d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{4} \d{1,2}$ to validate the Cron expression format in the HTTP request parameters; S42, the cron field value of the target class is directly modified through the Javassist bytecode manipulation library. The modification operation is completed during the class loading phase and does not require restarting the JVM.

5. The method according to claim 1, characterized in that, Also includes: S5. A performance monitoring module is dynamically implanted through Javaagent. The performance monitoring module records the conversion time after each bytecode conversion and triggers an alarm mechanism when the conversion time exceeds a preset threshold.

6. A device for dynamically triggering scheduled tasks via an HTTP interface based on Javaagent, characterized in that, include: The agent loading module is used to load a Javaagent agent program containing the Premain-Class attribute via JVM startup parameters. The agent program is loaded by the system class loader and the premain method is executed before the application starts. The class converter registration module is used to register class converters with the class loader based on the Instrumentation instance. The class converters dynamically modify the bytecode of the target class through the transform method. The HTTP interface weaving module is used to identify and weave the HTTP interface triggering logic of the Controller class, so that the Controller class can directly call the execution method of the corresponding scheduled task when it receives an HTTP request containing a task identifier. The Cron expression dynamic replacement module is used to dynamically parse and replace the Cron expression of the target scheduled task based on the HTTP request parameters, so as to realize the real-time execution and configuration modification of the scheduled task.

7. A computer device, characterized in that, Including processor and memory; The processor reads executable program code stored in the memory to run a program corresponding to the executable program code, so as to implement a Javaagent-based method for dynamically triggering HTTP interfaces for timed tasks as described in any one of claims 1-5.

8. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements a method for dynamically triggering a scheduled task via an HTTP interface based on a Java agent, as described in any one of claims 1-5.