A method, device, electronic device and storage medium for processing timed message sending
By storing data in a database and redis hset and using periodic timing tasks, the method addresses high complexity and load issues in traditional systems, ensuring timely and efficient message delivery.
Patent Information
- Application Number
- CN202311235891.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-25
- Publication Date
- 2024-12-17
- Estimated Expiration
- 2043-09-25
AI Technical Summary
Traditional scheduled message sending systems suffer from high algorithm complexity (O(n)) and frequent database scanning, leading to performance issues and database pressure, which compromises message timeliness.
Implement a method that stores data in a database and redis hset, using a periodic timing task to retrieve and send messages, reducing retrieval complexity and database pressure by sorting messages based on planned sending time.
This approach enhances message timeliness and reduces database and server load by optimizing message retrieval and processing, ensuring timely delivery with improved performance.
Smart Images

Figure CN117312408B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a method, device, electronic device and storage medium for processing timed message sending. Background Art
[0002] In traditional scheduled message sending systems, the entire message table in the database is usually scanned periodically to determine which messages have reached the sending time and which have not yet reached the sending time. Then, the messages that have reached the sending time are queried in the server (JVM), and then these messages are traversed and sent. After each message is successfully sent, the status of the message in the database is updated to sent until all messages are traversed and processed. Figure 1 The traditional timed message processing method has the following two serious problems:
[0003] 1. In each processing cycle, since each message in the database needs to be traversed, the algorithm complexity is O(n), so the performance is very low, which severely limits the scanning frequency and makes it impossible to meet the timeliness of the message;
[0004] 2. Periodic and high-frequency scanning of the database will put tremendous pressure on the database, affecting the database's processing of other businesses and even causing database service downtime. Summary of the invention
[0005] The purpose of the present invention is to solve the defects mentioned in the above background technology by proposing a processing method, device, electronic device and storage medium for timing message sending.
[0006] The technical solution adopted by the present invention is as follows:
[0007] A method for processing scheduled message sending is provided, comprising the following steps:
[0008] S1: receiving first service data;
[0009] S2: extract the second business data according to the first business data and store it in the database, and at the same time copy the second business data and store it in redis through the redis hset data structure;
[0010] S3: deploy a periodic scheduled task with a preset period in the server, and retrieve a long integer value whose score value is less than the current time in each preset period;
[0011] S4: Processing the retrieved score value being less than the long integer value of the current time.
[0012] As a preferred technical solution of the present invention: in S2, the second business data stored in the database includes: message unique identifier, planned sending time, sending status, actual sending time, sender, receiver and other message contents.
[0013] As a preferred technical solution of the present invention: in the redis hset data structure in S2, the message string is used as the primary key, and the content of each second business data is converted into a json string as data, and the long integer value of the planned sending time is used as the score value.
[0014] As a preferred technical solution of the present invention: the hset sorts the stored second business data in ascending order according to the score values.
[0015] As a preferred technical solution of the present invention: in S3, the periodic timed task is deployed on the server together with the service code of the server, and the service starts working after it is started, and the task is triggered to be executed once every preset period.
[0016] As a preferred technical solution of the present invention: in S3, the period is set according to the timeliness of the message, and each period retrieves a long integer value whose score value is less than the current time.
[0017] As a preferred technical solution of the present invention: in S4, if the long integer value whose score value is less than the current time is not retrieved, then continue to wait for the next retrieval cycle to search again; if the long integer value whose score value is less than the current time is retrieved, then traverse the data to process each long integer value whose score value is less than the current time, and repeat S4.1-S4.3 until all the data whose score value is less than the long integer value of the current time is processed; then execute according to the following steps:
[0018] S4.1: Convert the long integer value whose score value is less than the current time into the second service data and send it;
[0019] S4.2: Modify the corresponding second service data sending status in the database to the sent status, and update the actual sending time;
[0020] S4.3: Delete the data in redis.
[0021] A device for processing timed message sending is provided, comprising:
[0022] Data receiving unit: used for receiving first service data;
[0023] Data storage unit: used to extract the second business data according to the first business data and store it in the database, and at the same time copy the second business data and store it in redis through the redis hset data structure;
[0024] Timing retrieval unit: used to deploy a periodic timing task with a preset period in the server, and retrieve a long integer value whose score value is less than the current time in each preset period;
[0025] Retrieval processing unit: used to process the long integer value whose retrieved score value is less than the current time.
[0026] An electronic device is provided, comprising a memory, a processor and a computer program stored in the memory, wherein when the computer program is executed by the processor, the above-mentioned processing method for sending a timed message is implemented.
[0027] A computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the above-mentioned processing method for sending a timed message is implemented.
[0028] Compared with the prior art, the method, device, electronic device and storage medium for processing scheduled message sending provided by the present invention have the following beneficial effects:
[0029] The present invention stores data in a database, pushes messages to redis hset while storing data in the database, and uses a periodic timing task in a server to poll the hset for messages that meet the timing conditions and send them, and then modifies the database sending record and sending time. Sorting messages through redis hset and periodic timing tasks can reduce retrieval complexity and provide high-speed retrieval functions, and can act on a single or multiple service objects, can accurately send daily messages at a fixed time, and can solve the problems of high database and server pressure and untimely sending of scheduled messages caused by traditional scheduled message sending. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 This is a schematic diagram of traditional timed message processing;
[0031] Figure 2 A method flow chart of a preferred embodiment of the present invention;
[0032] Figure 3 It is a structural block diagram of a device in a preferred embodiment of the present invention;
[0033] Figure 4 The present invention is a flowchart of a method for processing timed message sending according to a specific embodiment of the present invention.
[0034] The meanings of the various marks in the figure are: 100, data receiving unit; 200, data storage unit; 300, timing retrieval unit; 400, retrieval processing unit. DETAILED DESCRIPTION
[0035] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of this embodiment can be combined with each other. The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings 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.
[0036] Reference Figure 1 The preferred embodiment of the present invention provides a method for processing timed message sending, comprising the following steps:
[0037] S1: receiving first service data;
[0038] S2: extract the second business data according to the first business data and store it in the database, and at the same time copy the second business data and store it in redis through the redis hset data structure;
[0039] S3: deploy a periodic scheduled task with a preset period in the server, and retrieve a long integer value whose score value is less than the current time in each preset period;
[0040] S4: Processing the retrieved score value being less than the long integer value of the current time.
[0041] In S2, the second business data stored in the database includes: message unique identifier, planned sending time, sending status, actual sending time, sender, receiver and other message contents.
[0042] In the redis hset data structure in S2, the message string is used as the primary key, and the content of each second business data is converted into a json string as data, and the long integer value of the planned sending time is used as the score value.
[0043] The hset sorts the stored second business data in ascending order according to the score values.
[0044] In S3, the periodic timed task is deployed on the server together with the service code of the server, and the service starts working after it is started, and the task is triggered to be executed once every preset period.
[0045] In S3, the period is set according to the timeliness of the message, and each period retrieves a long integer value whose score value is less than the current time.
[0046] In S4, if the long integer value whose score value is less than the current time is not retrieved, continue to wait for the next retrieval cycle to search again; if the long integer value whose score value is less than the current time is retrieved, traverse the data to process each long integer value whose score value is less than the current time, repeat S4.1-S4.3 until all the data whose score value is less than the long integer value of the current time is processed; then execute according to the following steps:
[0047] S4.1: Convert the long integer value whose score value is less than the current time into the second service data and send it;
[0048] S4.2: Modify the corresponding second service data sending status in the database to the sent status, and update the actual sending time;
[0049] S4.3: Delete the data in redis.
[0050] Reference Figure 2 , a processing device for sending a timed message is provided, comprising:
[0051] The data receiving unit 100 is configured to receive first service data;
[0052] Data storage unit 200: used to extract second business data according to the first business data and store it in the database, and at the same time copy the second business data and store it in redis through the redis hset data structure;
[0053] The timing search unit 300 is used to deploy a periodic timing task with a preset period in the server, and search for a long integer value whose score value is less than the current time in each preset period;
[0054] Retrieval processing unit 400: used to process the long integer value whose retrieved score value is less than the current time.
[0055] An electronic device is provided, comprising a memory, a processor and a computer program stored in the memory, wherein when the computer program is executed by the processor, the above-mentioned processing method for sending a timed message is implemented.
[0056] A computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the above-mentioned processing method for sending a timed message is implemented.
[0057] In one embodiment, a business message, such as a user's order message, is received, and a message to be sent, such as a reminder message, is extracted from the business message, and when the message to be sent is stored in the database, a copy is simultaneously stored in redis; the message content stored in the database includes at least: the message unique identifier id, the planned sending time plan_send_time, the sending state send_state, the actual sending time send_time, the sender sender, the receiver receiver and other message content (message title, message content, message type, etc.); when the message is stored in redis, redis is used The hset data structure is stored, and the "message" string is used as the primary key. The primary key can be regarded as the unique name of a type of data set. For example, the key of message data can be message; the key of user data can be user; the content of each message is converted into a json string as data, and the long integer value of the planned sending time plan_send_time (the number of milliseconds from January 1, 1970 00:00:00 to plan_send_time) is used as the score value, that is, score (the score must be a value that can be compared in size, and hset sorts data by the size of the score. The smaller the score, the first data retrieved is the data with the smallest score. Using the long integer value of plan_send_time as the score can meet the hset sorting needs, and the retrieved data is the earliest data).
[0058] Start a periodic scheduled task in the server (JVM) (a periodic scheduled task is a piece of code written by backend programmers, or an open source framework. There are many open source scheduled task frameworks, such as spring's scheduled, xxx-job, java's own Timer and TimerTask, ScheduledExecutorService, etc. The periodic scheduled task is deployed on the server together with the server-side business code. It starts working after the service is started, and triggers the task execution once every specific period. The period can be set in the configuration). The period is set according to the timeliness of the message (for example, if the timeliness of the message is required to be within 1s, the period of the scheduled task cannot be less than 1s). In each period, a long integer value with a score less than the current time is retrieved.
[0059] Process the scanned data:
[0060] If no data is scanned, nothing is done and the system continues to wait for the next scanning cycle to scan again;
[0061] If data is scanned, it will traverse the data to process each piece of data, repeating the following steps until all data is processed:
[0062] Convert data into messages and send them;
[0063] Modify the sending status send_state in the database to the sent status, and update the actual sending time send_time;
[0064] Delete this data in redis.
[0065] In one embodiment, taking a hotel reservation system as an example, referring to Figure 3 When a user places an order at a hotel, they will set the check-in time start_time and contact information. After the order is placed, the check-in time and contact information of the order are extracted to generate the second business data. At this time, the order status is "pending check-in". Now it is hoped that if the user has not checked in half an hour before the check-in time, a reminder SMS will be sent to the user, and the time error of the SMS sending shall not exceed one minute.
[0066] The reminder message to be sent is stored in the database, and a copy is stored in redis at the same time. The message content stored in the database at least includes: the unique identification ID of the reminder message to be sent, the planned sending time plan_send_time of the reminder message to be sent, that is, the planned sending time is half an hour before the user's check-in time, the sending state send_state of the reminder message to be sent, the actual sending time send_time of the reminder message to be sent, the sender of the reminder message to be sent, that is, the hotel, the receiver of the reminder message to be sent, that is, the user, and other message content (such as message title, message content, message type, etc.);
[0067] When the reminder message to be sent is stored in redis, it is stored using the redis hset data structure, with the "message" string as the primary key. The content of each message is converted into a json string as data, and the long integer value of the planned sending time plan_send_time is used as the score.
[0068] Write a periodic scheduled task on the background service (JVM). The period is set according to the timeliness of the message. Since the time error of sending short messages is set to no more than one minute, that is, the timeliness of the message is within 1 minute, the period of the scheduled task is not less than 1 minute. When the periodic scheduled task is triggered, each cycle scans the long integer value whose score is less than the current time. If no data is scanned, do nothing and continue to wait for the next scanning cycle to scan again; if data is scanned, traverse the data to process each order data, and repeat the following steps until all order data is processed:
[0069] Send reminder information according to the contact information reserved by the user in the order data;
[0070] Modify the sending status send_state of this order in redis to the sent status, and update the actual sending time send_time;
[0071] Then delete the order data in redis to complete the check-in reminder for the current user.
[0072] It will be apparent to those skilled in the art that the invention is not limited to the details of the exemplary embodiments described above and that the invention can be implemented in other specific forms without departing from the spirit or essential features of the invention. Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the invention is defined by the appended claims rather than the foregoing description, and it is intended that all variations falling within the meaning and scope of the equivalent elements of the claims be included in the invention. Any reference numeral in a claim should not be considered as limiting the claim to which it relates.
[0073] In addition, it should be understood that although the present specification is described according to implementation modes, not every implementation mode contains only one independent technical solution. This description of the specification is only for the sake of clarity. Those skilled in the art should regard the specification as a whole. The technical solutions in each embodiment may also be appropriately combined to form other implementation modes that can be understood by those skilled in the art.
Claims
1. A method for processing timed message sending, characterized in that: The steps include: S1: receiving first service data; S2: extract the second business data according to the first business data and store it in the database, and at the same time copy the second business data and store it in redis through the redis hset data structure; S3: deploy a periodic scheduled task with a preset period in the server, and retrieve a long integer value whose score value is less than the current time in each preset period; S4: Processing the long integer value whose retrieved score value is less than the current time; The hset sorts the stored second business data in ascending order according to the score value; In the redis hset data structure in S2, the message string is used as the primary key, and the content of each second business data is converted into a json string as data, and the long integer value of the planned sending time is used as the score value; In S4, if the long integer value whose score value is less than the current time is not retrieved, continue to wait for the next retrieval cycle to search again; if the long integer value whose score value is less than the current time is retrieved, traverse the data to process each long integer value whose score value is less than the current time, repeat S4.1-S4.3 until all the data whose score value is less than the long integer value of the current time is processed; then execute according to the following steps: S4.1: Convert the long integer value whose score value is less than the current time into the second service data and send it; S4.2: Modify the corresponding second service data sending status in the database to the sent status, and update the actual sending time; S4.3: Delete the data in redis.
2. The method for processing scheduled message sending according to claim 1, characterized in that: In S2, the second business data stored in the database includes: message unique identifier, planned sending time, sending status, actual sending time, sender, receiver and other message contents.
3. The method for processing scheduled message sending according to claim 1, characterized in that: In S3, the periodic timed task is deployed on the server together with the service code of the server, and the service starts working after it is started, and the task is triggered to be executed once every preset period.
4. The method for processing scheduled message sending according to claim 3, characterized in that: In S3, the period is set according to the timeliness of the message, and each period retrieves a long integer value whose score value is less than the current time.
5. An electronic device comprising a memory, a processor and a computer program stored in the memory, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.
6. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Timing information scheduling method, device, setting, medium and program
CN116795510A
