Timing task scheduling method and device, computer device and storage medium

By combining a hierarchical time wheel strategy with a delayed queue and a dead letter exchange, the problem of low scheduling efficiency in existing timed tasks is solved, and efficient and controllable execution of timed tasks and resource utilization are achieved.

CN114168281BActive Publication Date: 2026-06-05CHINA CONSTRUCTION BANK

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
CHINA CONSTRUCTION BANK
Filing Date
2021-11-25
Publication Date
2026-06-05

AI Technical Summary

Technical Problem

Existing scheduled task scheduling methods are inefficient, waste resources, and can affect the execution of other tasks when a task fails to execute, making it impossible to effectively control the timing error of scheduled tasks.

Method used

A hierarchical time wheel strategy is adopted. By obtaining the event messages of the scheduled task, the granularity of the hierarchical time wheel is determined according to the business data, the corresponding delay queue is obtained and bound to the dead letter exchange. Events are registered to the delay queue, and event consumption is performed based on the effective delay queue and the bound dead letter exchange. The hierarchical time wheel strategy is used for cascading scheduling. When the triggering condition is met, the event is written to the task execution queue to execute the scheduled task.

Benefits of technology

It achieves efficient scheduled task scheduling, avoids message congestion, controls the time error of scheduled tasks, and improves system performance and resource utilization efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114168281B_ABST
    Figure CN114168281B_ABST
Patent Text Reader

Abstract

The application relates to a timing task scheduling method and device, computer equipment, a storage medium and a computer program product, wherein the method comprises the following steps: acquiring an event message of a timing task; determining a layered time wheel granularity according to service data carried by the event message, acquiring a delay queue corresponding to the layered time wheel granularity, and binding the delay queue with a dead letter exchange; registering an event corresponding to the event message to the delay queue to obtain an effective delay queue; consuming the event based on the effective delay queue and the bound dead letter exchange by adopting a layered time wheel strategy; and when an event triggering condition is met, writing the event into a task execution queue to execute the timing task. In the whole process, the delay consumption of the message can be realized based on the delay queue and the dead letter exchange, the consumption scheduling is realized by adopting the layered time wheel strategy, the message congestion is effectively avoided, and the efficient timing task scheduling is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of big data resource scheduling technology, and in particular to a timed task scheduling method, apparatus, computer equipment, storage medium, and computer program product. Background Technology

[0002] In practical application development, scheduled task scheduling is an indispensable service. Scheduled task scheduling is a pre-set task that is executed periodically or once.

[0003] Currently, Linux's built-in cron job can execute specified system commands or shell scripts at fixed time intervals. The JDK's built-in Timer job execution class is simple and convenient to use.

[0004] However, starting a timer for each scheduled task is inefficient and time-consuming. Furthermore, if one task takes too long or encounters an error, it will affect the execution of other tasks. Spring's Scheduler schedules use database polling, which requires continuously consuming threads and maintaining database connections to determine whether to execute the task, putting significant pressure on the database and resulting in large time errors. Quartz uses Triggers to set scheduling strategies: SimpleTrigger executes tasks once or multiple times at specific time intervals, while CronTrigger uses calendar-based scheduling for more complex arrangements. RabbitMQ uses message TTL (Time To Live) and dead-letter queues to implement delayed tasks for scheduled execution. However, due to the first-in, first-out (FIFO) nature of queues, only expired messages at the head of the queue are routed to the dead-letter queue, requiring consistent execution times for each task, greatly limiting its use cases. Alternatively, ensuring messages with the same delay time enter the same queue and binding different queues for different scheduling requirements wastes queue resources and leads to inefficient scheduled task scheduling. Summary of the Invention

[0005] Therefore, it is necessary to provide an efficient method, apparatus, computer device, and storage medium for scheduling timed tasks to address the aforementioned technical problems.

[0006] A method for scheduling timed tasks, the method comprising:

[0007] Retrieve event messages from scheduled tasks;

[0008] The granularity of the hierarchical time wheel is determined based on the service data carried in the event message, and a delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is bound to the dead letter exchange.

[0009] Register the event corresponding to the event message to the delay queue to obtain the effective delay queue;

[0010] The events are consumed using a hierarchical time wheel strategy based on an effective delay queue and a bound dead-letter exchange.

[0011] When the event triggering condition is met, the event is written into the task execution queue to execute the scheduled task.

[0012] In one embodiment, consuming the events using a hierarchical time wheel strategy based on an effective delay queue and a bound dead-letter exchange includes:

[0013] Monitor the cyclic consumption of events in the valid delay queues at all time granularities at the dead letter exchange, and use a hierarchical time wheel strategy to cascade the cyclic consumption.

[0014] In one embodiment, the monitoring of events in the effective delay queues at all time granularities during cyclic consumption at the dead-letter exchange, and the cascading scheduling of cyclic consumption using a hierarchical time wheel strategy, includes:

[0015] Bind all time-granularity effective delay queues to the same dead-letter exchange;

[0016] Events in a single valid delay queue are consumed cyclically at the same dead-letter exchange, and a hierarchical time wheel strategy is used to cascade the cyclic consumption.

[0017] In one embodiment, the step of cyclically consuming events in a single valid delay queue at the same dead-letter exchange, and cascading the cyclic consumption using a hierarchical time-wheel strategy, includes:

[0018] Determine the effective delay queue with the highest time granularity to obtain the target effective delay queue;

[0019] When the target effective delay queue flows once at the corresponding time granularity, the event is consumed once by the same dead letter exchange;

[0020] When the number of cyclic consumptions reaches the value of the current time granularity corresponding to the scheduled task, the effective delay queue of the next time granularity is selected as the target effective delay queue, and the process returns to the step where the event enters the same dead letter exchange once when the target effective delay queue flows once with the corresponding time granularity, until all effective delay queues of all time granularities are cycled through.

[0021] In one embodiment, the event message for obtaining the scheduled task includes:

[0022] Get scheduled tasks;

[0023] The business data of the scheduled task is encapsulated into an event message.

[0024] In one embodiment, determining the granularity of the hierarchical time wheel based on the service data carried in the event message includes:

[0025] Identify the task execution cycle based on the aforementioned business data;

[0026] Identify the minimum and maximum time units in the task execution cycle;

[0027] The minimum granularity of the layered time wheel is determined based on the minimum time unit, and the maximum granularity of the layered time wheel is determined based on the maximum time unit.

[0028] In one embodiment, the step of writing the event into the task execution queue to execute the scheduled task when the event triggering condition is met includes:

[0029] When the event triggering condition is met, the event is written into the task execution queue to obtain a valid task execution queue;

[0030] The preset task callback function is invoked to execute the valid task execution queue, and the preset task callback function corresponds to the timed task.

[0031] A timed task scheduling device, the device comprising:

[0032] The task acquisition module is used to acquire event messages from scheduled tasks.

[0033] The queue acquisition module is used to determine the granularity of the hierarchical time wheel based on the service data carried by the event message, and acquire the delayed queue corresponding to the granularity of the hierarchical time wheel. The delayed queue is bound to the dead letter exchange.

[0034] The registration module is used to register the event corresponding to the event message to the delay queue to obtain a valid delay queue.

[0035] The circular consumption module is used to consume the events based on an effective delay queue and a bound dead-letter exchange, using a hierarchical time wheel strategy.

[0036] The execution module is used to write the event into the task execution queue to execute the scheduled task when the event triggering condition is met.

[0037] In one embodiment, the circular consumption module is also used to monitor the circular consumption of events in the valid delay queues of all time granularities at the dead letter exchange, and to use a hierarchical time wheel strategy to cascade the circular consumption.

[0038] In one embodiment, the circular consumption module is also used to bind all valid delay queues of all time granularities to the same dead-letter exchange; to circulate the events in a single valid delay queue at the same dead-letter exchange, and to cascade the circular consumption using a hierarchical time wheel strategy.

[0039] In one embodiment, the cyclic consumption module is further configured to determine the effective delay queue with the highest time granularity to obtain a target effective delay queue; when the target effective delay queue circulates once with the corresponding time granularity, the event enters the same dead-letter exchange for consumption once; when the number of cyclic consumptions reaches the value of the current time granularity corresponding to the timed task, the effective delay queue with the next time granularity is selected as the target effective delay queue, and the step of entering the same dead-letter exchange for consumption once with the corresponding time granularity when the target effective delay queue circulates once is returned, until all effective delay queues with all time granularities are cyclically consumed.

[0040] In one embodiment, the task acquisition module is further configured to acquire a scheduled task and encapsulate the business data of the scheduled task into an event message.

[0041] In one embodiment, the queue acquisition module is further configured to identify the task execution cycle based on the business data; identify the minimum time unit and the maximum time unit in the task execution cycle; determine the lowest granularity of the hierarchical time wheel based on the minimum time unit, and determine the highest granularity of the hierarchical time wheel based on the maximum time unit.

[0042] In one embodiment, the execution module is further configured to write the event into the task execution queue when the event triggering condition is met, thereby obtaining a valid task execution queue; and to call a preset task callback function to execute the valid task execution queue, wherein the preset task callback function corresponds to the timed task.

[0043] A computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program performing the following steps:

[0044] Retrieve event messages from scheduled tasks;

[0045] The granularity of the hierarchical time wheel is determined based on the service data carried in the event message, and a delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is bound to the dead letter exchange.

[0046] Register the event corresponding to the event message to the delay queue to obtain the effective delay queue;

[0047] The events are consumed using a hierarchical time wheel strategy based on an effective delay queue and a bound dead-letter exchange.

[0048] When the event triggering condition is met, the event is written into the task execution queue to execute the scheduled task.

[0049] A computer-readable storage medium having a computer program stored thereon, the computer program performing the following steps when executed by a processor:

[0050] Retrieve event messages from scheduled tasks;

[0051] The granularity of the hierarchical time wheel is determined based on the service data carried in the event message, and a delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is bound to the dead letter exchange.

[0052] Register the event corresponding to the event message to the delay queue to obtain the effective delay queue;

[0053] The events are consumed using a hierarchical time wheel strategy based on an effective delay queue and a bound dead-letter exchange.

[0054] When the event triggering condition is met, the event is written into the task execution queue to execute the scheduled task.

[0055] A computer program product. The computer program product includes a computer program that, when executed by a processor, performs the following steps:

[0056] Retrieve event messages from scheduled tasks;

[0057] The granularity of the hierarchical time wheel is determined based on the service data carried in the event message, and a delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is bound to the dead letter exchange.

[0058] Register the event corresponding to the event message to the delay queue to obtain the effective delay queue;

[0059] The events are consumed using a hierarchical time wheel strategy based on an effective delay queue and a bound dead-letter exchange.

[0060] When the event triggering condition is met, the event is written into the task execution queue to execute the scheduled task.

[0061] The aforementioned scheduled task scheduling method, apparatus, computer equipment, storage medium, and computer program product acquire event messages for scheduled tasks; determine the granularity of the hierarchical time wheel based on the business data carried in the event messages, acquire a delay queue corresponding to the hierarchical time wheel granularity, and bind the delay queue to a dead-letter exchange; register the events corresponding to the event messages to the delay queue to obtain a valid delay queue; consume the events using a hierarchical time wheel strategy based on the valid delay queue and the bound dead-letter exchange; and write the events into a task execution queue to execute the scheduled task when the event triggering condition is met. Throughout this process, delayed message consumption can be achieved based on the delay queue and the dead-letter exchange, and the hierarchical time wheel strategy is used for consumption scheduling, effectively avoiding message congestion and achieving efficient scheduled task scheduling. Attached Figure Description

[0062] Figure 1 This is a diagram illustrating the application environment of a timed task scheduling method in one embodiment.

[0063] Figure 2 This is a flowchart illustrating a timed task scheduling method in one embodiment;

[0064] Figure 3 This is a flowchart illustrating the timed task scheduling method in yet another embodiment;

[0065] Figure 4 Schematic diagram of the layered time wheel system;

[0066] Figure 5 This is a flowchart illustrating a timed task scheduling method in another embodiment;

[0067] Figure 6 This is a flowchart illustrating the scheduled task method in an application example.

[0068] Figure 7 This is a structural block diagram of a timed task scheduling device in one embodiment;

[0069] Figure 8 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation

[0070] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0071] The scheduled task method provided in this application can be applied to, for example, Figure 1In the application environment shown, the user operates on the terminal side, setting a new scheduled task (e.g., a reminder to water plants every 3 days, 6 hours, and 30 minutes). The terminal responds to the user's setting by obtaining the event message for the scheduled task; it determines the granularity of the hierarchical time wheel based on the business data carried in the event message, obtains the delay queue corresponding to the granularity of the hierarchical time wheel, and binds the delay queue to a dead-letter exchange; it registers the event corresponding to the event message to the delay queue, obtaining a valid delay queue; based on the valid delay queue and the bound dead-letter exchange, it consumes events using a hierarchical time wheel strategy; when the event triggering condition is met, the event is written to the task execution queue to execute the scheduled task.

[0072] In one embodiment, such as Figure 2 As shown, a timed task scheduling method is provided, which can be applied to... Figure 1 Taking the terminal in the example, the explanation includes the following steps:

[0073] S100: Get the event message of the scheduled task.

[0074] A scheduled task is a task triggered when certain time conditions are met, such as a scheduled alarm, a scheduled reminder, or a scheduled data switch. The event message for a scheduled task is the event message corresponding to the scheduled task. This event message carries the business data of the scheduled task, including the specific content of the event, the task execution cycle, and the number of times the task is executed. For example, a scheduled task that sets an alarm every 24 hours for 5 consecutive days would have the following business data: event – ​​alarm; execution cycle – 24-hour interval; number of executions – 5 times.

[0075] S200: Determine the granularity of the hierarchical time wheel based on the business data carried in the event message, obtain the delay queue corresponding to the granularity of the hierarchical time wheel, and bind the delay queue to the dead letter exchange.

[0076] As mentioned above, business data includes information representing task trigger times. By analyzing the time granularity of this time, the granularity of the hierarchical time wheel can be determined. Common time granularities include seconds, minutes, hours, days, months, and years, with corresponding time wheels of seconds, minutes, hours, days, months, and years, respectively. Figure 3As shown. Delay queues corresponding to the hierarchical time granularity are obtained, and these delay queues are bound to the dead-letter exchange. Specifically, based on business data, delay queues with different time granularities are created, bound to the dead-letter exchange (event.all), and routed to the dead-letter queue by the event exchange. For example, delay queues with three granularities—daily, hourly, and minute—can be created. The execution error of scheduled tasks can be controlled within the unit time of the highest granularity, the minute wheel, i.e., 1 minute. Theoretically, the execution cycle of scheduled tasks can be supported in days, with no numerical limit. For performance considerations, if the execution cycle of a scheduled task exceeds the maximum scale of a time wheel, such as the execution time of a scheduled task being February 3 days 4 hours later, then an additional delay queue with a monthly granularity needs to be created. Here, the scheduling time wheel granularity of the scheduled tasks is optional. Different time wheel granularity delay queues can be selected from a business perspective to customize the time error of scheduled task execution and obtain better system performance.

[0077] S300: Register the event corresponding to the event message to the delay queue to obtain the effective delay queue.

[0078] The event message is parsed to generate a specific trigger expression. This specific trigger expression represents the event corresponding to the time message, such as an alarm sounding every 3 hours. This specific trigger expression is then registered to a delay queue, resulting in a valid delay queue.

[0079] In practical applications, the process involves first acquiring the scheduled task, encapsulating the corresponding business data into an event message, parsing the event message to generate a specific trigger expression, and registering this specific trigger expression to a delay queue with the corresponding time granularity, thus obtaining an effective delay queue. From an underlying implementation perspective, the distributed scheduled task service receives and parses the event message from the scheduled task, updates the recvTimestamp (record timestamp) to the current time, generates a timed trigger expression, determines the granularity of the delay queue corresponding to the business data carried in the scheduled task, and registers it to the corresponding delay queue.

[0080] S400: Based on an effective delay queue and a bound dead-letter exchange, it uses a hierarchical time wheel strategy to consume events.

[0081] The effective delay queue and its associated dead-letter exchange employ a hierarchical time wheel strategy to consume events. Specifically, it can monitor the cyclic consumption of events in the effective delay queues at all time granularities at the dead-letter exchange and use a hierarchical time wheel strategy to cascade the cyclic consumption. Cascading scheduling refers to the cascading scheduling between layered time wheels. After the current layer's time wheel consumption is completed, the next layer's time consumption begins. For example, for a scheduled task with a time of 3 days, 4 hours, and 5 minutes, the event is first registered in the daily wheel delay queue and consumed at the dead-letter exchange. When the daily wheel cycles once, the event expires once in the daily wheel delay queue and enters the dead-letter exchange once. After accumulating 3 expirations, the event is registered in the hour wheel delay queue and consumed at the dead-letter exchange. When the hour wheel cycles once, the event expires once in the hour wheel delay queue and enters the dead-letter exchange once. After accumulating 4 expirations, the event is registered in the sub-wheel delay queue and consumed at the dead-letter exchange. When the sub-wheel cycles once, the event expires once in the sub-wheel delay queue and enters the dead-letter exchange once. After accumulating 5 expirations, the event trigger condition is met, and the scheduled task needs to be executed immediately.

[0082] S500: When the event triggering conditions are met, the event is written to the task execution queue to execute the scheduled task.

[0083] An event trigger condition refers to the arrival of a specific time point corresponding to a scheduled task. For example, for a 3-hour alarm, the event trigger condition is that 3 hours have elapsed. When the event trigger condition is met, the event is written to the task execution queue to execute the scheduled task. Furthermore, such as... Figure 3 As shown, when the event triggering condition is not met, return to S400 to continue consuming events using the hierarchical time wheel strategy until the event triggering condition is met, and then enter S500.

[0084] The above-described scheduled task scheduling method involves: obtaining the event message of the scheduled task; determining the granularity of the hierarchical time wheel based on the business data carried in the event message; obtaining the delay queue corresponding to the granularity of the hierarchical time wheel; binding the delay queue to a dead-letter exchange; registering the event corresponding to the event message to the delay queue to obtain the effective delay queue; consuming events using a hierarchical time wheel strategy based on the effective delay queue and the bound dead-letter exchange; and writing the event to the task execution queue when the event triggering condition is met to execute the scheduled task. Throughout this process, delayed message consumption can be achieved based on the delay queue and the dead-letter exchange, and the hierarchical time wheel strategy is used for consumption scheduling, effectively avoiding message congestion and achieving efficient scheduled task scheduling.

[0085] In one embodiment, the event consumption strategy based on an effective delay queue and a bound dead-letter exchange includes:

[0086] Monitor the cyclic consumption of events in the valid delay queues at all time granularities at the dead letter exchange, and use a hierarchical time wheel strategy to cascade the cyclic consumption.

[0087] As mentioned above, effective delay queues correspond to different time granularities, and different time granularities correspond to different time wheel levels. Therefore, a hierarchical time wheel strategy can be used to cascade the effective delay queues of different time granularities (different time wheel levels). Cascade scheduling specifically refers to scheduling the next time granularity's effective delay queue for cyclic consumption after completing the cyclic consumption of events in the current time granularity's effective delay queue. For example, for a scheduled task of 3 days, 4 hours, and 2 minutes, it includes time granularities of days, hours, and minutes, with the corresponding time wheels being the day wheel, hour wheel, and minute wheel, respectively. Starting with the day wheel delay queue, if an event in the day wheel delay queue is cyclically consumed 3 times at the dead-letter exchange, the event is registered to the hour wheel delay queue. If an event in the hour wheel delay queue is cyclically consumed 4 times at the dead-letter exchange, the event is registered to the minute wheel delay queue. If an event in the minute wheel delay queue is cyclically consumed 2 times at the dead-letter exchange, the event trigger condition is met.

[0088] In one embodiment, monitoring the cyclic consumption of events in the valid delay queues at all time granularities at the dead-letter exchange, and employing a hierarchical time wheel strategy to cascade the cyclic consumption includes:

[0089] Bind all valid delay queues at all time granularities to the same dead-letter exchange; circulate events in a single valid delay queue at the same dead-letter exchange, and use a hierarchical time wheel strategy to cascade the circular consumption.

[0090] Effective delay queues with different time granularities are bound to the same dead-letter exchange for unified scheduling and management. Binding to the same dead-letter exchange can be understood as events in effective delay queues with different time granularities being consumed at the same dead-letter exchange.

[0091] In one embodiment, the cascading scheduling of events in a single valid delay queue at the same dead-letter exchange, using a hierarchical time-wheel strategy, includes:

[0092] The system identifies the highest-granularity effective delay queue to obtain the target effective delay queue. When the target effective delay queue circulates once at the corresponding time granularity, the event is consumed once by the same dead-letter exchange. When the number of loop consumptions reaches the value of the current time granularity corresponding to the scheduled task, the system selects the next time granularity effective delay queue as the target effective delay queue and returns to the step where the event is consumed once by the same dead-letter exchange when the target effective delay queue circulates once at the corresponding time granularity. This process continues until all effective delay queues of all time granularities have been looped through.

[0093] All delay queues of all granularities are bound to the same dead-letter exchange. Individual delay queues are consumed cyclically at the dead-letter exchange, or they are moved to finer-grained delay queues. For example... Figure 4 As shown, parsing the Event message of Task1 reveals that the scheduled task will execute in 3 days and 2 hours, thus determining the granularity of the target delay queue as the time wheel. The Event is registered to the time wheel's delay queue, and its TTL (Time To Live) is set to 1 day. As the time wheel cycles through the time column once per day, the Event expires once in the delay queue and enters the dead-letter queue. The recvTimestamp and system time determine the number of times the Event will be reordered in this delay queue. After the Event expires three times in this delay queue, the scheduled task dispatch mechanism sends the Event to the time wheel's delay queue, repeating the above process until the time wheel reaches the execution time of Task1.

[0094] like Figure 5 As shown, in one embodiment, S100 includes:

[0095] S120: Get the scheduled task;

[0096] S140: Encapsulate the business data of the scheduled task into an event message.

[0097] The terminal responds to user actions by creating a scheduled task and encapsulates the business data within the task into an event message for further data processing. Furthermore, when creating a scheduled task, the execution cycle and number of executions need to be specified. In practical applications, the terminal responds to user actions, dynamically creates a scheduled task at a specified node in the business process, determines the task's execution cycle and number of executions, and binds a task callback function.

[0098] like Figure 5 As shown, in one embodiment, S200 includes:

[0099] S220: Identify task execution cycles based on business data;

[0100] S240: Identify the minimum and maximum time units in the task execution cycle;

[0101] S260: Determine the minimum granularity of the stratified time wheel based on the minimum time unit, and determine the maximum granularity of the stratified time wheel based on the maximum time unit;

[0102] S280: Get the delay queue corresponding to the granularity of the hierarchical time wheel.

[0103] As mentioned above, business data can include task execution cycle data, i.e., how often a task needs to be executed. The minimum and maximum time units within the execution cycle are identified. For example, a task with 3 days, 2 hours, and 4 minutes has a minimum time unit of minutes and a maximum time unit of days; similarly, a task with 3 months, 4 days, 5 hours, and 6 minutes has a minimum time unit of seconds and a maximum time unit of months. Correspondingly, the lowest granularity of the layered time wheel is determined by the minimum time unit, and the highest granularity by the maximum time unit. For example, the 3-day, 2-hour, and 4-minute scheduled task has a layered time wheel including a day wheel, an hour wheel, and a minute wheel; while the 3-month, 4-day, 5-hour, and 6-minute scheduled task has a layered time wheel including a month wheel, a day wheel, an hour wheel, a minute wheel, and a second wheel.

[0104] In one embodiment, when the event triggering condition is met, writing the event into the task execution queue to execute the scheduled task includes:

[0105] When the event triggering condition is met, the event is written to the task execution queue to obtain a valid task execution queue; the preset task callback function is called to execute the valid task execution queue, and the preset task callback function corresponds to the scheduled task.

[0106] The task callback function corresponds to the scheduled task. A corresponding task callback function is generated when a scheduled task is acquired / created. When the scheduled task event meets the triggering condition, it enters the task execution queue. By listening to the events arriving in this task execution queue, the business type of the event is distinguished, and the corresponding task callback function is triggered to complete one execution of the scheduled task.

[0107] Furthermore, after completing a scheduled task, it can also determine whether the events in the task execution queue need to be executed periodically. If it is not a one-time task, it will reset recvTimestamp to the current time and return to the step of registering the event corresponding to the event message to the delay queue.

[0108] To further illustrate the technical solution and effects of the scheduled task method of this application, specific application examples will be used below. Figure 6 Expand the explanation. For example... Figure 6 As shown, the scheduled task method of this application includes the following steps:

[0109] 1. Create a scheduled task, specify the task execution cycle and number of executions, and encapsulate the business data of the scheduled task into an Event message;

[0110] 2. Set up a hierarchical time wheel delay queue;

[0111] 3. Parse the Event message, specify the time granularity, and register the Event to the corresponding granularity's delay queue;

[0112] 4. The delayed queue and its bound dead-letter exchange consume Events according to the hierarchical time wheel strategy;

[0113] 5. Check if the Event triggering condition is met. If not, return to step 4; if met, proceed to step 6.

[0114] 6. Events are added to the scheduled task execution queue. The system listens for scheduled task execution, distinguishes the event type, and executes the task accordingly.

[0115] 7. Determine if the Event needs to be executed repeatedly. If yes, update the Event, reset recvTimestamp, and return to step 3; otherwise, end.

[0116] Overall, the scheduled task scheduling method in this application, based on the characteristics of dead-letter exchanges, further optimizes the business nodes for scheduled task creation, the loop strategy of the delay queue, and the custom consumption method of scheduled tasks. It adopts a layered time wheel delay queue for self-loop consumption and cascading collaborative consumption, so as to avoid wasting space and time. At the same time, it realizes that the time granularity of scheduled tasks is controllable and adjustable. In addition, the scheduled task execution record can be consumed by the user by listening to the scheduled task queue.

[0117] It should be understood that although the steps in the above flowcharts are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the above flowcharts may include multiple steps or stages, which are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps.

[0118] In one embodiment, such as Figure 7 As shown, a timed task scheduling device is provided, comprising:

[0119] The task acquisition module 100 is used to acquire event messages of scheduled tasks;

[0120] The queue acquisition module 200 is used to determine the granularity of the hierarchical time wheel based on the business data carried by the event message, acquire the delayed queue corresponding to the granularity of the hierarchical time wheel, and bind the delayed queue to the dead letter exchange.

[0121] The registration module 300 is used to register the event corresponding to the event message to the delay queue to obtain the effective delay queue.

[0122] The circular consumption module 400 is used to consume events based on an effective delay queue and a bound dead-letter exchange, using a hierarchical time wheel strategy.

[0123] The execution module 500 is used to write the event into the task execution queue to execute the scheduled task when the event triggering conditions are met.

[0124] The aforementioned scheduled task scheduling device acquires event messages for scheduled tasks; determines the granularity of the hierarchical time wheel based on the business data carried in the event messages, obtains the delay queue corresponding to the hierarchical time wheel granularity, and binds the delay queue to a dead-letter exchange; registers the events corresponding to the event messages to the delay queues, obtaining a valid delay queue; consumes events using a hierarchical time wheel strategy based on the valid delay queues and the bound dead-letter exchange; and writes the events to the task execution queue when the event triggering conditions are met to execute the scheduled task. Throughout this process, delayed message consumption is achieved based on the delay queues and dead-letter exchange, and the hierarchical time wheel strategy is used for consumption scheduling, effectively avoiding message congestion and achieving efficient scheduled task scheduling.

[0125] In one embodiment, the circular consumption module 400 is also used to monitor the circular consumption of events in the valid delay queues of all time granularities at the dead letter exchange, and to perform cascading scheduling of circular consumption using a hierarchical time wheel strategy.

[0126] In one embodiment, the circular consumption module 400 is further configured to bind all valid delay queues of all time granularities to the same dead-letter exchange; to circulate the events in a single valid delay queue at the same dead-letter exchange, and to cascade the circular consumption using a hierarchical time wheel strategy.

[0127] In one embodiment, the cyclic consumption module 400 is further configured to determine the effective delay queue with the highest time granularity to obtain the target effective delay queue; when the target effective delay queue circulates once with the corresponding time granularity, the event enters the same dead-letter exchange for consumption once; when the number of cyclic consumptions reaches the value of the current time granularity corresponding to the timed task, the effective delay queue with the next time granularity is selected as the target effective delay queue, and the step of entering the same dead-letter exchange for consumption once with the corresponding time granularity when the target effective delay queue circulates once is returned, until all effective delay queues with all time granularities are cyclically consumed.

[0128] In one embodiment, the task acquisition module 100 is further configured to acquire a scheduled task and encapsulate the business data of the scheduled task into an event message.

[0129] In one embodiment, the queue acquisition module 200 is further configured to identify the task execution cycle based on business data; identify the minimum time unit and the maximum time unit in the task execution cycle; determine the lowest granularity of the hierarchical time wheel based on the minimum time unit, and determine the highest granularity of the hierarchical time wheel based on the maximum time unit.

[0130] In one embodiment, the execution module 500 is further configured to write the event into the task execution queue when the event triggering condition is met, thereby obtaining a valid task execution queue; and to call a preset task callback function to execute the valid task execution queue, wherein the preset task callback function corresponds to the timed task.

[0131] For specific embodiments of the timed task scheduling device, please refer to the embodiments of the timed task scheduling method described above, which will not be repeated here. Each module in the above-described timed task scheduling device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device in hardware form, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.

[0132] In one embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 8As shown, the computer device includes a processor, memory, communication interface, display screen, and input devices connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, carrier networks, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it implements a timed task scheduling method. The display screen can be an LCD screen or an e-ink screen. The input devices can be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the computer device casing, or an external keyboard, touchpad, or mouse.

[0133] Those skilled in the art will understand that Figure 8 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0134] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:

[0135] Retrieve event messages from scheduled tasks;

[0136] The granularity of the hierarchical time wheel is determined based on the business data carried in the event message, and the delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is then bound to the dead letter exchange.

[0137] Register the event corresponding to the event message to the delay queue to obtain the effective delay queue;

[0138] Based on an effective delay queue and a bound dead-letter exchange, a hierarchical time wheel strategy is adopted to consume events;

[0139] When the event triggering conditions are met, the event is written to the task execution queue to execute the scheduled task.

[0140] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0141] Monitor the cyclic consumption of events in the valid delay queues at all time granularities at the dead letter exchange, and use a hierarchical time wheel strategy to cascade the cyclic consumption.

[0142] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0143] Bind all valid delay queues at all time granularities to the same dead-letter exchange; circulate events in a single valid delay queue at the same dead-letter exchange, and use a hierarchical time wheel strategy to cascade the circular consumption.

[0144] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0145] The system identifies the highest-granularity effective delay queue to obtain the target effective delay queue. When the target effective delay queue circulates once at the corresponding time granularity, the event is consumed once by the same dead-letter exchange. When the number of loop consumptions reaches the value of the current time granularity corresponding to the scheduled task, the system selects the next time granularity effective delay queue as the target effective delay queue and returns to the step where the event is consumed once by the same dead-letter exchange when the target effective delay queue circulates once at the corresponding time granularity. This process continues until all effective delay queues of all time granularities have been looped through.

[0146] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0147] Retrieve scheduled tasks; encapsulate the business data of scheduled tasks into event messages.

[0148] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0149] Identify task execution cycles based on business data; identify the minimum and maximum time units within the task execution cycle; determine the lowest granularity of the layered time wheel based on the minimum time unit, and determine the highest granularity of the layered time wheel based on the maximum time unit.

[0150] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0151] When the event triggering condition is met, the event is written to the task execution queue to obtain a valid task execution queue; the preset task callback function is called to execute the valid task execution queue, and the preset task callback function corresponds to the scheduled task.

[0152] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, the computer program performing the following steps when executed by a processor:

[0153] Retrieve event messages from scheduled tasks;

[0154] The granularity of the hierarchical time wheel is determined based on the business data carried in the event message, and the delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is then bound to the dead letter exchange.

[0155] Register the event corresponding to the event message to the delay queue to obtain the effective delay queue;

[0156] Based on an effective delay queue and a bound dead-letter exchange, a hierarchical time wheel strategy is adopted to consume events;

[0157] When the event triggering conditions are met, the event is written to the task execution queue to execute the scheduled task.

[0158] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0159] Monitor the cyclic consumption of events in the valid delay queues at all time granularities at the dead letter exchange, and use a hierarchical time wheel strategy to cascade the cyclic consumption.

[0160] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0161] Bind all valid delay queues at all time granularities to the same dead-letter exchange; circulate events in a single valid delay queue at the same dead-letter exchange, and use a hierarchical time wheel strategy to cascade the circular consumption.

[0162] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0163] The system identifies the highest-granularity effective delay queue to obtain the target effective delay queue. When the target effective delay queue circulates once at the corresponding time granularity, the event is consumed once by the same dead-letter exchange. When the number of loop consumptions reaches the value of the current time granularity corresponding to the scheduled task, the system selects the next time granularity effective delay queue as the target effective delay queue and returns to the step where the event is consumed once by the same dead-letter exchange when the target effective delay queue circulates once at the corresponding time granularity. This process continues until all effective delay queues of all time granularities have been looped through.

[0164] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0165] Retrieve scheduled tasks; encapsulate the business data of scheduled tasks into event messages.

[0166] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0167] Identify task execution cycles based on business data; identify the minimum and maximum time units within the task execution cycle; determine the lowest granularity of the layered time wheel based on the minimum time unit, and determine the highest granularity of the layered time wheel based on the maximum time unit.

[0168] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0169] When the event triggering condition is met, the event is written to the task execution queue to obtain a valid task execution queue; the preset task callback function is called to execute the valid task execution queue, and the preset task callback function corresponds to the scheduled task.

[0170] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, performs the following steps:

[0171] Retrieve event messages from scheduled tasks;

[0172] The granularity of the hierarchical time wheel is determined based on the business data carried in the event message, and the delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is then bound to the dead letter exchange.

[0173] Register the event corresponding to the event message to the delay queue to obtain the effective delay queue;

[0174] Based on an effective delay queue and a bound dead-letter exchange, a hierarchical time wheel strategy is adopted to consume events;

[0175] When the event triggering conditions are met, the event is written to the task execution queue to execute the scheduled task.

[0176] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0177] Monitor the cyclic consumption of events in the valid delay queues at all time granularities at the dead letter exchange, and use a hierarchical time wheel strategy to cascade the cyclic consumption.

[0178] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0179] Bind all valid delay queues at all time granularities to the same dead-letter exchange; circulate events in a single valid delay queue at the same dead-letter exchange, and use a hierarchical time wheel strategy to cascade the circular consumption.

[0180] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0181] The system identifies the highest-granularity effective delay queue to obtain the target effective delay queue. When the target effective delay queue circulates once at the corresponding time granularity, the event is consumed once by the same dead-letter exchange. When the number of loop consumptions reaches the value of the current time granularity corresponding to the scheduled task, the system selects the next time granularity effective delay queue as the target effective delay queue and returns to the step where the event is consumed once by the same dead-letter exchange when the target effective delay queue circulates once at the corresponding time granularity. This process continues until all effective delay queues of all time granularities have been looped through.

[0182] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0183] Retrieve scheduled tasks; encapsulate the business data of scheduled tasks into event messages.

[0184] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0185] Identify task execution cycles based on business data; identify the minimum and maximum time units within the task execution cycle; determine the lowest granularity of the layered time wheel based on the minimum time unit, and determine the highest granularity of the layered time wheel based on the maximum time unit.

[0186] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0187] When the event triggering condition is met, the event is written to the task execution queue to obtain a valid task execution queue; the preset task callback function is called to execute the valid task execution queue, and the preset task callback function corresponds to the scheduled task.

[0188] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, or optical storage, etc. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc.

[0189] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0190] The above embodiments merely illustrate several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.

Claims

1. A method for scheduling timed tasks, characterized in that, The method includes: Retrieve event messages from scheduled tasks; The granularity of the hierarchical time wheel is determined based on the service data carried in the event message, and a delay queue corresponding to the granularity of the hierarchical time wheel is obtained. The delay queue is bound to the dead letter exchange. Register the event corresponding to the event message to the delay queue to obtain the effective delay queue; Bind all time-granularity effective delay queues to the same dead-letter exchange; Determine the effective delay queue with the highest time granularity to obtain the target effective delay queue; When the target effective delay queue flows once at the corresponding time granularity, the event is consumed once by the same dead letter exchange; When the number of cyclic consumptions reaches the value of the current time granularity corresponding to the timed task, the effective delay queue of the next time granularity is selected as the target effective delay queue, and the process returns to the step where the event enters the same dead letter exchange once when the target effective delay queue flows once with the corresponding time granularity, until all effective delay queues of all time granularities are cycled through. When the event triggering condition is met, the event is written into the task execution queue to execute the scheduled task.

2. The method according to claim 1, characterized in that, The event message for obtaining the scheduled task includes: Get scheduled tasks; The business data of the scheduled task is encapsulated into an event message.

3. The method according to claim 1, characterized in that, Determining the granularity of the hierarchical time wheel based on the service data carried in the event message includes: Identify the task execution cycle based on the aforementioned business data; Identify the minimum and maximum time units in the task execution cycle; The minimum granularity of the layered time wheel is determined based on the minimum time unit, and the maximum granularity of the layered time wheel is determined based on the maximum time unit.

4. The method according to claim 1, characterized in that, The step of writing the event into the task execution queue to execute the scheduled task when the event triggering condition is met includes: When the event triggering condition is met, the event is written into the task execution queue to obtain a valid task execution queue; The preset task callback function is invoked to execute the valid task execution queue, and the preset task callback function corresponds to the timed task.

5. A timed task scheduling device, characterized in that, The device includes: The task acquisition module is used to acquire event messages from scheduled tasks. The queue acquisition module is used to determine the granularity of the hierarchical time wheel based on the service data carried by the event message, and acquire the delayed queue corresponding to the granularity of the hierarchical time wheel. The delayed queue is bound to the dead letter exchange. The registration module is used to bind all valid delay queues of all time granularities to the same dead-letter exchange, determine the valid delay queue with the highest time granularity, and obtain the target valid delay queue. When the target valid delay queue circulates once with the corresponding time granularity, the event enters the same dead-letter exchange for consumption once. When the number of loop consumptions reaches the value of the current time granularity corresponding to the scheduled task, the valid delay queue of the next time granularity is selected as the target valid delay queue, and the step of "when the target valid delay queue circulates once with the corresponding time granularity, the event enters the same dead-letter exchange for consumption once" is returned, until all valid delay queues of all time granularities are looped through. The circular consumption module is used to consume the events based on an effective delay queue and a bound dead-letter exchange, using a hierarchical time wheel strategy. The execution module is used to write the event into the task execution queue to execute the scheduled task when the event triggering condition is met.

6. The apparatus according to claim 5, characterized in that, The task acquisition module is also used to acquire scheduled tasks and encapsulate the business data of the scheduled tasks into event messages.

7. The apparatus according to claim 5, characterized in that, The queue acquisition module is also used to identify the task execution cycle based on the business data; identify the minimum time unit and the maximum time unit in the task execution cycle; determine the lowest granularity of the layered time wheel based on the minimum time unit, and determine the highest granularity of the layered time wheel based on the maximum time unit.

8. The apparatus according to claim 5, characterized in that, The execution module is further configured to write the event into the task execution queue when the event triggering condition is met, thereby obtaining a valid task execution queue; and to call a preset task callback function to execute the valid task execution queue, wherein the preset task callback function corresponds to the timed task.

9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 4.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 4.

11. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 4.