A delayed message delivery method, device, equipment and storage medium
Through the time wheel sharding mechanism and message offset identification method, the problems of delayed message delivery time and storage pressure in the prior art are solved, and flexible delayed message processing and efficient storage management are realized.
Patent Information
- Application Number
- CN202210622815.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-02
- Publication Date
- 2024-12-17
- Estimated Expiration
- 2042-06-02
AI Technical Summary
Existing delay message processing systems cannot cope with a wide range of business scenarios, and delay messages often lead to storage pressure and performance degradation.
The time round sharding mechanism is adopted, and the time is preset sharding processing is performed to support customizing the delivery time of delayed messages, and putting the offset of the message into the time round to realize the storage and delivery of delayed messages.
It breaks through the limitation of delayed message delivery time, supports more usage scenarios, reduces server memory overhead, controls the amount of message storage data, and avoids storage pressure and performance degradation.
Smart Images

Figure CN115022263B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a delayed message delivery method, device, equipment and storage medium. Background Art
[0002] Delayed messages have a wide range of application scenarios in Internet systems. Delayed messages refer to messages sent by message producers to message queues, but they are not expected to be delivered immediately. Instead, they are delivered to message consumers for consumption after a certain delay. For example: when teaching online live, all students are notified to prepare for class 5 minutes before the class starts; when a user has not paid for an order for more than 12 hours, a reminder is sent to the user; when broadcasting news, a preparation message is sent to the host at 17:30 every day to prepare, etc.
[0003] like Figure 1 The figure shows the main process and module diagram of the current message delay processing solution. Message producers produce two types of messages: ordinary messages and delayed messages. After the messages are produced, they need to be delivered so that they can be consumed by message consumers in a timely and accurate manner. Ordinary messages are messages with zero delay. For ordinary messages, when they are consumed, they will first be written sequentially to the commitlog message storage module. During asynchronous reput replay, the corresponding message queue index is generated through DispatchService, so that message consumers can consume the corresponding messages from the message queue. For delayed messages, when the message is written to the commitlog message storage module, the topic message title is set to SCHEDULE_TOPIC, the real title is saved in the context attribute and the delay time value is set; in asynchronous reput replay, a delayed consumption queue of SCHEDULE_TOPIC is generated through DispatchService, and there are only 18 delay levels, namely: 1 second, 5 seconds, 10 seconds, 30 seconds, 1 minute, 2 minutes, 3 minutes, 4 minutes, 5 minutes, 6 minutes, 7 minutes, 8 minutes, 9 minutes, 10 minutes, 20 minutes, 30 minutes, 1 hour, and 2 hours; set a timer for each delay level in ScheduleMessageService, read information from the delayed consumption queue, and if the element in the delayed consumption queue has expired, read the message content from the CommitLog message storage, restore the message content and write it to the CommitLog message storage, and then hand it over to the message consumer for consumption as a normal message.
[0004] However, as can be seen from the process of the original solution, although there are 18 levels of delay time, it is still unable to cope with a wide range of business scenarios, which limits the use of this message delay processing system; and all delayed messages are stored in the commitlog message storage module. If there are many delayed messages, the storage capacity of this module will be very large, resulting in storage pressure and performance degradation.
[0005] In summary, how to make the delivery time of delayed messages unlimited to support more usage scenarios, and how to control the amount of data stored in messages to reduce the overhead on server memory are issues that need to be resolved. Summary of the invention
[0006] In view of this, the purpose of the present invention is to provide a delayed message delivery method, device, equipment and storage medium, which can make the delayed message delivery time unlimited, support more usage scenarios, and control the amount of data stored in the message, reducing the overhead of the server memory. The specific scheme is as follows:
[0007] In a first aspect, the present application discloses a delayed message delivery method, comprising:
[0008] Obtaining a delayed message produced by a message producer, and determining a delivery time of the delayed message;
[0009] Compare the delivery time with the time of the time wheel to obtain a corresponding comparison result, and put the offset of the delayed message in the current message queue into the time wheel; wherein the time wheel is obtained by slicing the time according to a preset slicing length;
[0010] Based on the comparison result and using the offset currently obtained from the time wheel, the content of the delayed message is obtained from the message storage file, so as to deliver the delayed message to the message consumer.
[0011] Optionally, before obtaining the delayed message generated by the message producer, the method further includes:
[0012] Determine the type of message produced by the message producer;
[0013] If the message type is a common message that does not require delayed delivery, the common message is stored in the message storage file, and the common message in the current message queue is immediately delivered to the message consumer;
[0014] When the message type is the delayed message, the title of the delayed message is modified to the target title to mark the delayed message, and then the step of obtaining the delayed message produced by the message producer and determining the delivery time of the delayed message is triggered.
[0015] Optionally, comparing the delivery time with the time of the time wheel to obtain a corresponding comparison result includes:
[0016] When the delivery time is not greater than the current time of the time wheel, the target title is restored to the title of the common message, so as to convert the delayed message into a corresponding common message and deliver it to the message consumer.
[0017] Optionally, comparing the delivery time with the time of the time wheel to obtain a corresponding comparison result, and placing the offset of the delayed message in the current message queue into the time wheel, includes:
[0018] When the time slice where the delivery time is located is the same as the time slice where the time wheel is currently located, the offset of the delayed message in the current message queue is placed in the time wheel;
[0019] When the time slice where the delivery time is located is the same as the first time slice where the time wheel is located, and the time wheel in the first time slice is not empty, the offset of the delayed message in the current message queue is placed in the time wheel of the first time slice; wherein, the first time slice is the time slice after a preset number of time slices of the current time slice where the time wheel is located.
[0020] Optionally, determining the delivery time of the delayed message includes:
[0021] Obtaining the storage time of the delayed message and the delay time of the delayed message delivery;
[0022] The storage time and the delay time are summed and the absolute value is taken to determine the delivery time of the delayed message.
[0023] Optionally, after comparing the delivery time with the time of the time wheel to obtain a corresponding comparison result and placing the offset of the delayed message in the current message queue into the time wheel, the method further includes:
[0024] The storage position of the delayed message in the message storage file is determined based on the delivery time and the preset segment length, so that the delayed message is stored accordingly according to the storage position.
[0025] Optionally, obtaining the content of the delayed message from the message storage file so as to deliver the delayed message to a message consumer includes:
[0026] Obtain the content of the delayed message from the message storage file, and determine whether the time wheel has started to load the second time slice; wherein the second time slice is the next time slice of the current time slice of the time wheel;
[0027] If the time wheel has started to load the second time slice, determine whether the time wheel has reached the second time slice, if so, replace the time wheel, then update the time of the time wheel, and update the current consumption position of the message queue to deliver the next delayed message, if not, directly execute the steps of updating the time of the time wheel and updating the current consumption position of the message queue to deliver the next delayed message;
[0028] If the time wheel has not started loading the second time slice, determine whether the time of the time wheel is less than a preset threshold from the time of the second time slice. If so, asynchronously load the next message storage file to the time wheel on the second time slice, and start loading the second time slice. Then execute the step of updating the time of the time wheel and updating the current consumption point of the message queue to deliver the next delayed message. Otherwise, directly execute the step of updating the time of the time wheel and updating the consumption point of the current message queue to deliver the next delayed message.
[0029] In a second aspect, the present application discloses a delayed message delivery device, comprising:
[0030] A delivery time determination module, used to obtain the delayed message produced by the message producer and determine the delivery time of the delayed message;
[0031] An offset storage module is used to compare the delivery time with the time of the time wheel to obtain a corresponding comparison result, and put the offset of the delayed message in the current message queue into the time wheel; wherein the time wheel is obtained by slicing the time according to a preset slicing length;
[0032] The message delivery module is used to obtain the content of the delayed message from the message storage file based on the comparison result and using the offset obtained from the time wheel according to the current time, so as to deliver the delayed message to the message consumer.
[0033] In a third aspect, the present application discloses an electronic device, comprising a processor and a memory; wherein the memory is used to store a computer program, and the computer program is loaded and executed by the processor to implement the delayed message delivery method as described above.
[0034] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein the computer program, when executed by a processor, implements the delayed message delivery method as described above.
[0035] In this application, firstly, the delayed message produced by the message producer is obtained, and the delivery time of the delayed message is determined; then the delivery time is compared with the time of the time wheel to obtain the corresponding comparison result, and the offset of the delayed message in the current message queue is placed in the time wheel; wherein the time wheel is obtained by slicing the time according to the preset slicing length; finally, based on the comparison result and using the offset currently obtained from the time wheel, the content of the delayed message is obtained from the message storage file, so as to deliver the delayed message to the message consumer. It can be seen that the time slice obtained by slicing the time according to the preset slicing length is combined with the delivery of the delayed message, and the delayed message with customized delay is supported. There is no limit on the delayed delivery time of the delayed message, and it can be delivered and consumed after the delivery time is reached. Compared with the original scheme in which the delay time is limited to 18 levels and cannot be supported outside the 18 levels, this application breaks through the original limitation, can have a larger usage scenario, and has greater value. In addition, delayed messages are stored in time slices. Due to the existence of slices, the data volume of a single slice is not particularly large. If the data volume of a single slice is large, the slice time can be adjusted to a smaller size, so that the data volume of the slice is controlled at a reasonable level, which will not bring storage pressure or performance degradation. Furthermore, using the offset of the message to identify the delayed message and putting the offset in the time wheel can greatly reduce the overhead of the server memory. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0037] Figure 1 A schematic diagram of a current message delay processing solution disclosed in this application;
[0038] Figure 2 A flow chart of a delayed message delivery method disclosed in this application;
[0039] Figure 3 A schematic diagram of the content structure of a message model disclosed in this application;
[0040] Figure 4A schematic diagram of shard storage disclosed in this application;
[0041] Figure 5 A flowchart of a specific delayed message delivery method disclosed in this application;
[0042] Figure 6 A schematic diagram of a specific delayed message delivery module disclosed in this application;
[0043] Figure 7 A schematic diagram of the structure of a delayed message delivery device disclosed in this application;
[0044] Figure 8 This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION
[0045] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0046] Delayed messages have a wide range of application scenarios in Internet systems. However, the current delayed message delivery delay level is only 18, which cannot cope with a wide range of business scenarios, limiting the use of this message delay processing system. In addition, when there are many delayed messages, it will cause storage pressure and performance degradation.
[0047] To this end, the present application provides a delayed message delivery solution that can make the delivery time of delayed messages unlimited, support more usage scenarios, and control the amount of data stored in the message, reducing the overhead on server memory.
[0048] The embodiment of the present invention discloses a method for delivering a delayed message, see Figure 2 As shown, the method includes:
[0049] Step S11: Obtain the delayed message produced by the message producer, and determine the delivery time of the delayed message.
[0050] In the embodiment of the present application, the message producer can produce ordinary messages or delayed messages, etc. Therefore, it is necessary to first determine the message type produced by the message producer. If an ordinary message is produced, it means that the message does not need delayed delivery, and the ordinary message is stored in the message storage file according to the current time, and is consumed by the message consumer immediately after entering the consumption queue; if a delayed message is produced, it means that the message carries the time information of delayed delivery, and it is necessary to further obtain the delayed message, and then mark the delayed message, that is, modify the title of the delayed message to the target title, and then determine its delivery time to achieve the delivery of the delayed message.
[0051] like Figure 3 The following are some properties contained in the message. It should be noted that the typical message model includes message title (TOPIC), message content (BODY), and context properties (properties). The message title is used to mark the subject of the message, the message content is used to record the content of the message, and the context properties include message size, message queue id, message queue offset, etc. After determining that the message produced by the message producer is a delayed message, modifying the title of the delayed message can mark the message as a delayed message.
[0052] In the embodiment of the present application, the delivery time is determined by the storage time of the delayed message and the delay time of the delayed message delivery. Specifically, the storage time and the delay time are summed and the absolute value is taken to determine the delivery time of the delayed message. Taking computer time as an example, that is, the total number of milliseconds from 00:00:00 on January 1, 1970, Greenwich Mean Time (GMT) to the present, if the storage time of the delayed message is 00:00:00 on May 31, 2021, it needs to be delayed for one day, that is, delayed for 24 hours, so the delivery time of the message = message storage time + delay time = 1653955200000 + 86400000 = 1654041600000 milliseconds, that is, the message is delivered at 00:00:00 on June 1, 2021.
[0053] Step S12: Compare the delivery time with the time of the time wheel to obtain a corresponding comparison result, and put the offset of the delayed message in the current message queue into the time wheel; wherein, the time wheel is obtained by slicing the time according to a preset slicing length.
[0054] In the embodiment of the present application, the method of time rotation in the time slice is used to determine how the delayed message is delivered. Specifically, if the delivery time is not greater than the current time of the time wheel, it means that the delivery time point of the delayed message has arrived or has exceeded the delivery time point of the delayed message, then the target title is restored to the title of the ordinary message to convert the delayed message into a corresponding ordinary message and deliver it to the message consumer. For example, if the time slice is designed to be 30 minutes, it takes 30 minutes for the time wheel to rotate a time slice. If a delayed message is stored at 8:00 and the message is to be delivered at 8:25, when the time of the time wheel rotates to 26 minutes, the delivery time is less than the current time of the time wheel, so the mark of the delayed message can be eliminated, and the delayed message can be converted into an ordinary message for delivery.
[0055] In an embodiment of the present application, if the delivery time is greater than the current time of the time wheel, it means that the delivery time of the delayed message has not yet arrived. In a specific implementation, when the time slice where the delivery time is located is the same as the current time slice where the time wheel is located, it means that in the current time slice, it is only necessary to wait for the time wheel to reach the delivery time to deliver the delayed message. It should be noted that if the time slice where the delivery time is located is the same as the current time slice where the time wheel is located, the offset of the delayed message in the current message queue is placed in the time wheel. In this way, the offset of the message is used to identify the delayed message, and the offset is placed in the time wheel, which can greatly reduce the overhead on the server memory.
[0056] In another specific implementation, the time slice where the delivery time is located is the same as the first time slice where the time wheel is located, and the time wheel in the first time slice is not empty. It should be noted that the first time slice is the slice after the slice where the current time wheel is located. If the time slice where the delivery time is located is the next time slice of the time wheel, and the next time slice is not empty, the offset of the delayed message in the current message queue is placed in the time wheel of the first time slice, that is, the offset of the delayed message in the current message is placed in the corresponding time slice.
[0057] In the embodiment of the present application, the delayed message is stored in segments. Specifically, the storage location of the delayed message in the message storage file is determined based on the delivery time and the preset segment length, so that the delayed message is stored accordingly according to the storage location.
[0058] For example, Figure 4The figure shows a schematic diagram of storing delayed messages by shards. When storing, the messages are hashed according to their delivery time. Half of the shards are designed to be 30 minutes, which is not specifically limited here. If the amount of data in a single shard is large, the time of the shard can be adjusted to a smaller size. This can keep the amount of data in a single shard at a reasonable level without causing storage pressure or performance degradation.
[0059] Among them, the calculation formula for the fragment position = delayed message delivery time (milliseconds) / fragment length / 1000 milliseconds. For example, if the delivery time of the delayed message (computer time) is 179776800251 milliseconds, then the fragment position of this delayed message is substituted into the formula, and the fragment position = 179776800251 / 1800 / 1000 = 99876 is obtained, so this message is stored in the fragment numbered 99876.
[0060] Step S13: Based on the comparison result and using the offset currently obtained from the time wheel, the content of the delayed message is obtained from the message storage file, so as to deliver the delayed message to the message consumer.
[0061] In the embodiment of the present application, because the offset of the delayed message is stored in the corresponding time slice, the comparison result obtained based on the delivery time and the time in the time wheel can be used to quickly find important information such as the message body of the message from the message storage file corresponding to the message queue using the offset obtained from the time theory, obtain the content of the delayed message, and then return it to the message consumer.
[0062] In this application, firstly, the delayed message produced by the message producer is obtained, and the delivery time of the delayed message is determined; then the delivery time is compared with the time of the time wheel to obtain the corresponding comparison result, and the offset of the delayed message in the current message queue is placed in the time wheel; wherein the time wheel is obtained by slicing the time according to the preset slicing length; finally, based on the comparison result and using the offset currently obtained from the time wheel, the content of the delayed message is obtained from the message storage file, so as to deliver the delayed message to the message consumer. It can be seen that the time slice obtained by slicing the time according to the preset slicing length is combined with the delivery of the delayed message, and the delayed message with customized delay is supported. There is no limit on the delayed delivery time of the delayed message, and it can be delivered and consumed after the delivery time is reached. Compared with the original scheme in which the delay time is limited to 18 levels and cannot be supported outside the 18 levels, this application breaks through the original limitation, can have a larger usage scenario, and has greater value. In addition, delayed messages are stored in time slices. Due to the existence of slices, the data volume of a single slice is not particularly large. If the data volume of a single slice is large, the slice time can be adjusted to a smaller size, so that the data volume of the slice is controlled at a reasonable level, which will not bring storage pressure or performance degradation. Furthermore, using the offset of the message to identify the delayed message and putting the offset in the time wheel can greatly reduce the overhead of the server memory.
[0063] The present application embodiment discloses a specific method for delivering a delayed message, see Figure 5 As shown, the method includes:
[0064] Step S21: Obtain the delayed message produced by the message producer, and determine the delivery time of the delayed message.
[0065] Step S22: Compare the delivery time with the time of the time wheel to obtain a corresponding comparison result, and put the offset of the delayed message in the current message queue into the time wheel; wherein, the time wheel is obtained by slicing the time according to a preset slicing length.
[0066] For more specific processing procedures of the above steps S21 and S22, reference may be made to the corresponding contents disclosed in the above embodiments, which will not be described in detail here.
[0067] Step S23: Based on the comparison result and using the offset currently obtained from the time wheel, obtain the content of the delayed message from the message storage file, and determine whether the time wheel has started to load the second time slice; wherein the second time slice is the next time slice of the current time slice of the time wheel.
[0068] It is understandable that after consuming the current delayed message, it is also necessary to determine whether the next delayed message needs to be delivered, that is, to determine whether the loading of the next time slice has begun.
[0069] It should be pointed out that after the delayed message is obtained, if the time slice where the delivery time is located is the same as the first time slice where the time wheel is located, the offset of the delayed message in the message storage is placed in the first time slice. Because the first time slice is the time slice after the preset number of time slices where the time wheel is located, when the preset number is one, it proves that the current slice is the next time slice of the current time slice where the time wheel is located, that is, the second time slice.
[0070] Step S24: If the time wheel has started to load the second time slice, determine whether the time wheel has reached the second time slice.
[0071] In the embodiment of the present application, if the second time slice has been loaded, it is further determined whether the second time slice has been reached. If so, the time wheel needs to be replaced, and the time of the time wheel and the consumption position of the current message queue need to be updated, so as to deliver the delayed message of the next delivery time. If the next time slice has been loaded but the second time slice has not been reached, it means that the current time slice time wheel has not rotated to the end, but the delayed message of the next time wheel is about to be delivered, so the time of the time wheel and the consumption position are directly updated to further prepare for delivering the next delayed message.
[0072] Step S25: If the time wheel has not started to load the second time slice, it is determined whether the time of the time wheel is less than a preset threshold from the time of the second time slice.
[0073] In an embodiment of the present application, if the loading of the second time slice has not started, it means that there is still some time before the delayed message is delivered on the second time slice, and then it is further determined whether it is close to the second time slice. That is, it is determined whether the time of the time wheel is less than the preset threshold from the time of the second time slice. If so, it means that the time of the time wheel is close to the second time slice. At this time, the next message storage file is asynchronously loaded to the time wheel on the second time slice, and the loading of the second time slice is started, and then the step of updating the time of the time wheel is executed, and the current consumption site of the message queue is updated to deliver the next delayed message; if otherwise, the step of directly updating the time of the time wheel is executed, and the consumption site of the current message queue is updated to deliver the next delayed message.
[0074] like Figure 6The figure shows a specific delayed message delivery process diagram, which mainly includes five modules: delayed message processing module, commitlog / message storage module, message queue module, DispatchMsg / message distribution module and TimeWheelTask / rotation time slice module.
[0075] 1. In the delayed message processing module, if the message producer produces a delayed message, after determining that it is a delayed message, the delayed message is processed: the title of the delayed message is modified to: SCHEDULE_TOPIC, and topic = ScheduleMessageService.SCHEDULE_TOPIC is set; at the same time, the message is marked as a delayed message, and then the title, content body and other information of the delayed message are stored in the message storage module.
[0076] 2. In the commitlog / message storage module, hashing is performed according to the message delivery time. Ordinary messages are stored in the current shard of the message storage module, and delayed messages are stored in the corresponding shard of the message storage module according to the delayed delivery time. The length of the shard and the storage amount can be controlled. The specific storage location can refer to the previous embodiment and will not be repeated here.
[0077] 3. In the message queue module, the message will be replayed to generate the consumer queue (consumerqueue) of the corresponding topic. If it is a normal message, it will be consumed immediately by the message consumer; if it is a message with a delay mark and the message title is SCHEDULE_TOPIC, it will not be consumed. The storage file structure in the message queue module is as follows: offset represents the offset of this message in the message queue; size represents the message size; message tag hashcode represents the hash value of the tag parameter, which is mainly used for filtering. It can be seen that if the topic and queue are known, the consumer queue file can be used to read the message sequentially according to the consumer's consumption progress, and then the obtained offset can be used to quickly find the message body and other important information from the message queue, and then return it to the message consumer.
[0078] 4. In the DispatchMsg / distribution message module, the main function of this module is to process the messages with the title SCHEDULE_TOPIC one by one according to the delivery time. If the delivery time has arrived, the flag of the delayed message is cleared, the expiration offset is obtained from the time wheel according to the current time, that is, the offset of the message in the message storage is pointed to, the message content is obtained from the message storage according to the offset, the message title is restored, and the message title is changed from SCHEDULE_TOPIC to the real title of the message. If the time slice where the delivery time is located is equal to the time slice where the time wheel time is located, the offset of the delayed message in the message storage is put into the time wheel, the delayed message is put into the message storage, the expiration offset is obtained from the time wheel according to the current time, the message content is obtained from the message storage according to the offset, the message title is restored, and the message title is changed from SCHEDULE_TOPIC to the real title of the message. If the delivery time has not yet arrived, the time slice where the delivery time is located is equal to the next time slice where the time wheel time is located, and the next time wheel is not empty, which means that the message is in the next time slice and the next time wheel has been loaded, then the offset of the delayed message in the message storage is put into the next time wheel, that is, the message is stored in the message storage module according to the slice where the delivery time is located, and then the expiration offset is obtained from the time wheel according to the current time, the message content is obtained from the message storage according to the offset, the message title is restored, and the message title is changed from SCHEDULE_TOPIC to the real title of the message. Determine whether the next time slice has arrived, and if so, switch to the next time slice. If not, update the time in the time wheel, update the consumption position of the delayed consumption queue, and then deliver the delayed message of the next delivery time.
[0079] 5. In the TimeWheelTask / rotating time slice module, this module mainly obtains the data in the corresponding slice according to the current time, obtains the message content from the message storage according to the offset, and restores the message title to the original actual title. Therefore, at this time, the delayed message has no delayed message mark, the message content is normal, and has been converted into a normal message, which can be consumed and processed by the message consumer as a normal message.
[0080] In summary, the delayed message processing module, commitlog / message storage module, DispatchMsg / message distribution module, TimeWheelTask / rotation time slice module and other parts form a complete full process, so that the delay can be set and defined, and the granularity of the delay setting is fine enough, and the delay range can be achieved from 0 to days. In addition, this solution also involves a delayed message persistence solution, which does not affect the restart and recovery of the service. At the same time, the design complexity of the system is not high, thereby saving costs.
[0081] In this application, firstly, the delayed message produced by the message producer is obtained, and the delivery time of the delayed message is determined; then the delivery time is compared with the time of the time wheel to obtain the corresponding comparison result, and the offset of the delayed message in the current message queue is placed in the time wheel; wherein the time wheel is obtained by slicing the time according to the preset slicing length; finally, based on the comparison result and using the offset currently obtained from the time wheel, the content of the delayed message is obtained from the message storage file, so as to deliver the delayed message to the message consumer. It can be seen that the time slice obtained by slicing the time according to the preset slicing length is combined with the delivery of the delayed message, and the delayed message with customized delay is supported. There is no limit on the delayed delivery time of the delayed message, and it can be delivered and consumed after the delivery time is reached. Compared with the original scheme in which the delay time is limited to 18 levels and cannot be supported outside the 18 levels, this application breaks through the original limitation, can have a larger usage scenario, and has greater value. In addition, delayed messages are stored in time slices. Due to the existence of slices, the data volume of a single slice is not particularly large. If the data volume of a single slice is large, the slice time can be adjusted to a smaller size, so that the data volume of the slice is controlled at a reasonable level, which will not bring storage pressure or performance degradation. Furthermore, using the offset of the message to identify the delayed message and putting the offset in the time wheel can greatly reduce the overhead of the server memory.
[0082] Correspondingly, the embodiment of the present application also discloses a delayed message delivery device, see Figure 7 As shown, the device comprises:
[0083] A delivery time determination module 11 is used to obtain a delayed message produced by a message producer and determine a delivery time of the delayed message;
[0084] The offset storage module 12 is used to compare the delivery time with the time of the time wheel to obtain a corresponding comparison result, and put the offset of the delayed message in the current message queue into the time wheel; wherein the time wheel is obtained by slicing the time according to a preset slicing length;
[0085] The message delivery module 13 is used to obtain the content of the delayed message from the message storage file based on the comparison result and using the offset obtained from the time wheel according to the current time, so as to deliver the delayed message to the message consumer.
[0086] Among them, for more specific working processes of the above-mentioned modules, please refer to the corresponding contents disclosed in the aforementioned embodiments, which will not be repeated here.
[0087] It can be seen that, through the above scheme of this embodiment, first obtain the delayed message produced by the message producer, and determine the delivery time of the delayed message; then compare the delivery time with the time of the time wheel to obtain the corresponding comparison result, and put the offset of the delayed message in the current message queue into the time wheel; wherein, the time wheel is obtained by slicing the time according to the preset slicing length; finally, based on the comparison result and using the offset currently obtained from the time wheel, obtain the content of the delayed message from the message storage file, so as to deliver the delayed message to the message consumer. It can be seen that the time slice obtained by slicing the time according to the preset slicing length is combined with the delivery of the delayed message, and the delayed message with customized delay is supported. There is no limit on the delayed delivery time of the delayed message, and it can be delivered and consumed after the delivery time is reached. Compared with the original scheme in which the delay time is limited to 18 levels and cannot be supported outside the 18 levels, this application breaks through the original limitation, can have a larger usage scenario, and has greater value. In addition, delayed messages are stored in time slices. Due to the existence of slices, the data volume of a single slice is not particularly large. If the data volume of a single slice is large, the slice time can be adjusted to a smaller size, so that the data volume of the slice is controlled at a reasonable level, which will not bring storage pressure or performance degradation. Furthermore, using the offset of the message to identify the delayed message and putting the offset in the time wheel can greatly reduce the overhead of the server memory.
[0088] Furthermore, the present application also discloses an electronic device. Figure 8 This is a structural diagram of an electronic device 20 according to an exemplary embodiment, and the content in the diagram cannot be considered as any limitation on the scope of use of the present application.
[0089] Figure 8 A schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the delayed message delivery method disclosed in any of the aforementioned embodiments. In addition, the electronic device 20 in this embodiment may specifically be a computer.
[0090] In this embodiment, the power supply 23 is used to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present application, and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs and is not specifically limited here.
[0091] In addition, the memory 22 as a carrier for resource storage may be a read-only memory, a random access memory, a disk or an optical disk, etc. The resources stored thereon may include an operating system 221, a computer program 222 and data 223, etc. The data 223 may include various data. The storage method may be temporary storage or permanent storage.
[0092] The operating system 221 is used to manage and control the hardware devices and computer program 222 on the electronic device 20, which can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program that can be used to complete the delayed message delivery method performed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program that can be used to complete other specific tasks.
[0093] Furthermore, the embodiment of the present application also discloses a computer-readable storage medium, wherein the computer-readable storage medium mentioned here includes a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a magnetic disk or an optical disk, or any other form of storage medium known in the technical field. Wherein, when the computer program is executed by the processor, the aforementioned delayed message delivery method is implemented. For the specific steps of the method, reference can be made to the corresponding contents disclosed in the aforementioned embodiment, which will not be repeated here.
[0094] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0095] The delivery of delayed messages or the steps of the algorithm described in conjunction with the embodiments disclosed herein can be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module can be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0096] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0097] The above is a detailed introduction to the delayed message delivery method, device, equipment and storage medium provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A delayed message delivery method, characterized in that: include: Obtaining a delayed message produced by a message producer, and determining a delivery time of the delayed message; The delivery time is compared with the time of the time wheel to obtain a corresponding comparison result, and the offset of the delayed message in the current message queue is placed in the time wheel; wherein the time wheel is obtained by slicing the time according to a preset slice length; wherein, the delivery time is compared with the time of the time wheel to obtain a corresponding comparison result, and the offset of the delayed message in the current message queue is placed in the time wheel, including: when the time slice where the delivery time is located is the same as the current time slice where the time wheel is located, the offset of the delayed message in the current message queue is placed in the time wheel; when the time slice where the delivery time is located is the same as the first time slice where the time wheel is located, and the time wheel in the first time slice is not empty, the offset of the delayed message in the current message queue is placed in the time wheel of the first time slice; wherein the first time slice is the time slice after the preset number of time slices of the current time slice where the time wheel is located; Based on the comparison result and using the offset currently obtained from the time wheel, the content of the delayed message is obtained from the message storage file so as to deliver the delayed message to the message consumer; wherein, the obtaining of the content of the delayed message from the message storage file so as to deliver the delayed message to the message consumer includes: obtaining the content of the delayed message from the message storage file, and determining whether the time wheel has started to load the second time slice; wherein, the second time slice is the next time slice of the current time slice of the time wheel; if the time wheel has started to load the second time slice, determining whether the time wheel has reached the second time slice, and if so, replacing the time wheel, and then updating the time of the time wheel, and updating the current consumption position of the message queue, so as to deliver the next delayed message, if the next time slice has started to be loaded but If the second time slice has not arrived yet, directly execute the step of updating the time of the time wheel and updating the current consumption position of the message queue to deliver the next delayed message; if the time wheel has not started to load the second time slice, determine whether the time of the time wheel is less than the preset threshold value from the time of the second time slice; if the time of the time wheel is less than the preset threshold value from the time of the second time slice, asynchronously load the next message storage file to the time wheel on the second time slice, and start loading the second time slice, then execute the step of updating the time of the time wheel and updating the current consumption position of the message queue to deliver the next delayed message; if the time of the time wheel is not less than the preset threshold value from the time of the second time slice, directly execute the step of updating the time wheel and updating the current consumption position of the message queue to deliver the next delayed message.
2. The method for delivering a delayed message according to claim 1, characterized in that: Before obtaining the delayed message produced by the message producer, the method further includes: Determine the type of message produced by the message producer; If the message type is a common message that does not require delayed delivery, the common message is stored in the message storage file, and the common message in the current message queue is immediately delivered to the message consumer; When the message type is the delayed message, the title of the delayed message is modified to the target title to mark the delayed message, and then the step of obtaining the delayed message produced by the message producer and determining the delivery time of the delayed message is triggered.
3. The method for delivering a delayed message according to claim 2, characterized in that: The step of comparing the delivery time with the time of the time wheel to obtain a corresponding comparison result includes: When the delivery time is not greater than the current time of the time wheel, the target title is restored to the title of the common message, so as to convert the delayed message into a corresponding common message and deliver it to the message consumer.
4. The method for delivering a delayed message according to claim 1, characterized in that: The determining the delivery time of the delayed message includes: Obtaining the storage time of the delayed message and the delay time of the delayed message delivery; The storage time and the delay time are summed and the absolute value is taken to determine the delivery time of the delayed message.
5. The method for delivering delayed messages according to claim 1, characterized in that: After comparing the delivery time with the time of the time wheel to obtain a corresponding comparison result and placing the offset of the delayed message in the current message queue into the time wheel, the method further includes: The storage position of the delayed message in the message storage file is determined based on the delivery time and the preset segment length, so that the delayed message is stored accordingly according to the storage position.
6. A delayed message delivery device, characterized in that: include: A delivery time determination module, used to obtain the delayed message produced by the message producer and determine the delivery time of the delayed message; An offset storage module, used for comparing the delivery time with the time of the time wheel to obtain a corresponding comparison result, and putting the offset of the delayed message in the current message queue into the time wheel; wherein the time wheel is obtained by slicing the time according to a preset slice length; wherein the offset storage module is specifically used for putting the offset of the delayed message in the current message queue into the time wheel when the time slice where the delivery time is located is the same as the current time slice where the time wheel is located; when the time slice where the delivery time is located is the same as the first time slice where the time wheel is located, and the time wheel in the first time slice is not empty, putting the offset of the delayed message in the current message queue into the time wheel of the first time slice; wherein the first time slice is the time slice after the preset number of time slices where the time wheel is located; A message delivery module, used to obtain the content of the delayed message from the message storage file based on the comparison result and using the offset obtained from the time wheel according to the current time, so as to deliver the delayed message to the message consumer; wherein the message delivery module is specifically used to: obtain the content of the delayed message from the message storage file, and determine whether the time wheel has started to load the second time slice; wherein the second time slice is the next time slice of the current time slice of the time wheel; if the time wheel has started to load the second time slice, determine whether the time wheel has reached the second time slice, if it has reached the second time slice, replace the time wheel, then update the time of the time wheel, and update the current consumption position of the message queue to deliver the next delayed message, if the next time slice has started to be loaded but the second time slice has not yet arrived time slice, directly execute the step of updating the time of the time wheel and updating the current consumption position of the message queue to deliver the next delayed message; if the time wheel has not started to load the second time slice, determine whether the time of the time wheel is less than the preset threshold value from the time of the second time slice; if the time of the time wheel is less than the preset threshold value from the time of the second time slice, asynchronously load the next message storage file to the time wheel on the second time slice, and start loading the second time slice, then execute the step of updating the time of the time wheel and updating the current consumption position of the message queue to deliver the next delayed message; if the time of the time wheel is not less than the preset threshold value from the time of the second time slice, directly execute the step of updating the time of the time wheel and updating the current consumption position of the message queue to deliver the next delayed message.
7. An electronic device, characterized in that: The electronic device includes a processor and a memory; wherein the memory is used to store a computer program, and the computer program is loaded and executed by the processor to implement the delayed message delivery method as described in any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that: Used to store computer programs; wherein the computer program, when executed by a processor, implements the delayed message delivery method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Message delayed delivering method, device and equipment
CN109450778A
Message delay delivery method, device and equipment
CN109726024A
