Methods, devices, equipment, and storage media for managing scheduled tasks
By uploading scheduled task code through a front-end visual page and dynamically compiling it using JavaCompiler, combined with custom class loading and reflection technology, the problem of scheduled task code being unable to be dynamically compiled into the production system was solved. This enabled flexible uploading and modification of scheduled task code, improving the system's flexibility and availability.
Patent Information
- Application Number
- CN202211696678.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-28
- Publication Date
- 2026-11-10
- Estimated Expiration
- 2042-12-28
AI Technical Summary
In existing technologies, new or modified scheduled task code cannot be dynamically programmed into a running production system, lacking flexibility and affecting the system's continuous operation and availability.
The scheduled task code is uploaded through a front-end visual page, dynamically compiled using JavaCompiler, and combined with custom class loading and discovery functions, reflection, and dependency injection to dynamically publish the task entity to the task object factory, thus realizing the dynamic compilation of the scheduled task code.
Without shutting down the system or releasing new versions, the system maximizes the freedom to upload, develop, modify, and release scheduled task code, thereby improving system flexibility and reducing the impact on system operation.
Smart Images

Figure CN115934288B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of timed task technology, and in particular to a method, apparatus, device and storage medium for managing timed tasks. Background Technology
[0002] Currently, the most well-known technology supporting distributed scheduled tasks is the Quartz framework, an open-source scheduled task system implemented in Java that manages tasks based on a schedule. Additionally, Spring's built-in scheduled task scheduling function, @Scheduled, combined with @EnableSchedulerLock, can also implement distributed scheduled task functionality. However, these technologies primarily focus on the scheduled tasks themselves, lacking features for tasks such as starting and pausing.
[0003] Among related technologies, improvements have been made to the processing of scheduled tasks by combining database storage, distributed locks based on the data structure server (Redis), front-end visualization, and message queues (MQ). These technologies enable functions such as real-time control over the execution, pause, resetting of the cycle, distributed operation, cluster operation, and front-end page manipulation of scheduled tasks. However, new or modified scheduled task code cannot be dynamically "integrated" into a running production system. Summary of the Invention
[0004] This application provides a method, apparatus, device, and storage medium for managing scheduled tasks, in order to solve the problem that new or modified scheduled task code cannot be dynamically "programmed" into a running production system.
[0005] Firstly, this application provides a method for managing scheduled tasks, including:
[0006] The code for the scheduled task to be published can be uploaded to the front-end visual page through any of the following methods: online coding, Java file upload, and compressed package upload.
[0007] The code and its corresponding toolkit are compiled to obtain bytecode, and the bytecode is cached in a cache mapping table;
[0008] The class discovery function and the class loading function are called in sequence. The class discovery function is used to generate the class file corresponding to the code based on the bytecode in the cache mapping table, and the class loading function is used to dynamically load the class file into the method area of the Java Virtual Machine.
[0009] The class file is instantiated using reflection to generate a task entity, and the task entity is assigned a dependency object through injection.
[0010] The assigned task entity is dynamically published to the task object factory.
[0011] One possible implementation involves assigning dependency injection values to the task entity, including: locating the fields annotated with @Autowired or @Resource in the task entity; and assigning dependency injection values to the task entity using applicationContext.getBean() and the field's Field.set().
[0012] One possible implementation involves compiling the code and its corresponding toolkit to obtain bytecode, including: traversing the toolkits that the code depends on from the security directory or lib path of the global wide area network application; passing the toolkits as parameters and calling javaCompiler to compile the code to obtain bytecode.
[0013] In one possible implementation, dynamically publishing the assigned task entity to the task object factory includes: determining whether a task entity exists in the task object factory; if not, registering the assigned task entity to the task object factory; if it exists, replacing the existing task entity in the task object factory with the assigned task entity.
[0014] In one possible implementation, after dynamically publishing the assigned task entity to the task object factory, the name attribute and code of the task entity are persistently recorded in the database.
[0015] In one possible implementation, the name attribute and code of the task entity are persistently recorded in the database, including: determining whether the name attribute exists in the database; if it exists, transferring the original scheduled task record corresponding to the name attribute in the database to the job_compile_his table, and persistently recording the name attribute and code in the job_compile table of the database; if it does not exist, persistently recording the name attribute and code in the job_compile table of the database.
[0016] In one possible implementation, after persistently recording the name attribute and code of the task entity in the database, the method further includes: obtaining incremental tasks from the schedule_job table through heartbeat polling by a daemon thread, wherein the daemon thread is started using the @PostConstruct annotation during the production system startup process; and incorporating the incremental tasks into the task management center.
[0017] In one possible implementation, the task management center is used to schedule timed tasks, and the scheduling includes at least one of execution logging, alarm notification, and failure retries.
[0018] Secondly, this application provides a management device for scheduled tasks, comprising:
[0019] The acquisition module is used to acquire the code of the scheduled task to be published. The code can be uploaded to the front-end visual page through any of the following methods: online coding, Java file upload, and compressed package upload.
[0020] The compilation module is used to compile the code and the corresponding toolkit to obtain bytecode, and cache the bytecode in the cache mapping table;
[0021] The generation module is used to call the class discovery function and the class loading function in sequence. The class discovery function is used to generate the class file corresponding to the code based on the bytecode in the cache mapping table, and the class loading function is used to dynamically load the class file into the method area of the Java Virtual Machine.
[0022] The assignment module is used to instantiate class files through reflection, generate task entities, and perform dependency object injection and assignment on the task entities.
[0023] The publishing module is used to dynamically publish the assigned task entities to the task object factory.
[0024] In one possible implementation, the assignment module can be specifically used to: locate the fields annotated with @Autowired or @Resource in the task entity; and perform dependency object injection assignment on the task entity through applicationContext.getBean() and the field's Field.set().
[0025] In one possible implementation, the compilation module can be specifically used to: traverse the toolkits that the code depends on from the security directory or lib path of the global wide area network application; pass the toolkits as parameters, and call javaCompiler to compile the code to obtain bytecode.
[0026] In one possible implementation, the publishing module can be specifically used to: determine whether a task entity exists in the task object factory; if not, register the assigned task entity to the task object factory; if it exists, replace the existing task entity in the task object factory with the assigned task entity.
[0027] In one possible implementation, the management device for scheduled tasks may further include a persistence module. After the assigned task entity is dynamically published to the task object factory, the persistence module may be specifically used to persistently record the name, attributes, and code of the task entity in the database.
[0028] In one possible implementation, the persistence module can also be used to: determine whether a name attribute exists in the database; if it exists, transfer the original scheduled task record corresponding to the name attribute in the database to the job_compile_his table, and persist the name attribute and code record in the job_compile table of the database; if it does not exist, persist the name attribute and code record in the job_compile table of the database.
[0029] In one possible implementation, the scheduled task management device may further include an inclusion module, which may be specifically used to: obtain incremental tasks from the schedule_job table through heartbeat polling by a daemon thread, wherein the daemon thread is started using the @PostConstruct annotation during the production system startup process; and include the incremental tasks in the task management center.
[0030] In one possible implementation, the management device for scheduled tasks may further include a scheduling module, which may be specifically used to schedule scheduled tasks from a task management center. The scheduling includes at least one of execution recording, alarm notification, and failure retry.
[0031] Thirdly, this application provides an electronic device, including a memory and a processor. The memory is used to store program instructions; the processor is used to invoke the program instructions in the memory to execute the management method for the timing task of the first aspect.
[0032] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed, implement the timed task management method of the first aspect.
[0033] Fifthly, this application provides a computer program product comprising a computer program, which, when executed by a processor, is used to implement the management method for the timed tasks of the first aspect.
[0034] The scheduled task management method, apparatus, device, and storage medium provided in this application obtain the code of the scheduled task to be published. The code can be uploaded to a front-end visual page through any of the following methods: online encoding, Java file upload, and compressed package upload. This allows for the uploading or writing of scheduled task code in various forms through the front-end visual page. The code and its corresponding toolkit are compiled to obtain bytecode, which is then cached in a cache mapping table. A class discovery function and a class loading function are called sequentially. The class discovery function generates the corresponding class file based on the bytecode in the cache mapping table, and the class loading function dynamically loads the class file into the method area of the Java Virtual Machine. The class file is instantiated using reflection to generate a task entity, and dependency object injection is performed on the task entity. The assigned task entity is then dynamically published to the task object factory. This allows for the dynamic "compiling" of new or modified scheduled task code into the running production system, improving system flexibility. It maximizes the freedom of uploading, developing, modifying, and publishing scheduled task code without system downtime or version releases, minimizing the impact on system operation. Attached Figure Description
[0035] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0036] Figure 1 This is a schematic diagram illustrating an application scenario of the timed task management method provided in an embodiment of this application;
[0037] Figure 2 This is a flowchart illustrating a method for managing scheduled tasks according to an embodiment of this application;
[0038] Figure 3 This is a schematic diagram of the structure of a task code uploading method provided in an embodiment of this application;
[0039] Figure 4 This is a schematic diagram of the structure of dynamic compilation of timed tasks provided in an embodiment of this application;
[0040] Figure 5 This is a schematic diagram of the structure of a scheduled task entity entering a Bean factory according to an embodiment of this application;
[0041] Figure 6 This is a schematic diagram of the structure of a system that calls a timed task that is persistently stored in the database when the system is restarted, according to an embodiment of this application.
[0042] Figure 7 This is a schematic diagram of the overall flow of timed task publishing provided in an embodiment of this application;
[0043] Figure 8 This is a schematic diagram of the structure for publishing and scheduling timed tasks according to an embodiment of this application;
[0044] Figure 9 This is a schematic diagram of the structure of a timed task management device provided in an embodiment of this application;
[0045] Figure 10 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.
[0046] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0047] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0048] The terms “first,” “second,” etc., used in the specification and claims of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented, for example, in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, products, or apparatus.
[0049] In related technologies, task scheduling methods such as starting, pausing, and status management have become fundamental elements of task center solutions, and the "platformization" approach has also been implemented by excellent open-source enterprise-level task centers. Nowadays, task scheduling can be achieved without system downtime or interruption, enhancing task availability.
[0050] In addition, an outstanding industry pioneer has open-sourced a distributed scheduled task framework (xxl-job), which solves the problems of lack of statistics, non-platformization, lack of alarms, lack of monitoring, lack of execution logs, and lack of support for failure retry in a single solution. It provides a scheduled task management center that is easy to learn, easy to maintain, and centralized platform-based.
[0051] However, a deeper analysis of the "task code" and "task itself" reveals a continued contradiction with "system operation." New or modified scheduled task code cannot be dynamically "integrated" into a running production system, nor can it be dynamically incorporated into the existing task framework or task scheduling management center.
[0052] In the current internet industry, scheduled tasks play an important role in most businesses, such as settlement and billing, unified price adjustment, and data synchronization. However, every time new scheduled task code needs to be released or the code logic of an existing scheduled task needs to be modified, it is necessary to schedule a version release or apply for an emergency version, or to release a new version or discontinue production to change the code logic of the scheduled task. This lack of flexibility conflicts with the continuous operation and availability of the system.
[0053] To address the aforementioned issues, this application proposes a scheduled task management solution. It allows users to upload or write scheduled task code (Java code) through a front-end visual page. The key technology used is JavaCompiler dynamic compilation. Since dynamic compilation is similar to the `javac` command, it allows passing in JAR files to dynamically publish scheduled task Java files and importing dependencies. By importing all system-dependent JAR files as compilation parameters, combined with custom class loading and discovery functions, and utilizing reflection and dependency injection, the assigned task entity is published to the task object factory. This enables the dynamic "compiling" of new or modified scheduled task code into the task management center of the running production system, improving system flexibility and maximizing the freedom of uploading, developing, modifying, and publishing scheduled task code while minimizing impact on system operation. Furthermore, the newly published scheduled task Java code supports features such as Spring's built-in scheduled task scheduling functionality.
[0054] Figure 1 This is a schematic diagram illustrating an application scenario of the timed task management method provided in an embodiment of this application. For example... Figure 1As shown, this application scenario consists of two parts. First, users register and input new scheduled tasks or modify existing ones on the front end. The input scheduled task information (i.e., the key fields in the task entity) includes the scheduled task identifier (job_ID), scheduled task name (bean_name), scheduled task class execution method (exec_method), scheduled task input parameters (params), scheduled expression (cron_expression), and scheduled task status (job_status), where the scheduled task status includes started and paused. The scheduled task information input on the front end is sent to the back end for adding (Create), retrieving (Retrieve), updating (Update), and deleting (Delete) scheduled tasks (i.e., backend CRUD operations), and the scheduled tasks can be persisted to the database.
[0055] In the second part, the user will register the completed task implementation class code into the Spring bean project using `@Component(bean_name)` and run it in the system. During system runtime, initialization can be completed using the `@PostConstruct` annotation, which starts a daemon thread. This daemon thread uses heartbeat polling to continuously retrieve the task list (JobList()) from the database, promptly updating and retrieving all task instance information maintained by the task management center, and maintaining it in a mapping table (Map) with the key `job_ID` and the value `task_entity`.
[0056] Then, the newly obtained task entity is compared with the original task entity in the Map based on the job_ID. The comparison result includes three cases: the new task entity already exists in the Map and is the same as the original task entity; the new task entity does not exist in the Map; and the new task entity exists in the Map but is different from the original task entity.
[0057] If a new task entity already exists in the Map and is identical to the original task entity, then continue executing subsequent operations, such as scheduled task scheduling.
[0058] If the new task entity does not exist in the Map, then it's a "registration function." Various key fields can be extracted from the new task entity. Using the Quartz framework, QuartzJobBean is used as the task base class, and task information (JobDetail) is created with job_ID as the identifier. The task entity is stored in the JobDetail. Then, the cron expression of the task entity is extracted to construct a timer (CronScheduleBuilder) and a trigger (CronTrigger). Finally, the task entity is added to the Quartz framework's task management center. When the Quartz framework's CronSchedule is triggered at the pre-set time, the task base class executes the execute() method under the Quartz framework's call. After the method is overridden, the task entity is retrieved from the JobDetail, and key attributes (such as beanName) are obtained from the task entity. Using beanName, the actual task class is retrieved from the Spring context. Based on the method field, the scheduled task method is obtained through reflection. Then, the time-related function (invoke()) is called using reflection with parameters (param) to implement the scheduled task call. Aspects can then be used for logging. Furthermore, daemon threads can be used to support retries on failure.
[0059] If a new task entity already exists in the Map but differs from the original task entity, it falls under the "scheduling function." The task with this job_ID is retrieved from the task base class managed by the Quartz framework. Then, based on the key information of the newly returned task entity, the task base class (e.g., cron, start / pause, etc.) is reconstructed, replacing the original task base class. The task is then re-executed based on the new task entity information, thus changing the task scheduling logic. Changes to or real-time updates of the task entity can be implemented through modifications to the front-end page. After the task entity is updated, the subsequent steps are implemented similarly to the "registration function," and will not be elaborated here.
[0060] Furthermore, the scheduled task management method provided in this application can also perform at least one of execution recording, alarm notification, and failure retry during the scheduled task scheduling process, to record the scheduled task scheduling process, whether the scheduling is successful, and to provide an alarm and reschedule after scheduling failure.
[0061] The following is combined Figure 1 Application scenarios, refer to Figure 2 This application describes a method for managing timed tasks according to exemplary embodiments thereof. It should be noted that the above application scenarios are shown only to facilitate understanding of the spirit and principles of this application, and the embodiments of this application are not limited to those described herein. Figure 1 The limitations of the application scenarios shown.
[0062] Figure 2 This is a flowchart illustrating a method for managing scheduled tasks according to an embodiment of this application. Figure 2 As shown, the method for managing scheduled tasks in this application embodiment includes the following steps:
[0063] S201: Obtain the code for the scheduled task to be published. The code can be uploaded to the front-end visual page via any of the following methods: online coding, Java file upload, or compressed package upload.
[0064] Figure 3 This is a schematic diagram illustrating the structure of a task code uploading method provided in an embodiment of this application. In this step, as... Figure 3 As shown, users can upload task code, i.e. the code of the scheduled task to be published, through various methods such as online coding (or Java online page coding), Java file upload, and compressed package upload (or JAR package compressed package upload) on the front-end visual page. Among them, compressed package upload is the task code upload method used when there are dependencies that do not exist in the system, which can achieve the purpose of adding dependent JAR packages.
[0065] Optionally, after the code for the scheduled task to be published is successfully uploaded, the code can be converted into corresponding bytecode using a custom JavaFileManager and JavaFileObject.
[0066] S202: Compile the code and the corresponding toolkit to obtain bytecode, and cache the bytecode in the cache mapping table.
[0067] In some embodiments, compiling the code and its corresponding toolkit to obtain bytecode may include: traversing the toolkits that the code depends on from the security directory (WEB-INF) or lib path of the global wide area network application; passing the toolkits as parameters and calling javaCompiler to compile the code to obtain bytecode. For example, the toolkits corresponding to the code can be traversed from the WEB-INF or lib path through the JAR files that the project management tool (Maven) depends on, passed as parameters, and used to call javaCompiler for compilation (similar to the javac command), which can return bytecode content and cache the bytecode in a cache mapping table.
[0068] In this step, javaCompiler is called, passing all JAR files under WEB-INF or lib as compilation parameters. This enables the dynamically compiled new scheduled task Java class to easily use all existing system dependencies. Consequently, it allows other existing task management centers to be perfectly integrated with the dynamically compiled and published scheduled task method provided in this application, exhibiting excellent extensibility and versatility.
[0069] S203: The class discovery function and the class loading function are called in sequence. The class discovery function is used to generate the class file corresponding to the code based on the bytecode in the cache mapping table. The class loading function is used to dynamically load the class file into the method area of the Java Virtual Machine.
[0070] In this step, users can define custom class discovery functions (findClass) and class loading functions (ClassLoad). Optionally, since the code for the scheduled task to be published uploaded through the front-end visualization page, i.e., the bytecode file, does not exist in the project's default classPath but in a user-defined bytecode cache mapping table, the custom findClass function can be used to generate the corresponding class file (.class file) from the bytecode in the cache mapping table using loadClass(), and then the custom ClassLoad function can be used to dynamically load the new scheduled task, i.e., the scheduled task to be published, into the method area of the Java Virtual Machine (JVM).
[0071] Based on the above embodiments, Figure 4 This is a schematic diagram illustrating the structure of dynamic compilation of a timed task according to an embodiment of this application. Figure 4 As shown, this code uses JavaCompiler to dynamically compile Java files for scheduled tasks uploaded via an API or extracted from a database (i.e., the scheduled tasks to be published). First, a custom JavaFileManager and JavaFileObject are used to convert the obtained Java file content into bytecode. Maven dependency JAR files are retrieved from the WEB-INF or lib path and passed as parameters to the next step of dynamic compilation. Then, JavaCompiler is called to compile the results of the first two steps (similar to the javac command), returning the bytecode content and storing it in a cache Map. Finally, custom ClassLoad and findClass methods are defined to read the bytecode obtained in the previous steps from the cache Map, and loadClass() generates the corresponding .class file. The custom ClassLoad then dynamically loads the new scheduled task's Class file into the JVM's method area.
[0072] S204: Instantiate the class file using reflection, generate the task entity, and assign values to the task entity by injecting dependent objects.
[0073] This step, based on the above embodiment, instantiates the dynamically compiled .class file through reflection to generate the task entity.
[0074] In some embodiments, since the scheduled task instantiated using reflection is not automatically loaded by Spring, the dependency injection assignment of the task entity may include: locating the fields annotated with @Autowired or @Resource in the task entity; and performing dependency injection assignment of the task entity through applicationContext.getBean() and the field's Field.set().
[0075] S205: Dynamically publish the assigned task entity to the task object factory.
[0076] In this step, the application context and bean factory can be used to dynamically publish the assigned task entity to the task object factory, thus dynamically adding or replacing task instances in the bean factory. For example, an existing scheduled task framework can retrieve the corresponding bean from the bean factory based on the configured beanName, and then use reflection to call the scheduled task execution method to register the new task entity assigned by dependency injection in step S204 into the bean factory, thereby integrating the new scheduled task into the task management center.
[0077] The scheduled task management method provided in this application embodiment enables the uploading or writing of various forms of scheduled task code to be published through a front-end visual page. It uses JavaCompiler to dynamically compile the scheduled task code and its corresponding toolkit. Combined with custom class loading and class discovery functions, the compiled bytecode is used to generate class files. Reflection and dependency injection are used to assign the task entity corresponding to the class file, and the task entity is published to the task object factory. This allows for the dynamic "compiling" of new or modified scheduled task code into the running production system, improving system flexibility. It maximizes the freedom of uploading, developing, modifying, and publishing scheduled task code without system downtime or release issues, minimizing the impact on system operation.
[0078] Based on the above embodiments, Figure 5 This is a schematic diagram illustrating the structure of a scheduled task entity entering a Bean factory, as provided in one embodiment of this application. Figure 5As shown, this method utilizes the Spring framework and reflection-based Application Programming Interfaces (APIs) to implement the instantiation, dependency injection, and registration of scheduled tasks into the bean factory. Specifically, reflection is first used to instantiate the dynamically compiled .class file into a task entity. Then, the fields annotated with `@Autowired` or `@Resource` are located within the task entity, and dependency injection is performed on the task entity using `applicationContext.getBean()` and `Field.set()`. The `applicationContext` and `BeanFactory` can be used to retrieve the task entity (bean entity) by passing the beanName from the front-end visualization page as an input parameter, and the assigned bean entity is dynamically published to the task object factory or bean factory.
[0079] For example, dynamically publishing the assigned task entity to the task object factory can include: determining whether the task entity exists in the task object factory; if not, registering the assigned task entity in the task object factory; if it exists, replacing the existing task entity in the task object factory with the assigned task entity. Specifically, it checks whether the bean factory already contains a bean entity with the name attribute (beanName) of this task entity. If not, it's task registration; the newly scheduled task entity, after being copied using the dependency object injection above, can be registered in the bean factory with the name beanName, and the beanName and the code (i.e., the Java file content) of the scheduled task to be published can be persistently recorded in the job_compile table of the database. If yes, it's task modification; the old task object can be removed and a new task object placed in it, realizing dynamic task replacement during system operation, and the beanName and the Java file content can be persistently recorded in the job_compile table of the database, while the database information corresponding to the old task is moved into the job_compile_his table.
[0080] In some embodiments, persistently recording the name attribute and code of a task entity in the job_compile table of a database may include: determining whether the name attribute exists in the database; if it exists, transferring the original scheduled task record corresponding to the name attribute in the database to the job_compile_his table, and persistently recording the name attribute and code in the job_compile table of the database; if it does not exist, persistently recording the name attribute and code in the job_compile table of the database.
[0081] Based on practical considerations, since scheduled tasks uploaded through the front-end visual page may be cleared in certain situations, such as system re-releases, the name, attributes, and code (Java file content) of the task entities uploaded using the scheduled task management method provided in this application will be persistently recorded in the database. If the system is re-released, incremental tasks stored persistently in the `schedule_job` table can be retrieved through a daemon thread heartbeat polling. The daemon thread is started using the `@PostConstruct` annotation during the production system startup process; incremental tasks are then incorporated into the task management center. The `schedule_job` table can be accessed by modifying or registering the field attributes of scheduled tasks through the front-end visual page. These field attributes include the scheduled task's `jobId`, `beanName`, `execMethod`, `Params`, `cronExpression`, and `jobStatus`. The `beanName` and other attributes of scheduled tasks in the `schedule_job` table can be retrieved through daemon thread heartbeat polling, allowing the corresponding scheduled task to be found in the persistent database.
[0082] Figure 6 This is a schematic diagram illustrating the structure of a system that invokes a timed task persistently stored in a database during system re-run, as provided in an embodiment of this application. Figure 6 As shown, after the system restarts, the @PostConstruct annotation can be used to obtain the list of tasks in the job_compile table of the database after the bean factory is initialized through the heartbeat polling of the daemon thread. Based on the task list, the scheduled task Java files stored in the database are traversed and compiled.
[0083] For example, if it's a one-time type, such as discovering problematic data in the system and modifying that data, then reflection can be used to execute the task one-time, or it can be executed one-time using a Groovy script. Specifically, by utilizing Groovy scripts or JavaCompiler, along with Spring's applicationContext and BeanFactory, it's possible to compile and execute a completely new, independent, and unstructured scheduled task in real-time without interrupting operations, releases, or system operation.
[0084] Corresponding to the above embodiments, the task management center can be used to schedule timed tasks, and the scheduling includes at least one of execution records, alarm notifications, and failure retries.
[0085] Figure 7This is a schematic diagram illustrating the overall workflow of a scheduled task deployment according to an embodiment of this application. The scheduled task Java file can be uploaded via a front-end visual page, and the corresponding toolkit can be obtained. Using JavaCompiler, JavaFileManager, and JavaFileObject, the corresponding task.class file is obtained. Then, a custom ClassLoad and findclass functions are used to obtain the task class object, which is dynamically loaded into the JVM's method area. Next, a task entity is created using the reflection function newInstant(), and the task class object from the method area is moved to the heap area to obtain the task instance object. By locating the context of the @Autowired or @Resource annotation in the task instance object using getBean(), and reflecting using Field.set(), the task instance object's dependent objects, such as xxxServerice, are injected and assigned. Finally, registerSingleton() can be used to put the task instance object into Spring's BeanFactory, completing the deployment of the scheduled task.
[0086] This application provides a method for managing scheduled tasks, which can be divided into two parts for implementing distributed tasks or scheduled task publishing and scheduling. The first part (the secondary part) is a task scheduling module composed of the Spring framework, the Quartz framework, and a persistent database. The second part (the primary part) is a module that dynamically updates and publishes scheduled tasks without interrupting business, releasing new versions, or affecting system operation. This module uses the Java Compiler of the Java Development Kit (JDK), the applicationContext and BeanFactory of the Spring framework, and even Groovy scripts to implement the code logic of the scheduled tasks, i.e., the tasks themselves.
[0087] For example, Figure 8 This is a schematic diagram illustrating the structure of a timed task publishing and scheduling system provided in one embodiment of this application. Figure 8As shown, there are three ways to obtain scheduled tasks (or task implementation class entities) from Spring's Bean Factory, thereby enabling scheduled task scheduling. The first is to use scheduled tasks that already exist in the backend codebase, which can be loaded directly after the system starts. The second is to load scheduled tasks from the `job_compile` table in the persistent database after system or project initialization. The third is for users to upload scheduled task code through a front-end visual page. During system operation, the scheduled task management method provided in this application uses JavaCompiler combined with reflection and the Spring framework API to "publish" the scheduled tasks to the Bean Factory, achieving dynamic compilation. The Bean Factory acts as a medium, storing task implementation class entities. When dynamically published scheduled tasks enter the Bean Factory, users can modify or register the task's field attributes through a front-end visual interface in the `schedule_job` table. The user then passes the task's `jobId`, `beanName`, `execMethod`, `Params`, `cronExpression`, and `jobStatus` fields to the Quartz framework. The QuartzJobBean in the Quartz framework retrieves the task implementation class entity from the Spring context using attributes like `beanName` and uses reflection to call the task implementation class entity in the Bean Factory. By modifying or registering the scheduled task's field attributes in the `schedule_job` table through the front-end visual interface, the dynamically published scheduled task is successfully incorporated into the task management center, bridging the gap between JavaCompiler and the Quartz framework, and enabling the "publishing" and "scheduling" of scheduled tasks.
[0088] As can be seen from the above embodiments, scheduled tasks can be dynamically "programmed" into the running production system, and can be combined with scheduled task management methods to give the scheduled task management methods the function of dynamically publishing scheduled tasks.
[0089] In summary, this application has at least the following advantages:
[0090] 1. If an emergency system failure (bug) occurs during system production, the scheduling system can only pause the task. However, the method provided in this application allows for the uploading or writing of scheduled task code through a front-end visual page without interrupting system production or requiring a new version release. The code can be dynamically compiled during system operation, making the scheduled task effective immediately and incorporated into the scheduling management of the task management center. This maximizes the freedom of uploading, developing, modifying, and releasing scheduled task code, minimizes the impact on system operation, greatly improves task flexibility, and decouples the release of scheduled tasks from the system to the greatest extent possible.
[0091] 2. It resolves the contradiction between the release and modification of "task code" and "task itself" and "system operation" and "task production operation", and elevates the convenience of task solution from the superficial "task scheduling" level to the deep "task code" and "task itself" level.
[0092] 3. Furthermore, it establishes a one-stop center for task management, allowing users to manage the entire lifecycle of a task, from coding and development, uploading and compiling, integration into the running system, real-time scheduling, exception handling, real-time monitoring, and finally task removal. Among these features, online task coding and development, uploading and compiling, and integration into the running system are functionalities not available in existing scheduled task management methods.
[0093] 4. The technology used in the method provided in this application can be integrated with most existing Java-based task centers, such as the open-source XXL-Job scheduled task framework, allowing users to perform secondary development and add the functionality of non-destructive deployment and modification of scheduled task code to existing task centers. Furthermore, other task scheduling centers can also replace the Quartz framework combined with Spring's task scheduling structure in the method provided in this application to achieve secondary development.
[0094] The following are embodiments of the apparatus described in this application, which can be used to execute the embodiments of the method described in this application. For details not disclosed in the apparatus embodiments of this application, please refer to the embodiments of the method described in this application.
[0095] Figure 9 This is a schematic diagram of a timed task management device provided according to an embodiment of this application. For ease of explanation, only the parts relevant to the embodiment of this application are shown. Figure 9 As shown, the management device 90 for the scheduled task includes: an acquisition module 91, a compilation module 92, a generation module 93, an assignment module 94, and a publishing module 95. Wherein:
[0096] Module 91 is used to obtain the code of the scheduled task to be published. The code can be uploaded to the front-end visual page through any of the following methods: online coding, Java file upload, and compressed package upload.
[0097] Compiler module 92 is used to compile the code and the corresponding toolkit to obtain bytecode, and cache the bytecode in the cache mapping table;
[0098] The generation module 93 is used to call the class discovery function and the class loading function in sequence. The class discovery function is used to generate the class file corresponding to the code based on the bytecode in the cache mapping table, and the class loading function is used to dynamically load the class file into the method area of the Java Virtual Machine.
[0099] The assignment module 94 is used to instantiate class files through reflection, generate task entities, and perform dependency object injection and assignment on the task entities.
[0100] The publishing module 95 is used to dynamically publish the assigned task entity to the task object factory.
[0101] In one possible implementation, the assignment module 94 can be specifically used to: locate the fields annotated with @Autowired or @Resource in the task entity; and perform dependency object injection assignment on the task entity through applicationContext.getBean() and the field's Field.set().
[0102] In one possible implementation, the compilation module 92 can be specifically used to: traverse the toolkits that the code depends on from the security directory or lib path of the global wide area network application; pass the toolkits as parameters, and call javaCompiler to compile the code to obtain bytecode.
[0103] In one possible implementation, the publishing module 95 can be specifically used to: determine whether a task entity exists in the task object factory; if not, register the assigned task entity to the task object factory; if it exists, replace the existing task entity in the task object factory with the assigned task entity.
[0104] In one possible implementation, the management device for scheduled tasks may further include a persistence module. After the assigned task entity is dynamically published to the task object factory, the persistence module may be specifically used to persistently record the name, attributes, and code of the task entity in the database.
[0105] In one possible implementation, the persistence module can also be used to: determine whether a name attribute exists in the database; if it exists, transfer the original scheduled task record corresponding to the name attribute in the database to the job_compile_his table, and persist the name attribute and code record in the job_compile table of the database; if it does not exist, persist the name attribute and code record in the job_compile table of the database.
[0106] In one possible implementation, the scheduled task management device may further include an inclusion module, which may be specifically used to: obtain incremental tasks from the schedule_job table through heartbeat polling by a daemon thread, wherein the daemon thread is started using the @PostConstruct annotation during the production system startup process; and include the incremental tasks in the task management center.
[0107] In one possible implementation, the management device for scheduled tasks may further include a scheduling module, which may be specifically used to schedule scheduled tasks from a task management center. The scheduling includes at least one of execution recording, alarm notification, and failure retry.
[0108] The timed task management device provided in this application embodiment has a similar implementation principle and technical effect to the above embodiments. For details, please refer to the above embodiments, which will not be repeated here.
[0109] Figure 10 This is a schematic diagram of the structure of an electronic device provided according to an embodiment of this application. For example, the electronic device may be provided as a server. Figure 10 As shown, the electronic device 100 includes:
[0110] Processor 1010, further comprising one or more processors, and memory resources represented by memory 1020 for storing instructions executable by processor 1010, such as application programs. The application programs stored in memory 1020 may include one or more modules, each corresponding to a set of processing instructions. Furthermore, processor 1010 is configured to execute instructions to perform the aforementioned timed task management method.
[0111] Electronic device 100 may also include a power supply component 1030 configured to perform power management of electronic device 100, a wired or wireless network interface 1040 configured to connect electronic device 100 to a network, and an input / output interface 1050. Electronic device 100 may operate on an operating system stored in memory 1020, such as x86, Windows Server™, Mac OS X™, Unix™, Linux™, FreeBSD™, or similar.
[0112] Figure 10 The processor 1010 mentioned can be a general-purpose processor, including a central processing unit, a network processor (NP), etc.; a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0113] The memory 1020 may include random access memory (RAM), and may also include static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk, such as at least one disk storage device.
[0114] Those skilled in the art will understand that Figure 10 The electronic devices shown do not constitute a limitation on electronic devices and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0115] This application also provides a computer-readable storage medium storing computer-executable instructions. When the computer-executable instructions are executed, the above-mentioned timed task management method is implemented.
[0116] This application also provides a computer program product, including a computer program that, when executed, implements the above-mentioned method for managing timed tasks.
[0117] This application also provides a chip for executing instructions, which is used to manage timed tasks as described in any of the above method embodiments.
[0118] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and corresponding operation portals are provided for users to choose to authorize or refuse.
[0119] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.
[0120] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A method for managing scheduled tasks, characterized in that, include: Obtain the code for the scheduled task to be published. The code can be uploaded to the front-end visual page through any of the following methods: online coding, Java file upload, and compressed package upload. The code depends on the toolkits that are accessed by the security directory or lib path of the global wide area network application. The toolkits are passed as parameters and the javaCompiler is called to compile the code to obtain bytecode. The bytecode is then cached in the cache mapping table. The class discovery function and the class loading function are called sequentially. The class discovery function is used to generate the class file corresponding to the code based on the bytecode in the cache mapping table. The class loading function is used to dynamically load the class file into the method area of the Java Virtual Machine. The class file is instantiated using reflection to generate a task entity. The fields annotated with @Autowired or @Resource in the task entity are located, and the task entity is assigned a dependency object through applicationContext.getBean() and the field's Field.set(). The assigned task entity is dynamically published to the task object factory.
2. The management method according to claim 1, characterized in that, The step of dynamically publishing the assigned task entity to the task object factory includes: Determine whether the task entity exists in the task object factory. If it does not exist, the assigned task entity is registered to the task object factory; If it exists, the existing task entity in the task object factory will be replaced with the assigned task entity.
3. The management method according to any one of claims 1 to 2, characterized in that, Also includes: After dynamically publishing the assigned task entity to the task object factory, the name attribute of the task entity and the code are persistently recorded in the database.
4. The management method according to claim 3, characterized in that, The step of persistently recording the name attribute of the task entity and the code in the database includes: Determine whether the name attribute exists in the database; If it exists, the original scheduled task record corresponding to the name attribute in the database is transferred to the job_compile_his table, and the name attribute and the code are persisted in the job_compile table of the database. If it does not exist, the name attribute and the code are persistently recorded in the job_compile table of the database.
5. The management method according to claim 3, characterized in that, After persistently recording the name attribute of the task entity and the code to the database, the method further includes: The incremental tasks in the schedule_job table are obtained through heartbeat polling by a daemon thread, which is started using the @PostConstruct annotation during the production system startup process; The incremental tasks are then incorporated into the task management center.
6. The management method according to claim 5, characterized in that, The task management center is used to schedule timed tasks, and the scheduling includes at least one of execution records, alarm notifications, and failure retries.
7. A management device for timed tasks, characterized in that, include: The acquisition module is used to acquire the code of the scheduled task to be published. The code can be uploaded to the front-end visualization page through any of the following methods: online coding, Java file upload, and compressed package upload. The compilation module is used to traverse the toolkits that the code depends on from the security directory or lib path of the global wide area network application, pass the toolkits as parameters, call javaCompiler to compile the code, obtain bytecode, and cache the bytecode in the cache mapping table; The generation module is used to sequentially call the class discovery function and the class loading function. The class discovery function is used to generate the class file corresponding to the code based on the bytecode in the cache mapping table. The class loading function is used to dynamically load the class file into the method area of the Java Virtual Machine. The assignment module is used to instantiate the class file through reflection, generate a task entity, locate the fields annotated with @Autowired or @Resource in the task entity, and perform dependency object injection assignment on the task entity through applicationContext.getBean() and Field.set() of the field; The publishing module is used to dynamically publish the assigned task entities to the task object factory.
8. The management device according to claim 7, characterized in that, The management device further includes a scheduling module, which is used to schedule timed tasks from the task management center. The scheduling includes at least one of execution records, alarm notifications, and failure retries.
9. An electronic device, characterized in that, include: Memory and processor; The memory is used to store program instructions; The processor is used to invoke program instructions in the memory to execute the management method for timed tasks as described in any one of claims 1 to 6.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed, implement the timed task management method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Web system upgrading method and device, electronic equipment and computer readable medium
CN114816469A
Script language operation management method and system based on virtual machine security sandbox
CN115168838A
