Time-delay message delivery method and device, electronic equipment and storage medium
By generating a consumer queue with a preset format and a time wheel mechanism, the problem that RocketMQ cannot deliver messages with arbitrary delay time is solved. The message delivery function with arbitrary delay time is realized, and the stability and rapid recovery capability of the system are ensured.
Patent Information
- Application Number
- CN202410888238.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-04
- Publication Date
- 2025-10-14
- Estimated Expiration
- 2044-07-04
AI Technical Summary
In the existing technology, the message queue middleware RocketMQ can only use the existing 18 time-level delayed consumption queues, which cannot achieve message delivery with arbitrary delay time and cannot meet the needs of a wide range of business scenarios.
By generating a consumption queue with a preset format, combining it with the time wheel mechanism, and using the time wheel executor, data loader and scale pointer, message delivery with arbitrary delay time can be achieved.
The message delivery function with arbitrary delay time is realized, which avoids the cross-time wheel error when the time wheel executor is started, ensures the rapid recovery of delayed message delivery after the server crashes and restarts, and adjusts the time wheel scale pointer scale by the error time, thereby improving the accuracy of the time wheel rotation.
Smart Images

Figure CN118819896B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of communication, and in particular to a delay message delivery method and device, electronic equipment and storage medium. BACKGROUND
[0002] Delay messages have a wide range of application scenarios in Internet systems. Delay messages refer to messages produced by a message producer and sent to a message queue, but not expected to be delivered immediately, but rather delayed for a certain period of time before being delivered to a message consumer for consumption. In the prior art, when using RocketMQ, a message queue middleware, for delay message delivery, only the existing 18 time level delay consumption queues can be used, and the arbitrary delay time message delivery function cannot be implemented, which cannot cope with a wide range of business scenarios. SUMMARY
[0003] In view of the above, it is necessary to provide a delay message delivery method for implementing an arbitrary delay time message delivery function.
[0004] To achieve the above object, the present application provides a delay message delivery method, which comprises:
[0005] receiving a delay message sent by a message producer, and storing the delay message and a delivery time corresponding to the delay message through a middleware server;
[0006] generating a consumption queue in a preset format according to the delivery time, each consumption queue corresponding to a time period, setting a corresponding time wheel data loader for each time period consumption queue in a preset time wheel, and storing the delay message in the consumption queue corresponding to the delivery time, the time wheel comprising a time wheel executor, a time wheel data loader and a time wheel scale pointer;
[0007] when detecting that the time wheel executor is started, acquiring a first time wheel data loader corresponding to a first time period and a second time wheel data loader corresponding to a second time period, and loading the first time wheel data loader and the second time wheel data loader into the time wheel executor;
[0008] starting the first time wheel data loader, generating a consumption task according to the delay message in the consumption queue corresponding to the first time wheel data loader, and loading the consumption task into a preset time wheel scale;
[0009] The time wheel scale pointer extracts the consumer task from the time wheel scale and delivers the corresponding delay message of the consumer task to the message consumer, and a second time wheel data loader is started to perform delay message delivery in a second time period after the end of the first time period, and the time wheel data loader corresponding to the next time period is loaded into the time wheel executor in the order of time period at the beginning of each time period to be started after the end of the time period.
[0010] Optionally, the delay message includes message content and delay time, and the delay message and the delivery time corresponding to the delay message are stored by the middleware server, including:
[0011] The delivery time corresponding to the delay message is calculated according to the delay time, and the delay message and the corresponding delivery time are stored in the message database of the middleware server.
[0012] Optionally, the consumer queue in a preset format is generated according to the delivery time, including:
[0013] The delivery time of each delay message stored by the middleware server is converted into a preset time format, and the consumer queue is generated according to the time period corresponding to each preset time format and named with the preset time format.
[0014] Optionally, the consumer task is generated according to the delay message in the consumer queue corresponding to the first time wheel data loader, including:
[0015] The message size of the delay message is calculated, and the message offset is calculated according to the delivery time corresponding to the delay message.
[0016] The delay message and the corresponding message offset and message size are packaged into a consumer task.
[0017] Optionally, the consumer task is loaded into a preset time wheel scale, including:
[0018] The message offset in each consumer task is obtained, the time wheel scale corresponding to each consumer task in the time wheel data loader is calculated according to the message offset, and each consumer task is loaded into the corresponding time wheel scale.
[0019] Optionally, the time wheel scale pointer extracts the consumer task from the time wheel scale and delivers the corresponding delay message of the consumer task to the message consumer, including:
[0020] When the time wheel in which the time wheel scale is located is listened to, the consumer task in the time wheel scale corresponding to the time wheel scale pointer is extracted.
[0021] deliver the delay message corresponding to the consumption task to the message consumer corresponding to the delay message at the corresponding delivery time.
[0022] Optionally, after delivering the delay message in the consumption task to the message consumer corresponding to the delay message at the corresponding delivery time, the method further comprises:
[0023] calculating a sum of a time of taking the consumption task for consumption and a time of delivering the delay message as an error time;
[0024] adjusting a time wheel scale pointer of time wheel timing rotation according to the error time.
[0025] In addition, to achieve the above object, the application further provides a delay message delivery device, comprising:
[0026] a message receiving module, configured to receive a delay message sent by a message producer, and store the delay message and a delivery time corresponding to the delay message through a middleware server;
[0027] a message storage module, configured to generate a consumption queue in a preset format according to the delivery time, each consumption queue corresponding to a time period, and setting a corresponding time wheel data loader for the consumption queue of each time period in a preset time wheel, and storing the delay message into the consumption queue corresponding to the delivery time, wherein the time wheel comprises a time wheel executor, a time wheel data loader and a time wheel scale pointer;
[0028] a time wheel configuration module, configured to, when detecting that the time wheel executor is started, acquire a first time wheel data loader corresponding to a first time period and a second time wheel data loader corresponding to a second time period, and load the first time wheel data loader and the second time wheel data loader into the time wheel executor;
[0029] a message loading module, configured to start the first time wheel data loader, generate a consumption task according to the delay message in the consumption queue corresponding to the first time wheel data loader, and load the consumption task into a preset time wheel scale;
[0030] a message delivery module, configured to take the consumption task from the time wheel scale through the time wheel scale pointer and deliver the delay message corresponding to the consumption task to a message consumer, start the second time wheel data loader to perform delay message delivery of a second time period after the first time period ends, and load the time wheel data loader corresponding to a next time period into the time wheel executor in a time period order at the start of each time period to be started after the time period ends.
[0031] In addition, to achieve the above object, the application further provides an electronic device, comprising:
[0032] at least one processor; and
[0033] a memory connected with the at least one processor in communication; wherein
[0034] the memory stores a computer program executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the above-mentioned time-delay message delivery method.
[0035] In addition, to achieve the above-mentioned purpose, the application also provides a computer readable storage medium, the computer readable storage medium stores at least one computer program, and the at least one computer program is executed by a processor in an electronic device to realize the above-mentioned time-delay message delivery method.
[0036] The application realizes the message delivery function of any time-delay time by generating the time-delay message delivery time corresponding to the consumption queue and combining the consumption offset; avoids the error problem caused by the cross time wheel when the time wheel executor starts by loading the time wheel data loader of the first time period and the second time period into the time wheel executor; realizes the quick recovery of the time-delay delivery message after the server restarts by recording the consumption progress; avoids the inaccuracy of the time wheel scale caused by the time-consuming of the time wheel rotation by calculating the error time of the consumption task and the delivery time-delay message when the time wheel rotates to adjust the scale of the time wheel scale pointer. BRIEF DESCRIPTION OF DRAWINGS
[0037] Figure 1 Flow chart of an embodiment of the time-delay message delivery method of the application;
[0038] Figure 2 Schematic diagram of an embodiment of the electronic device of the application;
[0039] Figure 3 Schematic diagram of an embodiment of the time-delay message delivery device of the application.
[0040] The implementation, functional features and advantages of the application will be further described with reference to the accompanying drawings and embodiments. DETAILED DESCRIPTION
[0041] In order to make the purpose, technical scheme and advantages of the application more clear, the application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the application and not to limit the application. Based on the embodiments in the application, all other embodiments obtained by those skilled in the art without creative labor fall within the scope of the application.
[0042] It should be noted that the description of "first", "second" and the like in the present application is only for the purpose of description, and cannot be understood as indicating or implying the relative importance of the technical features indicated, or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first", "second" can be explicitly or implicitly included at least one of the features. In addition, the technical solutions of various embodiments can be combined with each other, but it must be based on the realization of ordinary skilled in the art, when the combination of technical solutions appears contradictory or cannot be realized, it should be considered that the combination of technical solutions does not exist, nor within the protection scope required by the present application.
[0043] As shown in the flow chart of an embodiment of the present application, the method for delayed message delivery includes steps S1-S5. Figure 1
[0044] S1, receiving the delayed message sent by the message producer, storing the delayed message and the delivery time corresponding to the delayed message through the middleware server.
[0045] In an embodiment, the delayed message includes message content and delay time, and storing the delayed message and the delivery time corresponding to the delayed message through the middleware server includes:
[0046] According to the delay time, the delivery time corresponding to the delayed message is calculated, and the delayed message and the corresponding delivery time are stored in the message database of the middleware server.
[0047] In this embodiment, the delayed message delivery method is realized by Rocket MQ, and the Rocket MQ delivers the delayed message sent by the message producer Producer to the message consumer Consumer for consumption through the middleware Broker for storing and managing the delayed message. Rocket MQ is a distributed message middleware constructed based on the topic (Topic) publishing / subscription mode, supporting high concurrency and high reliability of message transmission.
[0048] Rocket MQ can temporarily store excessive messages in the middleware Broker, prevent the system from being overwhelmed by excessive message pressure during peak period, and ensure the stability and reliability of the system. After the message producer sends the message to the Rocket MQ center, it can return immediately without waiting for the message to be consumed, improving the system throughput and response speed. Rocket MQ supports distributed transaction messages, which can ensure the atomicity of messages in a distributed environment. Rocket MQ supports the broadcast mode of messages, which can send messages to all message consumers, suitable for scenarios that need to broadcast messages to multiple consumers, such as advertisement push, real-time notification, etc.
[0049] S2, generating a consumption queue of a preset format according to the delivery time, each consumption queue corresponding to a time period, setting a corresponding time wheel data loader for the consumption queue of each time period in a preset time wheel, and storing the delay message in the consumption queue corresponding to the delivery time, the time wheel comprising a time wheel executor, a time wheel data loader and a time wheel scale pointer.
[0050] In an embodiment, generating a consumption queue of a preset format according to the delivery time comprises:
[0051] Converting the delivery time of each delay message stored by the middleware server into a preset time format, generating a consumption queue according to the time period corresponding to each preset time format and naming the consumption queue in the preset time format.
[0052] For example, the delivery time of delay message 1 is XXXX year YY month ZZ day AA hour BB minute CC second, the delivery time of delay message 2 is XXXX year YY month ZZ day AA hour DD minute EE second, and the delivery time of delay message 3 is XXXX year YY month ZZ day LL hour MM minute NN second; according to the preset time format of “year-month-day-hour”, the consumption queue generated according to the delivery time of delay message 1 and delay message 2 is “XXXXYYZZAA” consumption queue, and the consumption queue generated according to the delivery time of delay message 3 is “XXXXYYZZLL” consumption queue.
[0053] In an embodiment, storing the delay message in the consumption queue corresponding to the delivery time comprises:
[0054] Storing each delay message in the message database of the middleware server in the consumption queue corresponding to the delivery time.
[0055] In this embodiment, Rocket MQ, after storing the delay message sent by the message producer Producer through the middleware Broker, generates a consumption queue according to a preset time period when delivering the delay message, puts the delay message into the consumption queue of the time period where the delivery time is located according to the delivery time, and sets a corresponding time wheel data loader for each consumption queue in the time wheel, so as to realize delivering the delay message in each consumption queue in the time period corresponding to the consumption queue according to the delivery time of the delay message through the time wheel.
[0056] The native message queue of Rocket MQ stores the delay messages and delivers them at a uniform time, which cannot realize the message delivery of delay messages according to the preset delivery time of each delay message at an arbitrary delay time, while the time wheel can deliver messages according to the preset delivery time through the time wheel data loader. The application realizes the message delivery function of an arbitrary delay time in Rocket MQ by replacing the native message queue of Rocket MQ with the consumption queue corresponding to the time period and combining the time wheel message delivery mechanism.
[0057] S3, when detecting that the time wheel executor is started, acquiring a first time wheel data loader corresponding to a first time period and a second time wheel data loader corresponding to a second time period, and loading the first time wheel data loader and the second time wheel data loader into the time wheel executor.
[0058] In an embodiment, the time wheel executor is configured to start the time wheel to rotate at a preset frequency, and the time wheel data loader is configured to load a consumption task corresponding to a delay message in the consumption queue into a time wheel scale for the time wheel to extract the consumption task in the time wheel scale corresponding to the time wheel scale pointer when the time wheel rotates.
[0059] In an embodiment, each time period can be one hour, the first time wheel data loader corresponding to the first time period is a first time wheel data loader corresponding to the current hour, and the second time wheel data loader corresponding to the second time period is a second time wheel data loader corresponding to the next hour. The first time wheel data loader and the second time wheel data loader are loaded into the time wheel executor in sequence, so that the time wheel executor starts the first time wheel data loader and the second time wheel data loader in sequence.
[0060] S4, starting the first time wheel data loader, generating a consumption task according to a delay message in the consumption queue corresponding to the first time wheel data loader, and loading the consumption task into a preset time wheel scale.
[0061] In an embodiment, generating a consumption task according to a delay message in the consumption queue corresponding to the first time wheel data loader includes:
[0062] calculating the message size of the delay message, and calculating the message offset according to the delivery time corresponding to the delay message;
[0063] wrapping the delay message and the corresponding message offset and message size into a consumption task.
[0064] In an embodiment, the consumer queue is named in a time format of "year-month-day-hour", and stores the delayed messages that need to be delivered in an hour corresponding to the consumer queue name. According to the values of minutes and seconds of the delivery time corresponding to each delayed message in the consumer queue, the start time of the hour corresponding to the consumer queue name is taken as a reference, and the interval of seconds between the delivery of each delayed message is calculated, and the calculated interval time is taken as the message offset corresponding to the delayed message.
[0065] In an embodiment, loading the consumer task into the preset time wheel scale comprises:
[0066] Obtaining the message offset in each consumer task, calculating the time wheel scale corresponding to each consumer task in the time wheel data loader according to the message offset, and loading each consumer task into the corresponding time wheel scale.
[0067] In the embodiment, the message offset is used to represent the difference between the delivery time of the consumer task and the start time of the time period, and the consumer task is loaded into the time wheel scale corresponding to the difference according to the message offset, so that when the time wheel scale pointer points to the time wheel scale, the consumer task loaded therein is extracted for delivery.
[0068] S5, timing the consumer task from the time wheel scale by the time wheel scale pointer and delivering the delayed message corresponding to the consumer task to the message consumer, starting the second time wheel data loader to deliver the delayed messages in the second time period after the end of the first time period, and loading the time wheel data loader corresponding to the next time period into the time wheel executor in time period order at the beginning of each time period to be started after the end of the time period.
[0069] In an embodiment, timing the consumer task from the time wheel scale by the time wheel scale pointer and delivering the delayed message corresponding to the consumer task to the message consumer comprises:
[0070] When the rotation of the time wheel in which the time wheel scale is located is listened to, the consumer task in the time wheel scale corresponding to the time wheel scale pointer is extracted;
[0071] The delayed message corresponding to the consumer task is delivered to the message consumer corresponding to the delayed message at the corresponding delivery time.
[0072] In the embodiment, the time wheel rotates every second, and the corresponding time wheel scale pointer points to the time wheel scale corresponding to the current time. When the time wheel scale pointed to by the time wheel scale pointer has a consumer task, the consumer task is extracted for delivery of the corresponding delayed message.
[0073] In one embodiment, after delivering the delayed message in the consumption task to the message consumer corresponding to the delayed message at the corresponding delivery time, the method further includes:
[0074] The sum of the time it takes to extract the consumption task and the time it takes to deliver the delayed message is calculated as the error time.
[0075] The time wheel scale pointer that adjusts the time wheel to rotate regularly is adjusted according to the error time.
[0076] In one embodiment, after extracting the consumption task regularly through the time wheel for consumption, the method further includes:
[0077] Generate a recovery file by regularly recording the consumption progress of the time wheel through the delay queue and the time pointer container. The consumption progress includes the time wheel scale pointer scale and the consumption offset of the consumption task in the time wheel scale.
[0078] When it is detected that the server is down and recovering or restarting, the recovery file is obtained, and the corresponding time wheel is restarted according to the time wheel scale pointer scale and consumption offset in the recovery file to perform delayed message delivery.
[0079] In one embodiment, the method further includes periodically clearing delayed messages from a message database of the middleware server. Specifically, the last modification time and delay time of each delayed message in the message database are obtained, and the sum of the last modification time, the delay time, and the preset cache time is calculated. If the sum corresponding to the delayed message is less than the current time, the delayed message is determined to be outdated, and all outdated delayed messages are deleted.
[0080] It can be seen from the above embodiments that the delayed message delivery method proposed in the present invention realizes the message delivery function of arbitrary delay time by generating a consumption queue corresponding to the delayed message delivery time and combining the consumption offset; realizes the rapid recovery of delayed delivery message after the server crashes and restarts by recording the consumption progress; avoids the problem of errors caused by crossing the time wheel when the time wheel executor is started by loading the time wheel data loader of the first time period and the second time period into the time wheel executor; and adjusts the time wheel scale pointer scale by calculating the error time of extracting the consumption task and delivering the delayed message when the time wheel rotates, so as to avoid the time wheel scale being inaccurate due to the time wheel rotation being time-consuming.
[0081] like Figure 2 FIG2 is a schematic diagram of an embodiment of an electronic device according to the present invention. Electronic device 1 is a device capable of automatically performing numerical calculations and / or information processing according to pre-set or stored instructions. Electronic device 1 may be a computer, a server, or a server group consisting of multiple servers.
[0082] In the embodiment, the electronic device 1 includes, but is not limited to, a memory 11, a processor 12, and a network interface 13 which are communicatively connected through a system bus, and the memory 11 stores a delayed message delivery program 10 which can be executed by the processor 12. Figure 3 Only the electronic device 1 with the components 11-13 and the delayed message delivery program 10 is shown, and those skilled in the art can understand that, Figure 3 The structure shown does not constitute a limitation on the electronic device 1, and can include fewer or more components than shown, or combine certain components, or different component arrangements.
[0083] The memory 11 includes a memory and at least one type of readable storage medium. The memory provides a cache for the operation of the electronic device 1; the readable storage medium can be volatile or non-volatile. Specifically, the readable storage medium can be a storage medium such as a flash memory, a hard disk, a multimedia card, a card-type memory (e.g., an SD or DX memory, etc.), a random access memory (RAM), a static random access memory (SRAM), a read-only memory (ROM), an electrically erasable programmable read-only memory (EEPROM), a programmable read-only memory (PROM), a magnetic memory, a magnetic disk, an optical disk, etc. In some embodiments, the readable storage medium can be an internal storage unit of the electronic device 1, such as a hard disk of the electronic device 1; in other embodiments, the storage medium can also be an external storage device of the electronic device 1, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. provided on the electronic device 1. In the embodiment, the readable storage medium of the memory 11 mainly includes a program storage area and a data storage area, wherein the program storage area is usually used to store an operating system and various application software installed on the electronic device 1, such as the code of the delayed message delivery program 10 in an embodiment of the present application; the data storage area can store data created according to the use of the blockchain node, such as various data that has been output or will be output.
[0084] The processor 12 can be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other data processing chip in some embodiments. The processor 12 is usually used to control the overall operation of the electronic device 1, such as performing control and processing related to data interaction or communication with other devices, etc. In the embodiment, the processor 12 is used to run program codes or process data stored in the memory 11, such as running the delayed message delivery program 10, etc.
[0085] The network interface 13 can include a wireless network interface or a wired network interface, and is configured to establish a communication connection between the electronic device 1 and a server (not shown in the figure).
[0086] Optionally, the electronic device 1 can further include a user interface, which can include a display, an input unit such as a keyboard, and optionally a standard wired interface, a wireless interface. Optionally, in some embodiments, the display can be an LED display, a liquid crystal display, a touch liquid crystal display, an organic light-emitting diode (OLED) touch screen, etc. The display can also be appropriately referred to as a display screen or a display unit, and is configured to display information processed in the electronic device 1 and display a visualized user interface.
[0087] In an embodiment of the present application, the delay message delivery program 10 is executed by the processor 12 to implement the following steps S1-S5.
[0088] S1, receiving a delay message sent by a message producer, and storing the delay message and a delivery time corresponding to the delay message through a middleware server;
[0089] S2, generating a consumer queue in a preset format according to the delivery time, each consumer queue corresponding to a time period, setting a corresponding time wheel data loader for the consumer queue of each time period in a preset time wheel, and storing the delay message into the consumer queue corresponding to the delivery time, wherein the time wheel includes a time wheel executor, a time wheel data loader and a time wheel scale pointer;
[0090] S3, when detecting that the time wheel executor is started, acquiring a first time wheel data loader corresponding to a first time period and a second time wheel data loader corresponding to a second time period, and loading the first time wheel data loader and the second time wheel data loader into the time wheel executor;
[0091] S4, starting the first time wheel data loader, generating a consumer task according to the delay message in the consumer queue corresponding to the first time wheel data loader, and loading the consumer task into a preset time wheel scale;
[0092] S5, timing and canceling the consumption task from the time wheel scale by the time wheel scale pointer, and delivering the delay message corresponding to the consumption task to the message consumer, starting the second time wheel data loader to perform delay message delivery in the second time period after the first time period ends, and loading the time wheel data loader corresponding to the next time period into the time wheel executor in time period order at the beginning of each time period to be started after the time period ends.
[0093] The specific operation steps realized by the above steps S1-S5 are basically the same as those of the embodiment of the delay message delivery method of the application, and will not be described here again.
[0094] In other embodiments, the delay message delivery program 10 can also be divided into one or more modules, which are stored in the memory 11 and executed by one or more processors (the processor 12 in this embodiment) to complete the application. The module referred to in the application refers to a series of computer program instruction segments capable of completing a specific function, which is used to describe the execution process of the delay message delivery program 10 in the electronic device 1.
[0095] As shown in Figure 3 FIG. 1 is a schematic diagram of an embodiment of the delay message delivery device of the application.
[0096] In the first embodiment of the application, the delay message delivery device 100 includes a message receiving module 110, a message storage module 120, a time wheel configuration module 130, a message loading module 140, and a message delivery module 150, which are exemplarily described as follows.
[0097] The message receiving module 110 is configured to receive the delay message sent by the message producer, and store the delay message and the delivery time corresponding to the delay message through the middleware server.
[0098] The message storage module 120 is configured to generate a consumption queue in a preset format according to the delivery time, each consumption queue corresponding to a time period, and setting a corresponding time wheel data loader for the consumption queue of each time period in a preset time wheel, and storing the delay message in the consumption queue corresponding to the delivery time, wherein the time wheel includes a time wheel executor, a time wheel data loader, and a time wheel scale pointer.
[0099] The time wheel configuration module 130 is configured to, when detecting that the time wheel executor is started, acquire a first time wheel data loader corresponding to a first time period and a second time wheel data loader corresponding to a second time period, and load the first time wheel data loader and the second time wheel data loader into the time wheel executor.
[0100] The message loading module 140 is configured to start a first time wheel data loader, generate a consumption task according to a delay message in a consumption queue corresponding to the first time wheel data loader, and load the consumption task into a preset time wheel scale.
[0101] The message delivery module 150 is configured to periodically take out the consumption task from the time wheel scale through the time wheel scale pointer, and deliver the delay message corresponding to the consumption task to a message consumer. After the first time period ends, the second time wheel data loader is started to deliver the delay message in the second time period. At the beginning of each time period, the time wheel data loader corresponding to the next time period is loaded into the time wheel executor in the order of time period, so as to be started after the time period ends.
[0102] The specific operation steps realized when the message receiving module 110, the message storage module 120, the time wheel configuration module 130, the message loading module 140 and the message delivery module 150 are executed are generally the same as those of the above embodiment, and will not be described here.
[0103] In addition, the embodiment of the present application also provides a computer readable storage medium. The computer readable storage medium can be volatile or non-volatile. Specifically, the computer readable storage medium can be any one or any combination of a hard disk, a multimedia card, an SD card, a flash memory card, an SMC, a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a portable compact disc read-only memory (CD-ROM), a USB memory, etc. The delay message delivery program 10 is stored in the computer readable storage medium.
[0104] The delay message delivery program 10 is executed by the processor to realize the following operations:
[0105] A1, receiving a delay message sent by a message producer, and storing the delay message and a delivery time corresponding to the delay message through a middleware server;
[0106] A2, generating a consumption queue in a preset format according to the delivery time, each consumption queue corresponding to a time period, setting a corresponding time wheel data loader for the consumption queue of each time period in a preset time wheel, and storing the delay message in the consumption queue corresponding to the delivery time, the time wheel comprising a time wheel executor, a time wheel data loader and a time wheel scale pointer;
[0107] A3, when detecting that the time wheel executor is started, acquiring a first time wheel data loader corresponding to a first time period and a second time wheel data loader corresponding to a second time period, and loading the first time wheel data loader and the second time wheel data loader into the time wheel executor;
[0108] A4, start the first time wheel data loader, generate a consumption task according to the delay message in the consumption queue corresponding to the first time wheel data loader, and load the consumption task into a preset time wheel scale;
[0109] A5, timingly take out the consumption task from the time wheel scale through the time wheel scale pointer, and deliver the delay message corresponding to the consumption task to a message consumer, start the second time wheel data loader to perform delay message delivery in a second time period after the first time period ends, and load the time wheel data loader corresponding to the next time period into the time wheel executor in time period order at the beginning of each time period to be started after the time period ends.
[0110] The above-mentioned embodiment numbers of the present application are only for description, and do not represent the advantages and disadvantages of the embodiments.
[0111] It should be noted that in this paper, the term "including", "containing" or any other variant thereof is intended to cover non-exclusive containing, so that the process, device, article or method including a series of elements not only includes those elements, but also includes other elements not explicitly listed, or includes elements inherent to such process, device, article or method. Without more limitations, the element defined by the sentence "including a…" does not exclude the presence of other identical elements in the process, device, article or method including the element.
[0112] From the above description of the embodiments, those skilled in the art can clearly understand that the above-mentioned embodiment method can be realized by means of software and necessary general hardware platform, of course, it can also be realized by hardware, but in many cases the former is a better embodiment. Based on such understanding, the technical solutions of the present application can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes a plurality of instructions for making a terminal device (which can be a mobile phone, computer, server, air conditioner, or network device, etc.) execute the method described in each embodiment of the present application.
[0113] The above is only the preferred embodiment of the present application, and does not limit the patent scope of the present application, and any equivalent structure or equivalent flow transformation made by using the content of the specification and drawings of the present application, or direct or indirect application in other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A delayed message delivery method, characterized in that: The method comprises: Receive a delayed message sent by a message producer, and store the delayed message and the delivery time corresponding to the delayed message through a middleware server; Generate a consumption queue in a preset format according to the delivery time, each consumption queue corresponds to a time period, set a corresponding time wheel data loader for the consumption queue of each time period in the preset time wheel, store the delayed message in the consumption queue corresponding to the delivery time, the time wheel includes a time wheel executor, a time wheel data loader and a time wheel scale pointer; When it is detected that the timing wheel executor is started, obtaining a first timing wheel data loader corresponding to a first time period and a second timing wheel data loader corresponding to a second time period, and loading the first timing wheel data loader and the second timing wheel data loader into the timing wheel executor; Start the first time wheel data loader, generate a consumption task according to the delayed message in the consumption queue corresponding to the first time wheel data loader, and load the consumption task into the preset time wheel scale; The time wheel scale pointer is used to periodically extract consumption tasks from the time wheel scale and deliver the delayed message corresponding to the consumption task to the message consumer. After the first time period ends, the second time wheel data loader is started to deliver the delayed message of the second time period. At the beginning of each time period, the time wheel data loader corresponding to the next time period is loaded into the time wheel executor in the order of the time period to be started after the time period ends.
2. The delayed message delivery method according to claim 1, wherein: The delayed message includes message content and delay time, and storing the delayed message and the delivery time corresponding to the delayed message by the middleware server includes: The delivery time corresponding to the delayed message is calculated according to the delay time, and the delayed message and the corresponding delivery time are associated and stored in a message database of the middleware server.
3. The delayed message delivery method according to claim 1, wherein: Generating a consumption queue in a preset format according to the delivery time includes: The delivery time of each delayed message stored in the middleware server is converted into a preset time format, a consumption queue is generated according to a time period corresponding to each preset time format, and the consumption queue is named according to the preset time format.
4. The delayed message delivery method according to claim 1, wherein: Generating a consumption task based on the delayed message in the consumption queue corresponding to the first time round data loader includes: Calculating the message size of the delayed message and calculating the message offset according to the delivery time corresponding to the delayed message; The delayed message and the corresponding message offset and message size are packaged into a consumption task.
5. The delayed message delivery method according to claim 1, wherein: Loading the consumption task into the preset time wheel scale includes: The message offset in each consumption task is obtained, the time wheel scale corresponding to each consumption task in the time wheel data loader is calculated according to the message offset, and each consumption task is loaded into the corresponding time wheel scale.
6. The delayed message delivery method according to claim 1, wherein: Extracting consumption tasks from the time wheel scale regularly through the time wheel scale pointer and delivering delayed messages corresponding to the consumption tasks to the message consumer includes: When the rotation of the time wheel at the time wheel scale is detected, the consumption task in the time wheel scale corresponding to the time wheel scale pointer is extracted; The delayed message corresponding to the consumption task is delivered to the message consumer corresponding to the delayed message at the corresponding delivery time.
7. The delayed message delivery method according to claim 6, wherein: After delivering the delayed message in the consumption task to the message consumer corresponding to the delayed message at the corresponding delivery time, the method further includes: The sum of the time it takes to extract the consumption task and the time it takes to deliver the delayed message is calculated as the error time. The time wheel scale pointer that adjusts the time wheel to rotate regularly is adjusted according to the error time.
8. A delayed message delivery device, characterized in that: include: A message receiving module is used to receive the delayed message sent by the message producer and store the delayed message and the delivery time corresponding to the delayed message through the middleware server; A message storage module is configured to generate a consumption queue in a preset format according to the delivery time, wherein each consumption queue corresponds to a time period, and a corresponding time wheel data loader is set for the consumption queue of each time period in a preset time wheel, and the delayed message is stored in the consumption queue corresponding to the delivery time. The time wheel includes a time wheel executor, a time wheel data loader, and a time wheel scale pointer; a timing wheel configuration module, configured to, upon detecting that the timing wheel executor is started, obtain a first timing wheel data loader corresponding to a first time period and a second timing wheel data loader corresponding to a second time period, and load the first timing wheel data loader and the second timing wheel data loader into the timing wheel executor; A message loading module is used to start the first time wheel data loader, generate a consumption task according to the delayed message in the consumption queue corresponding to the first time wheel data loader, and load the consumption task into a preset time wheel scale; The message delivery module is used to periodically extract consumption tasks from the time wheel scale through the time wheel scale pointer and deliver the delayed message corresponding to the consumption task to the message consumer, start the second time wheel data loader after the end of the first time period to deliver the delayed message of the second time period, and load the time wheel data loader corresponding to the next time period into the time wheel executor in the order of the time period at the beginning of each time period to wait for it to be started after the time period ends.
9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the delayed message delivery method according to any one of claims 1 to 7.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the delayed message delivery method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Delay message delivery method and device, storage medium and computer equipment
CN112104519A
Delivery method and device of delay message, equipment and storage medium
CN115022263A