Message aggregation consumption method and system
By recording the message type and data volume in the message queue, aggregating messages by type, and sending them to the thread pool for split consumption, the problem of reduced message consumption rate caused by frequent thread switching is solved and more efficient message processing is achieved.
Patent Information
- Application Number
- CN202410891068.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-04
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2044-07-04
AI Technical Summary
In application scenarios with high concurrency and high throughput, when the existing technology processes messages through message queues and thread pools, threads are frequently switched, which affects the message consumption rate. Especially when there are a large number of small messages, the thread pool tasks are occupied too much, resulting in consumption. The rate decreases.
The client records the message type and data amount as key names, stores the messages in the message queue, and the server aggregates the messages by type when consuming, and sends the aggregated messages to the thread pool, which is split and consumed through the threads in the thread pool. Set thresholds to optimize thread pool utilization.
It reduces the usage of threads in the thread pool, reduces the IO consumption of message consumption, and increases the message consumption rate.
Smart Images

Figure CN118860686A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of message processing, and in particular to a method and system for message aggregation consumption. Background Art
[0002] Currently, most systems have high-concurrency and high-throughput application scenarios. In order to decouple various modules, the most common way is to use message middleware or message queues to decouple systems or modules.
[0003] The usual way is: after the client sends a message, it reaches the server, and the server will directly process such a message. In order to support high-concurrency operations, the server will further store the received messages through the message queue, and then consume the messages in this message queue and send them to a thread pool, and the threads in the thread pool will handle such message tasks. In this way, the message consumption rate can be increased to a certain extent. However, messages are large and small, and each message corresponds to a thread in the thread pool to handle this task. When there are a large number of small messages, a large number of tasks in the thread pool will be occupied, resulting in frequent thread switching, which will restrict the message consumption rate. Summary of the invention
[0004] The technical problem to be solved by the present invention is to provide a method and system for message aggregation consumption, which can improve the message consumption rate.
[0005] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0006] A method for message aggregation consumption, comprising the steps of:
[0007] The client receives the message, records the type and data volume of the message, uses the type and data volume of the message as a key name, and stores the message as a value in the message queue;
[0008] When the server consumes messages, it obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through threads in the thread pool.
[0009] In order to solve the above technical problems, another technical solution adopted by the present invention is:
[0010] A message aggregation consumption system, comprising:
[0011] The client is used to receive the message, record the type and data volume of the message, use the type and data volume of the message as a key name, and store the message as a value in the message queue;
[0012] The server, when consuming messages, obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through the threads in the thread pool.
[0013] The beneficial effects of the present invention are as follows: after the client receives a message, the type and data size of each message are recorded in advance, and the message is written into a message queue according to the type and data size of the message; when the server consumes the message, the message list is obtained from the message queue, and the messages are aggregated by type, and then these aggregated messages are sent to the thread pool. An aggregated message containing messages of the same type only needs one thread in the thread pool to process. In this way, after the corresponding aggregation is performed according to the message size, the IO consumption of subsequent message consumption can be reduced. At the same time, the message aggregation mode can effectively reduce the usage of threads in the thread pool, and can further improve the message consumption rate. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 A flowchart of a method for message aggregation consumption according to an embodiment of the present invention;
[0015] Figure 2 The present invention is a schematic diagram of a message aggregation consumption system according to an embodiment of the present invention. DETAILED DESCRIPTION
[0016] In order to explain the technical content, achieved objectives and effects of the present invention in detail, the following is an explanation in combination with the implementation modes and the accompanying drawings.
[0017] Please refer to Figure 1 The embodiment of the present invention provides a method for message aggregation consumption, comprising the steps of:
[0018] The client receives the message, records the type and data volume of the message, uses the type and data volume of the message as a key name, and stores the message as a value in the message queue;
[0019] When the server consumes messages, it obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through threads in the thread pool.
[0020] From the above description, it can be seen that the beneficial effects of the present invention are: when the client receives a message, the type and data size of each message are recorded in advance, and the message queue is written according to the type and data size of the message; when the server consumes the message, the message list is obtained from the message queue, and the messages are aggregated by type, and then these aggregated messages are sent to the thread pool. An aggregated message containing messages of the same type only needs one thread in the thread pool to process. In this way, after the corresponding aggregation is performed according to the size of the message, the IO consumption of subsequent message consumption can be reduced. At the same time, the message aggregation method can effectively reduce the usage of threads in the thread pool, which can further improve the message consumption rate.
[0021] Furthermore, the type and data volume of the message are used as key names, and the message is stored as a value in a message queue, further comprising:
[0022] In the message list of the message queue, the messages are sorted in ascending order of the key names.
[0023] It can be seen from the above description that by sorting the messages in the message list, it is convenient to aggregate the messages according to type later.
[0024] Furthermore, the messages of the same type in the key name are aggregated, including:
[0025] Setting an initial value of a first threshold, obtaining a thread occupancy ratio of a thread pool, and raising the first threshold according to the occupancy ratio;
[0026] Messages of the same type in the key name are aggregated in order according to the message list, and the data size of the aggregated messages is less than or equal to the first threshold.
[0027] From the above description, it can be seen that the upper limit of the data volume of the aggregated message can be set according to the specific situation of the thread pool, so as to perform message aggregation according to the specific situation of the thread pool and improve the utilization rate of the threads in the thread pool.
[0028] Further, the aggregated message is sent to the thread pool, including:
[0029] Setting an initial value of the second threshold, obtaining a thread occupancy ratio of the thread pool, and raising the second threshold according to the occupancy ratio;
[0030] It is determined whether the number of aggregate messages that are not currently sent to the thread pool reaches the second threshold; if so, all aggregate messages that are not currently sent to the thread pool are sent to the thread pool.
[0031] From the above description, it can be seen that the upper limit of the number of aggregated messages uploaded can be set according to the specific situation of the thread pool, so as to perform message aggregation according to the specific situation of the thread pool and improve the utilization rate of threads in the thread pool.
[0032] Furthermore, the aggregated message is split and consumed by threads in the thread pool, including:
[0033] It is determined by the threads in the thread pool whether the received message is an aggregate message. If so, the aggregate message is split and the split messages are consumed one by one.
[0034] From the above description, it can be seen that by aggregating messages and splitting aggregated messages, the usage of threads in the thread pool can be effectively reduced and the message consumption rate can be increased.
[0035] Please refer to Figure 2 Another embodiment of the present invention provides a message aggregation consumption system, including:
[0036] The client is used to receive the message, record the type and data volume of the message, use the type and data volume of the message as a key name, and store the message as a value in the message queue;
[0037] The server, when consuming messages, obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through the threads in the thread pool.
[0038] From the above description, it can be seen that the beneficial effects of the present invention are: when the client receives a message, the type and data size of each message are recorded in advance, and the message queue is written according to the type and data size of the message; when the server consumes the message, the message list is obtained from the message queue, and the messages are aggregated by type, and then these aggregated messages are sent to the thread pool. An aggregated message containing messages of the same type only needs one thread in the thread pool to process. In this way, after the corresponding aggregation is performed according to the size of the message, the IO consumption of subsequent message consumption can be reduced. At the same time, the message aggregation method can effectively reduce the usage of threads in the thread pool, which can further improve the message consumption rate.
[0039] Furthermore, the type and data volume of the message are used as key names, and the message is stored as a value in a message queue, further comprising:
[0040] In the message list of the message queue, the messages are sorted in ascending order of the key names.
[0041] It can be seen from the above description that by sorting the messages in the message list, it is convenient to aggregate the messages according to type later.
[0042] Furthermore, the messages of the same type in the key name are aggregated, including:
[0043] Setting an initial value of a first threshold, obtaining a thread occupancy ratio of a thread pool, and raising the first threshold according to the occupancy ratio;
[0044] Messages of the same type in the key name are aggregated in order according to the message list, and the data size of the aggregated messages is less than or equal to the first threshold.
[0045] From the above description, it can be seen that the upper limit of the data volume of the aggregated message can be set according to the specific situation of the thread pool, so as to perform message aggregation according to the specific situation of the thread pool and improve the utilization rate of the threads in the thread pool.
[0046] Further, the aggregated message is sent to the thread pool, including:
[0047] Setting an initial value of the second threshold, obtaining a thread occupancy ratio of the thread pool, and raising the second threshold according to the occupancy ratio;
[0048] It is determined whether the number of aggregate messages that are not currently sent to the thread pool reaches the second threshold; if so, all aggregate messages that are not currently sent to the thread pool are sent to the thread pool.
[0049] From the above description, it can be seen that the upper limit of the number of aggregated messages uploaded can be set according to the specific situation of the thread pool, so as to perform message aggregation according to the specific situation of the thread pool and improve the utilization rate of threads in the thread pool.
[0050] Furthermore, the aggregated message is split and consumed by threads in the thread pool, including:
[0051] It is determined by the threads in the thread pool whether the received message is an aggregate message. If so, the aggregate message is split and the split messages are consumed one by one.
[0052] From the above description, it can be seen that by aggregating messages and splitting aggregated messages, the usage of threads in the thread pool can be effectively reduced and the message consumption rate can be increased.
[0053] The method and system for message aggregation consumption described above in the present invention are suitable for effectively reducing the usage of threads in the thread pool by message aggregation, and can further improve the message consumption rate. The following is an explanation through specific implementation methods:
[0054] Embodiment 1
[0055] Please refer to Figure 1 , a method for message aggregation consumption, comprising the steps of:
[0056] S1. The client receives a message, records the type and data volume of the message, uses the type and data volume of the message as a key name, and stores the message as a value in a message queue.
[0057] Specifically, when the client receives a message, it needs to first parse out the message type in the message and obtain the actual size of the entire message. Then, it writes the message into the message queue as a key using the type + actual size method. At this time, the message list in the message queue can be sorted in ascending order according to the key.
[0058] S2. When the server consumes messages, it obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through the threads in the thread pool.
[0059] Specifically, when the server consumes messages, it obtains the message list from the message queue and performs the following operations:
[0060] S21. Set an initial value of the first threshold, obtain the thread occupancy ratio of the thread pool, and increase the first threshold according to the occupancy ratio; aggregate messages of the same type in the key name in order according to the message list, and the data size of the aggregated messages is less than or equal to the first threshold.
[0061] In this embodiment, the aggregation operation is performed only on messages of the same type, and the data volume of the aggregated message after aggregation cannot exceed the first threshold.
[0062] S22. Set an initial value of the second threshold and obtain the thread occupancy ratio of the thread pool, and raise the second threshold according to the occupancy ratio; determine whether the number of aggregated messages that are not currently sent to the thread pool reaches the second threshold, and if so, send all aggregated messages that are not sent to the thread pool to the thread pool.
[0063] In this embodiment, when the number of aggregated messages reaches a second threshold, for example, 10 aggregated messages are sent at one time. If 10 aggregated messages are not met, messages continue to be obtained from the message queue and aggregated until the number of aggregated messages reaches 10.
[0064] The aggregated messages are batch-requested to threads in the thread pool for subsequent task processing.
[0065] S23. Determine whether the received message is an aggregate message through the threads in the thread pool. If so, split the aggregate message and consume the split messages one by one.
[0066] Specifically, when a message is received in the thread pool, if it is a single message, the single message is directly processed; if it is an aggregated message, the message needs to be split first, and then the single message tasks are processed one by one.
[0067] Therefore, in this embodiment, the client sends the original message to the server, which is directly written into the real message queue. Subsequently, an independent thread reads messages in batches from the real message queue. According to the specific situation of the subsequent thread pool, the messages are aggregated by type and consumed again. The number and size of message groups can be dynamically and freely combined, and finally the message groups are sent to the real consumers.
[0068] Embodiment 2
[0069] Please refer to Figure 2 , a message aggregation consumption system, comprising:
[0070] The client is used to receive a message, record the type and data volume of the message, use the type and data volume of the message as a key name, and store the message as a value in a message queue. In a message list of the message queue, the messages are sorted in ascending order of the key name;
[0071] The server, when consuming messages, obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through the threads in the thread pool.
[0072] Furthermore, the messages of the same type in the key name are aggregated, including:
[0073] Setting an initial value of a first threshold, obtaining a thread occupancy ratio of a thread pool, and raising the first threshold according to the occupancy ratio;
[0074] Messages of the same type in the key name are aggregated in order according to the message list, and the data size of the aggregated messages is less than or equal to the first threshold.
[0075] Further, the aggregated message is sent to the thread pool, including:
[0076] Setting an initial value of the second threshold, obtaining a thread occupancy ratio of the thread pool, and raising the second threshold according to the occupancy ratio;
[0077] It is determined whether the number of aggregate messages that are not currently sent to the thread pool reaches the second threshold; if so, all aggregate messages that are not currently sent to the thread pool are sent to the thread pool.
[0078] Furthermore, the aggregated message is split and consumed by threads in the thread pool, including:
[0079] It is determined by the threads in the thread pool whether the received message is an aggregate message. If so, the aggregate message is split and the split messages are consumed one by one.
[0080] In summary, the present invention provides a method and system for message aggregation consumption. When the client receives a message, it records the type and data size of each message in advance, and writes the message queue according to the type and data size of the message; when the server consumes the message, it obtains the message list from the message queue, aggregates the messages by type, and then sends these aggregated messages to the thread pool. An aggregated message containing messages of the same type only needs one thread in the thread pool to process. In this way, after the corresponding aggregation is performed according to the message size, the IO consumption of subsequent message consumption can be reduced. At the same time, the message aggregation method can effectively reduce the usage of threads in the thread pool, and can further improve the message consumption rate.
[0081] The above descriptions are merely embodiments of the present invention and are not intended to limit the patent scope of the present invention. Any equivalent transformations made using the contents of the present invention's specification and drawings, or directly or indirectly applied in related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A method for message aggregation consumption, It is characterized in that Includes steps: The client receives the message, records the type and data volume of the message, uses the type and data volume of the message as a key name, and stores the message as a value in the message queue; When the server consumes messages, it obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through threads in the thread pool.
2. A method for message aggregation consumption according to claim 1, It is characterized in that The method uses the type and data volume of the message as a key name and stores the message as a value in a message queue, further comprising: In the message list of the message queue, the messages are sorted in ascending order of the key names.
3. A method for message aggregation consumption according to claim 2, It is characterized in that Aggregate messages of the same type in the key name, including: Setting an initial value of a first threshold, obtaining a thread occupancy ratio of a thread pool, and raising the first threshold according to the occupancy ratio; Messages of the same type in the key name are aggregated in order according to the message list, and the data volume of the aggregated messages is less than or equal to the first threshold.
4. A method for message aggregation consumption according to claim 1, It is characterized in that Send aggregate messages to the thread pool, including: Setting an initial value of the second threshold, obtaining a thread occupancy ratio of the thread pool, and raising the second threshold according to the occupancy ratio; It is determined whether the number of aggregate messages that are not currently sent to the thread pool reaches the second threshold; if so, all aggregate messages that are not currently sent to the thread pool are sent to the thread pool.
5. A method for message aggregation consumption according to claim 1, It is characterized in that The aggregate message is split and consumed by threads in the thread pool, including: It is determined by the threads in the thread pool whether the received message is an aggregate message. If so, the aggregate message is split and the split messages are consumed one by one.
6. A message aggregation consumption system, It is characterized in that include: The client is used to receive the message, record the type and data volume of the message, use the type and data volume of the message as a key name, and store the message as a value in the message queue; The server, when consuming messages, obtains a message list from the message queue, aggregates messages of the same type in the key name, sends the aggregated messages to the thread pool, and splits and consumes the aggregated messages through the threads in the thread pool.
7. A message aggregation consumption system according to claim 6, It is characterized in that The method uses the type and data volume of the message as a key name and stores the message as a value in a message queue, further comprising: In the message list of the message queue, the messages are sorted in ascending order of the key names.
8. A message aggregation consumption system according to claim 7, It is characterized in that Aggregate messages of the same type in the key name, including: Setting an initial value of a first threshold, obtaining a thread occupancy ratio of a thread pool, and raising the first threshold according to the occupancy ratio; Messages of the same type in the key name are aggregated in order according to the message list, and the data volume of the aggregated messages is less than or equal to the first threshold.
9. A message aggregation consumption system according to claim 6, It is characterized in that Send aggregate messages to the thread pool, including: Setting an initial value of the second threshold, obtaining a thread occupancy ratio of the thread pool, and raising the second threshold according to the occupancy ratio; It is determined whether the number of aggregate messages that are not currently sent to the thread pool reaches the second threshold; if so, all aggregate messages that are not currently sent to the thread pool are sent to the thread pool.
10. A message aggregation consumption system according to claim 6, It is characterized in that The aggregate message is split and consumed by threads in the thread pool, including: It is determined by the threads in the thread pool whether the received message is an aggregate message. If so, the aggregate message is split and the split messages are consumed one by one.
Citation Information
Patent Citations
Transaction message processing method, system and related device
CN111562995A
Data retransmission processing method and device, computer equipment and storage medium
CN113037440A
Message consumption method based on set, production method, system and storage medium
CN113742099A
Flow control method, device and equipment, readable storage medium and program product
CN116962304A
Method and device for aggregating map layer resources based on back end
CN118118554A
