A real-time scheduling method for domain tasks based on Quartz scheduled tasks

Through the combination of Quartz timing tasks and Redis distributed locks, the problem of repeated execution and flexible configuration of timing tasks in the pre-examination system is solved, and the accuracy and reliability of timing tasks under the distributed microservice architecture is realized, ensuring real-time domain tasks. Scheduling and resource management.

CN114020436BActive Publication Date: 2024-11-12SHANGHAI PUDONG DEVELOPMENT BANK
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111320104.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-09
Publication Date
2024-11-12
Estimated Expiration
2041-11-09

AI Technical Summary

Technical Problem

The existing pre-examination system is prone to repeated execution of job tasks when timing tasks are executed, and it is impossible to flexibly configure and schedule domain tasks in real time, resulting in the inability to effectively manage timing tasks.

Method used

Using the Quartz timing task method, the flexible configuration of timing tasks is achieved by initializing the configuration table, building a multi-threaded timing task core engine, traversing the timing task configuration table, remotely calling the timing task service and recording execution data, combining Redis distributed lock and RestTemplate to realize the flexible configuration of timing tasks. and real-time scheduling.

Benefits of technology

Under the distributed microservice architecture, it avoids repeated tasks, ensures the accuracy and reliability of timing tasks, realizes concurrent startup of multi-task nodes, avoids resource competition, and supports flexible configuration of timing tasks and real-time domain tasks. Scheduling.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114020436B_ABST
    Figure CN114020436B_ABST
Patent Text Reader

Abstract

The present invention relates to a real-time scheduling method for domain tasks based on Quartz scheduled tasks, comprising the following steps: S1, configuring scheduled tasks by initializing a configuration table to obtain a scheduled task configuration table; S2, building a multi-threaded scheduled task core engine; S3, creating scheduled tasks by traversing the scheduled task configuration table; S4, remotely calling the service corresponding to the scheduled task, and recording the corresponding execution data of the scheduled task, and then returning to step S3 until all configuration information in the scheduled task configuration table is traversed. Compared with the prior art, the present invention is based on the Quartz framework, and through the scheduled task configuration table, the Redis distributed lock is used to avoid the problem of multiple scheduled tasks competing for resources; and the RestTemplate is used in combination with the scheduled task configuration table to effectively and reliably realize the flexible configuration of scheduled tasks and the real-time scheduling of domain tasks.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of task scheduling, and in particular to a field task real-time scheduling method based on Quartz timed tasks. Background Art

[0002] As the bridgehead of the credit card center's customer acquisition and credit review line, the pre-examination system involves extensive and complex business. It usually needs to adopt advanced domain-driven design concepts to build a microservice system, so as to execute specific tasks on a regular basis and achieve business goals. That is, it is necessary to use scheduled tasks to complete specific tasks at a specific time or at a specific time rate, such as sending fingerprint device data to operations every minute, obtaining online application behavior trajectory data every minute, executing VIP queue exception resending every three minutes, executing duplicate application hold item issuance tasks at four in the morning, and so on.

[0003] Most of the existing pre-examination systems use SpringCloud technology to build microservices to achieve low coupling of domain services. The domain services are called through openfeign to enable domain atomic service reuse. The system is deployed in a cluster mode, and the scheduled task service will also be started on multiple nodes. It is easy to repeat the execution of tasks. Scheduled tasks sometimes need to assume the responsibility of calling domain tasks, which means that it is impossible to flexibly configure scheduled tasks and cannot schedule domain tasks in real time. Summary of the invention

[0004] The purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a real-time scheduling method for domain tasks based on Quartz scheduled tasks, so as to avoid repeated execution of tasks and realize real-time scheduling of domain tasks.

[0005] The purpose of the present invention can be achieved by the following technical solution: A real-time scheduling method for domain tasks based on Quartz scheduled tasks, comprising the following steps:

[0006] S1. Initialize the configuration table to configure the scheduled task and obtain the scheduled task configuration table;

[0007] S2. Build a multi-threaded timed task core engine;

[0008] S3. Create a scheduled task by traversing the scheduled task configuration table;

[0009] S4. Remotely call the service corresponding to the scheduled task and record the execution data corresponding to the scheduled task, then return to step S3 until all configuration information in the scheduled task configuration table is traversed.

[0010] Furthermore, the initialization configuration table in step S1 specifically configures the core fields corresponding to the scheduled task in the scheduled task configuration table.

[0011] Furthermore, the core fields include task description, CRON expression, task group name, task name, status, scheduling path, gateway path, business parameters, Redis lock time, and Redis lock key.

[0012] Further, the numerical values ​​corresponding to the states include N and Y, wherein N is a disabled state and Y is an enabled state;

[0013] The unit of the Redis lock time is set to seconds.

[0014] Furthermore, the step S2 specifically includes the following steps:

[0015] S21, construct a thread pool scheduler;

[0016] S22. Inject the timer register into the thread pool scheduler.

[0017] Furthermore, the specific process of step S3 is as follows:

[0018] Obtain a scheduled task configuration table, traverse the configuration information one by one, and for each piece of configuration information, create a scheduled task by performing a first condition judgment, a second condition judgment, a third condition judgment, a fourth condition judgment, a fifth condition judgment, and a sixth condition judgment.

[0019] Furthermore, the first condition judgment is specifically: judging whether it is necessary to continue traversing, if the judgment is yes, executing the second condition judgment, otherwise ending the traversal process;

[0020] The second condition judgment is specifically: judging whether the configuration information is empty, if it is, returning to traverse the next configuration information, otherwise executing the third condition judgment;

[0021] The third condition judgment is specifically: judging whether the configured scheduled task already exists in the scheduled task, if it is judged to be yes, executing the fourth condition judgment, otherwise creating a new scheduled task.

[0022] Further, the fourth condition judgment is specifically: judging whether the configured scheduled task is in a disabled state, if it is judged to be yes, returning to traverse the next configuration information, otherwise executing the fifth condition judgment;

[0023] The fifth condition judgment is specifically: judging whether there is a task instance, if it is judged to be yes, executing the sixth condition judgment, otherwise deleting the existing scheduled task and creating a new scheduled task;

[0024] The sixth condition judgment is specifically: judging whether the task instance has changed, if it is judged to be, deleting the existing scheduled task and creating a new scheduled task, otherwise returning to traverse the next configuration information.

[0025] Furthermore, the step S4 specifically includes the following steps:

[0026] S41. According to the newly created scheduled task, obtain the corresponding Redis lock key from the scheduled task configuration table. If the acquisition is successful, execute step S42; otherwise, return to traverse the next configuration information;

[0027] S42, executing the remote service to call the service configured in the scheduled task configuration table, and recording the call result and the execution result of the scheduled task in the log table;

[0028] S43. Release the Redis lock, and then return to traverse the next configuration information.

[0029] Furthermore, the step S42 specifically calls the service URL configured in the scheduled task configuration table through the RestTemplate remote service, initiates an http request, completes the Post call, determines whether the call is successful by judging the response result, and adds a new log to the scheduled scheduling log table to record the execution result of the scheduled task.

[0030] Compared with the prior art, the present invention has the following advantages:

[0031] 1. The present invention is based on the Quartz framework and combined with the Redis distributed lock. By configuring the scheduled tasks in the scheduled task configuration table, it can realize the concurrent startup of multiple task nodes under the distributed microservice architecture, thereby avoiding the phenomenon of repeated execution of tasks and circumventing the problem of multiple scheduled tasks competing for resources.

[0032] 2. The present invention traverses the configuration information in the scheduled task configuration table and combines multiple set condition judgments to ensure the accuracy and reliability of scheduled task creation. At the same time, combined with remote service calls, it can not only achieve flexible configuration of scheduled tasks, but also ensure real-time scheduling of domain tasks. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 It is a schematic diagram of the method flow of the present invention;

[0034] Figure 2 It is a schematic diagram of the application process of the embodiment;

[0035] Figure 3 is a corresponding timing diagram in the embodiment;

[0036] Figure 4 It is the corresponding class diagram in the embodiment. DETAILED DESCRIPTION

[0037] The present invention is described in detail below with reference to the accompanying drawings and specific embodiments.

[0038] Example

[0039] Quartz is another open source project of the OpenSymphony open source organization in the field of jobscheduling. It can be combined with J2EE and J2SE applications or used alone. Quartz can be used to create simple or complex programs that run ten, hundreds, or even tens of thousands of jobs. Jobs can be made into standard Java components or EJBs.

[0040] The core of the Quartz framework is the scheduler. The scheduler is responsible for managing the application runtime environment of Quartz. The scheduler does not do all the work by itself, but relies on some very important components within the framework. Quartz is more than just threads and thread management. To ensure scalability, Quartz uses a multi-threaded architecture. At startup, the framework initializes a set of worker threads, which are used by the scheduler to execute scheduled jobs. This is how Quartz can run multiple jobs concurrently. Quartz relies on a loosely coupled thread pool management component to manage the thread environment.

[0041] This technical solution uses the Quartz framework to propose a Quartz-based scheduled task to achieve a solution for real-time scheduling of domain tasks.

[0042] like Figure 1 As shown, a real-time scheduling method for domain tasks based on Quartz scheduled tasks includes the following steps:

[0043] S1. By initializing the configuration table, the scheduled task is configured to obtain the scheduled task configuration table, wherein the initialization configuration table specifically configures the core fields corresponding to the scheduled task in the scheduled task configuration table, and the core fields include task description, CRON expression, task group name, task name, status (the corresponding values ​​include N and Y, N is disabled, Y is enabled), scheduling path, gateway path, business parameters, Redis lock time, and Redis lock key;

[0044] In this embodiment, the unit of Redis lock time is set to seconds;

[0045] S2. Build a multi-threaded timed task core engine: first construct a thread pool scheduler; then inject the timer register into the thread pool scheduler;

[0046] S3. Create a scheduled task by traversing the scheduled task configuration table. Specifically:

[0047] Obtain a scheduled task configuration table, traverse the configuration information one by one, and for each configuration information, create a scheduled task by performing a first condition judgment, a second condition judgment, a third condition judgment, a fourth condition judgment, a fifth condition judgment, and a sixth condition judgment;

[0048] The first condition judgment is specifically: judging whether it is necessary to continue traversing, if the judgment is yes, executing the second condition judgment, otherwise ending the traversal process;

[0049] The second conditional judgment is specifically: judging whether the configuration information is empty, if it is, returning to traverse the next configuration information, otherwise executing the third conditional judgment;

[0050] The third condition judgment is specifically: judging whether the configured scheduled task already exists in the scheduled task, if it is judged to be yes, executing the fourth condition judgment, otherwise creating a new scheduled task;

[0051] The fourth condition judgment is specifically: judging whether the configured scheduled task is in a disabled state, if it is judged to be so, returning to traverse the next configuration information, otherwise executing the fifth condition judgment;

[0052] The fifth condition judgment is specifically: judging whether a task instance exists, if it is judged to be yes, executing the sixth condition judgment, otherwise deleting the existing scheduled task and creating a new scheduled task;

[0053] The sixth condition judgment is specifically: judging whether the task instance has changed, if it is judged to be, deleting the existing scheduled task and creating a new scheduled task, otherwise returning to traverse the next configuration information;

[0054] S4. Remotely call the service corresponding to the scheduled task and record the execution data corresponding to the scheduled task, then return to step S3 until all configuration information in the scheduled task configuration table is traversed. Specifically:

[0055] First, according to the newly created scheduled task, the corresponding Redis lock key is obtained from the scheduled task configuration table. If the acquisition is successful, step S42 is executed, otherwise it returns to traverse the next configuration information;

[0056] After that, the remote service is executed to call the service configured in the scheduled task configuration table, and the call result and the execution result of the scheduled task are recorded in the log table. In this embodiment, the service URL configured in the scheduled task configuration table is called by the RestTemplate remote service, an http request is initiated, and a Post call is completed. The call is determined to be successful by judging the response result, and a new log is added to the scheduled scheduling log table to record the execution result of the scheduled task;

[0057] S43. Release the Redis lock, and then return to traverse the next configuration information.

[0058] This embodiment applies the above method, inherits the QuartzJobBean class of the Quartz framework, rewrites the executeInternal method, and combines the Redis distributed lock inside the executeInternal method to achieve the function of concurrently starting multiple task nodes without repeated execution of tasks under the distributed microservice architecture. The specific application process of this embodiment is as follows Figure 2 As shown:

[0059] (1) Configure scheduled task information. Configure scheduled tasks in the scheduled task configuration table OPAS_CFG_JOB_INFO. The core fields of the data table include: task description, CRON expression, task group name, task name, status (N: disabled, Y: enabled), scheduling path, gateway path, business parameters, redis lock time (seconds), and redis lock key.

[0060] (2) Build the timer. First, build the thread pool scheduler, then inject the timer register into the thread pool scheduler, and the multi-threaded timed task core engine is assembled.

[0061] (3) Traverse the scheduled task configuration table OPAS_CFG_JOB_INFO and start creating scheduled tasks. After obtaining the scheduled task configuration information set, traverse the configuration information one by one. First, determine whether the configuration information is empty. If it is empty, skip this traversal; if it is not empty, start to determine whether the configured scheduled task already exists in the scheduled scheduling task. If it does not exist, create a new scheduled task; if it exists, determine whether the scheduled task is disabled. If it is disabled, skip this traversal and continue to traverse the next configuration information. If it is enabled, obtain the existing task instance. If the instance does not exist, execute (4). If it exists, determine whether the task configuration information has changed. If not, skip this traversal and continue to traverse the next configuration information. If there is a change, execute (4).

[0062] (4) Refresh scheduled tasks. First, delete the existing scheduled tasks in the scheduled plan, and then start creating new scheduled tasks. Get the Redis lock key in the scheduled task configuration table OPAS_CFG_JOB_INFO, and determine whether the Redis lock is obtained. If not, skip this traversal and continue to traverse the next configuration information; if it is obtained, call the service URL configured in the scheduled task configuration table OPAS_CFG_JOB_INFO through the RestTemplate remote service, initiate an http request, complete the Post call, determine whether the call is successful by judging the response result, and add a log to the scheduled scheduling log table OPAS_BIZ_JOB_LOG to record the execution status of the scheduled task. Finally, release the Redis lock, hand over control, and then continue to traverse the next configuration information until all configuration information is traversed.

[0063] like Figure 3As shown in the timing diagram, after the project is started, the initialization load is first implemented in the configureTasks() method of SchedulingConfig. TaskScheduler constructs a multi-threaded scheduler and injects it into the ScheduledTaskRegistrar of the scheduled task register to form the core engine of the scheduled task scheduler. The scheduled task register ScheduledTaskRegistrar starts a scheduled task IntervalTask. The content of the task execution is the scheduleUpdate() method of the SchedulerRefreshService interface. The scheduleUpdate() method obtains the scheduled task configuration information by calling the findAll() method of the JobInfoService interface, and executes the executJoskInfo() method to start traversing the configuration information. exec The utJobInfo() method obtains the scheduled task by referencing the getCronTrigger() method of the ScheduleUtil class, removes the scheduled task by the deleteJob() method, refreshes the scheduled task by the updateJob() method, and creates the scheduled task by the createJob() method. The createJob() method references the executeInternal() method of the TaskJob class to start creating the scheduled task. The executeInternal() method obtains the Redis lock by using the isAbsentKey() of the RedisSupportUtils class, and executes the RestTemplate remote service call by referencing the restTemplate().postForEntity() method of the RestTemplateConfig class through the executeJob() method.

[0064] The class diagram corresponding to this embodiment is as follows Figure 4 As shown in the figure, the BaseMapper interface inherits the top-level interface Mapper, and the JobInfoMapper interface inherits BaseMapper. By overriding the parent class interface method, the atomic read and write operations of the scheduled task configuration table are implemented and the JobInfoServiceImpl implementation class is injected. The JobInfoServiceImpl class implements the JobInfoService interface and implements the business logic of the scheduled task through method overriding. The JobInfoService is referenced by the SchedulerRefreshServiceImpl class.

[0065] The JobDetail interface inherits the Serializable interface and Cloneable interface, and implements the serialization and parameter cloning functions of the scheduled task entity. The TaskJob class implements the Redis distributed lock and the scheduling of scheduled tasks by introducing the JobDetail interface, the isAbsentKey() method of the RedisSupportUtils class, and implementing the QuartzJobBean interface. The TaskJob class is referenced by the ScheduleUtil class.

[0066] SchedulerRefreshServiceImpl implements the SchedulerRefreshService interface. By introducing the Scheduler interface and combining the scheduleUpdate() method, it implements the function of refreshing the scheduled task schedule. It introduces ScheduleUtil to implement operations such as creating and removing scheduled tasks.

[0067] The SchedulingConfig class implements the SchedulingConfigurer interface. By overriding the configureTasks() method, it initializes the timer configuration and builds the scheduling engine. It introduces the SchedulerRefreshService interface to achieve real-time scheduling of domain tasks.

[0068] In summary, this technical solution configures scheduled tasks in the scheduled task configuration table OPAS_CFG_JOB_INFO; combines RestTemplate remote service and scheduled task configuration table to traverse configuration information. By using Redis distributed locks, the problem of multiple scheduled tasks competing for resources is avoided; RestTemplate is used in combination with the scheduled task configuration table to achieve flexible configuration of scheduled tasks and real-time scheduling of domain tasks.

Claims

1. A real-time scheduling method for domain tasks based on Quartz scheduled tasks, characterized in that: The following steps are involved: S1. Initialize the configuration table to configure the scheduled task and obtain the scheduled task configuration table; S2. Build a multi-threaded timed task core engine; S3. Create a scheduled task by traversing the scheduled task configuration table; S4, remotely call the service corresponding to the scheduled task, and record the execution data corresponding to the scheduled task, and then return to step S3 until all configuration information in the scheduled task configuration table is traversed; The specific process of step S3 is: Obtain a scheduled task configuration table, traverse the configuration information one by one, and for each configuration information, create a scheduled task by performing a first condition judgment, a second condition judgment, a third condition judgment, a fourth condition judgment, a fifth condition judgment, and a sixth condition judgment; The first condition judgment is specifically: judging whether it is necessary to continue traversing, if the judgment is yes, executing the second condition judgment, otherwise ending the traversal process; The second condition judgment is specifically: judging whether the configuration information is empty, if it is, returning to traverse the next configuration information, otherwise executing the third condition judgment; The third condition judgment is specifically: judging whether the configured scheduled task already exists in the scheduled task, if it is judged to be yes, executing the fourth condition judgment, otherwise creating a new scheduled task; The fourth condition judgment is specifically: judging whether the configured scheduled task is in a disabled state, if it is judged to be so, returning to traverse the next configuration information, otherwise executing the fifth condition judgment; The fifth condition judgment is specifically: judging whether there is a task instance, if it is judged to be yes, executing the sixth condition judgment, otherwise deleting the existing scheduled task and creating a new scheduled task; The sixth condition judgment is specifically: judging whether the task instance has changed, if it is judged to be, deleting the existing scheduled task and creating a new scheduled task, otherwise returning to traverse the next configuration information; The step S4 specifically comprises the following steps: S41. According to the newly created scheduled task, obtain the corresponding Redis lock key from the scheduled task configuration table. If the acquisition is successful, execute step S42; otherwise, return to traverse the next configuration information; S42, executing the remote service to call the service configured in the scheduled task configuration table, and recording the call result and the execution result of the scheduled task in the log table; S43. Release the Redis lock, and then return to traverse the next configuration information.

2. According to the method of real-time scheduling of domain tasks based on Quartz scheduled tasks in claim 1, it is characterized in that: Initializing the configuration table in step S1 specifically configures the core fields corresponding to the scheduled task in the scheduled task configuration table.

3. A method for real-time scheduling of domain tasks based on Quartz scheduled tasks according to claim 2, characterized in that: The core fields include task description, CRON expression, task group name, task name, status, scheduling path, gateway path, business parameters, Redis lock time, and Redis lock key.

4. A method for real-time scheduling of domain tasks based on Quartz scheduled tasks according to claim 3, characterized in that: The numerical values ​​corresponding to the states include N and Y, where N is a disabled state and Y is an enabled state; The unit of the Redis lock time is set to seconds.

5. According to the method of real-time scheduling of domain tasks based on Quartz scheduled tasks in claim 1, it is characterized in that: The step S2 specifically includes the following steps: S21, construct a thread pool scheduler; S22. Inject the timer register into the thread pool scheduler.

6. A method for real-time scheduling of domain tasks based on Quartz scheduled tasks according to claim 1, characterized in that: The step S42 specifically calls the service URL configured in the scheduled task configuration table through the RestTemplate remote service, initiates an http request, completes the Post call, determines whether the call is successful by judging the response result, and adds a new log in the scheduled scheduling log table to record the execution result of the scheduled task.