Message processing method and device based on message queue, equipment and medium
By introducing dual message queues and independent locking mechanisms in the multi-producer and multi-consumer model, combined with queue switching logic, the performance bottlenecks and scalability problems of traditional designs in high concurrency environments are solved, and efficient and scalable message processing is achieved.
Patent Information
- Application Number
- CN202411549530.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-01
- Publication Date
- 2025-02-25
AI Technical Summary
The traditional multi-producer multi-consumer model has performance bottlenecks, limited scalability and unfair problems in high concurrency environments, resulting in poor performance and poor scalability in handling high-traffic data interactions.
A dual message queue design is adopted, where one message queue is dedicated to producers and the other is dedicated to consumers, and each queue is equipped with an independent lock mechanism, which automatically swaps roles when the consumer queue is empty through queue swap logic, reducing lock competition and waiting time.
It significantly reduces lock competition, improves the system throughput and scalability, enhances the balance of resource utilization, and solves the performance and scalability problems of traditional designs in high concurrency environments.
Smart Images

Figure CN119512784A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a message processing method, device, equipment and medium based on a message queue. Background Art
[0002] With the rapid development of Internet technology, the amount of data interaction between the server and the client has shown an explosive growth trend. Especially in high-traffic application scenarios such as e-commerce, social networks, and online games, the demand for data exchange has reached an unprecedented level. In this context, an efficient and stable message transmission mechanism has become a core element in system architecture design. The data processing capability in a high-concurrency environment is directly related to the smoothness of the user experience and the overall response speed of the system. Therefore, how to improve the efficiency of message transmission while ensuring data consistency and integrity has become a technical challenge that needs to be solved urgently.
[0003] In the existing technical framework, a widely used message queue processing mechanism is the "Multi-Producer Multi-Consumer" (MPMC) model. This model uses a shared message queue as an intermediary, allowing multiple producer threads to add messages to the queue, and multiple consumer threads to take out and process messages from the queue. In order to ensure thread safety, that is, to prevent data competition and consistency problems during concurrent access, a lock mechanism (such as a mutex lock) is usually used to synchronize message access to the queue. The original intention of this design is to balance the load and improve the system's throughput and processing power, but it still has the following defects:
[0004] Performance bottleneck: The fierce competition among multiple threads for the same lock has become a major bottleneck of system performance. Each time a thread acquires and releases a lock, a context switch is required, which not only consumes CPU resources but also introduces additional latency. When the concurrency level is very high, frequent lock contention will lead to serious performance loss and even cause the so-called "lock starvation" phenomenon, causing some threads to wait for a long time and fail to execute.
[0005] Limited scalability: The centralized control of locks limits the horizontal scalability of the system. As the number of producers and consumers increases, the competition for locks becomes more intense, and the scalability of the system is severely restricted. It is difficult to linearly increase processing power by simply increasing hardware resources.
[0006] Unfairness problem: In some implementations, the allocation of locks may not be fair enough, causing some threads to wait for a long time while other threads can frequently get execution opportunities, which further aggravates the imbalance and inefficiency of the system.
[0007] In summary, although the traditional multi-producer and multi-consumer model combined with the locking mechanism has solved the problem of concurrent access to a certain extent, its inherent performance loss and expansion limitations make it incapable of meeting the needs of high-concurrency data interaction. There is an urgent need for more efficient and low-latency concurrency control strategies and technical solutions. Summary of the invention
[0008] The purpose of the present application is to provide a message processing method, device, electronic device and storage medium based on a message queue to solve at least one problem existing in the above-mentioned prior art.
[0009] In a first aspect, an embodiment of the present application provides a message processing method based on a message queue, comprising:
[0010] Two message queues are created in advance, which are respectively recorded as a first message queue and a second message queue, wherein one of the first message queue and the second message queue is used as a producer queue, and the other is used as a consumer queue;
[0011] Control the producer thread to operate the producer queue according to the producer rules;
[0012] Control the consumer thread to operate the consumer queue according to consumer rules.
[0013] In a possible implementation, the producer rule includes: acquiring a lock of a producer queue, adding a message to the producer queue, and releasing the lock of the producer queue after the operation is completed.
[0014] In a possible implementation, the consumer rule includes: checking whether the consumer queue is empty, determining whether to execute the queue exchange logic according to the check result, acquiring the lock of the consumer queue, taking a message from the current consumer queue for processing, and releasing the lock of the consumer queue after processing the message;
[0015] The queue exchange logic is: replacing the consumer queue from the first message queue to the second message queue, or replacing the consumer queue from the second message queue to the first message queue.
[0016] In a possible implementation, the step of determining whether to execute queue switching logic according to the inspection result includes:
[0017] If the consumer queue is empty, the queue exchange logic is executed. After executing the queue exchange logic, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0018] If the consumer queue is not empty, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0019] In a second aspect, an embodiment of the present application provides a message processing device based on a message queue, comprising:
[0020] A creation module, used for pre-creating two message queues, which are respectively recorded as a first message queue and a second message queue, wherein one of the first message queue and the second message queue is used as a producer queue, and the other is used as a consumer queue;
[0021] The control module is used to control the producer thread to operate the producer queue according to the producer rules; and control the consumer thread to operate the consumer queue according to the consumer rules.
[0022] In a possible implementation, the producer rule includes: acquiring a lock of a producer queue, adding a message to the producer queue, and releasing the lock of the producer queue after the operation is completed.
[0023] In a possible implementation, the consumer rule includes: checking whether the consumer queue is empty, determining whether to execute the queue exchange logic according to the check result, acquiring the lock of the consumer queue, taking a message from the current consumer queue for processing, and releasing the lock of the consumer queue after processing the message;
[0024] The queue exchange logic is: replacing the consumer queue from the first message queue to the second message queue, or replacing the consumer queue from the second message queue to the first message queue.
[0025] In a possible implementation, the consumer rules specifically include:
[0026] If the consumer queue is empty, the queue exchange logic is executed. After executing the queue exchange logic, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0027] If the consumer queue is not empty, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0028] In a third aspect, the present application provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method described in the first aspect.
[0029] In a fourth aspect, the present application provides a computer-readable storage medium having computer-readable instructions stored thereon, wherein the computer-readable instructions can be executed by a processor to implement the method described in the first aspect.
[0030] Compared with the prior art, the message processing method based on message queue provided by the present application pre-creates two message queues, which are respectively recorded as the first message queue and the second message queue. One of the first message queue and the second message queue is used as the producer queue, and the other is used as the consumer queue; the producer thread is controlled to operate the producer queue according to the producer rules; the consumer thread is controlled to operate the consumer queue according to the consumer rules. Compared with the prior art, the present application pre-creates two independent message queues, one dedicated to producers and the other dedicated to consumers. Unlike the traditional single queue, the two message queues of the present application can be equipped with their own independent lock mechanism, which means that the lock operations of producers and consumers will be completely decoupled, which can significantly reduce the performance loss caused by lock competition, thereby realizing high-performance thread-safe queues, meeting the stringent requirements of modern large-scale distributed systems for data processing speed and concurrency. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0032] Figure 1 A flowchart of a message processing method based on a message queue provided in an embodiment of the present application is shown;
[0033] Figure 2 A schematic diagram of a message processing device based on a message queue provided in an embodiment of the present application is shown;
[0034] Figure 3 A schematic diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0035] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.
[0036] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in this application should have the common meanings understood by technicians in the field to which this application belongs.
[0037] In addition, the terms "first" and "second" etc. are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units that are inherent to these processes, methods, products or devices.
[0038] This application aims to fundamentally optimize the message delivery efficiency in multi-producer and multi-consumer scenarios, reduce the performance loss caused by inter-thread lock competition through innovative design ideas, and implement high-performance thread-safe queues, thereby meeting the stringent requirements of modern large-scale distributed systems for data processing speed and concurrency. The queue workflow is as follows:
[0039] Producer operation: The producer thread only needs to obtain the lock of the producer queue to quickly add the message to the queue and release the lock immediately after the operation is completed. The entire process does not need to wait for consumer operations, thus achieving an efficient production process.
[0040] Consumer operation: Similarly, the consumer thread only focuses on the consumer queue and consumes messages after acquiring its lock. If the current consumer queue is empty, the empty waiting strategy is not adopted, but the current producer queue and consumer queue are swapped through an efficient queue exchange mechanism. In this way, the original producer queue (which has been filled with messages) is immediately transformed into a new consumer queue and can be consumed immediately, avoiding idle resources caused by temporary idleness of the queue.
[0041] The advantages and performance improvements of the queue workflow of this application are as follows:
[0042] Significantly reduce lock contention: Since producers and consumers operate independent queues and locks, lock contention is almost eliminated, greatly improving the overall operating efficiency of the system;
[0043] Improve throughput: reduce thread waiting time and increase the number of messages processed per unit time, i.e. system throughput;
[0044] Enhanced scalability: The design is easy to expand and can further disperse the lock pressure by increasing the number of queues to adapt to higher concurrency requirements;
[0045] Balanced resource utilization: The queue exchange mechanism effectively utilizes the data generated by the producer, reduces the idle waiting time, and enables a more balanced allocation and utilization of system resources.
[0046] The following is a detailed introduction to this application.
[0047] Please refer to Figure 1 , Figure 1 1 is a flowchart of a message processing method based on a message queue provided in an embodiment of the present application. The method can be applied to a message processing system. The method includes the following steps S101 to S103:
[0048] S101, pre-create two message queues, which are respectively recorded as a first message queue and a second message queue, wherein one of the first message queue and the second message queue is used as a producer queue, and the other is used as a consumer queue;
[0049] The above step S101 is the initialization stage of the embodiment of the present application, specifically:
[0050] 1.1. Create queues: In the initialization phase, first create two empty first-in-first-out (FIFO) queues, named producer queue P and consumer queue C. These two queues will be used to store messages generated by producers and messages read by consumers, respectively.
[0051] 1.2. Allocation lock: Configure an independent lock for each queue, that is, the producer queue P corresponds to the lock LP, and the consumer queue C corresponds to the lock LC. The existence of the lock is to ensure that the operation of the queue in a multi-threaded environment is thread-safe, and to prevent data inconsistency caused by multiple threads modifying the queue at the same time.
[0052] The above-mentioned dual queue structure and independent lock mechanism of the present application achieves a high degree of decoupling of producer and consumer operations by splitting the traditional single queue into two independent queues (producer queue and consumer queue) and equipping each queue with a separate lock. This architectural innovation effectively reduces the performance loss caused by lock contention between threads.
[0053] S102, controlling the producer thread to operate the producer queue according to the producer rules;
[0054] The producer rules include: acquiring the lock of the producer queue, adding the message to the producer queue, and releasing the lock of the producer queue after completing the operation.
[0055] The above step S102 is the producer thread execution process of the embodiment of the present application, which is specifically:
[0056] 2.1. Get lock: When a producer needs to add a message to the queue, it must first get the lock LP. This is to ensure that no other producers or consumers interfere with the state of the queue while the producer is writing to the queue.
[0057] 2.2. Add message: The producer adds a message to the producer queue P. This operation may include steps such as creating a message, encapsulating data, and inserting it to the end of the queue.
[0058] 2.3. Release the lock: After completing the message addition, the producer immediately releases the lock LP to allow other threads (such as other producers or consumers) to access the queue.
[0059] S103. Control the consumer thread to operate the consumer queue according to the consumer rules.
[0060] The consumer rule includes: checking whether the consumer queue is empty, determining whether to execute the queue exchange logic according to the check result, acquiring the lock of the consumer queue, taking a message from the current consumer queue for processing, and releasing the lock of the consumer queue after processing the message;
[0061] The queue exchange logic is: replacing the consumer queue from the first message queue to the second message queue, or replacing the consumer queue from the second message queue to the first message queue.
[0062] The step of determining whether to execute the queue switching logic according to the inspection result specifically includes:
[0063] If the consumer queue is empty, the queue exchange logic is executed. After executing the queue exchange logic, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0064] If the consumer queue is not empty, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0065] The above-mentioned dynamic queue exchange mechanism of the present application can automatically and efficiently exchange the roles of the producer queue and the consumer queue when the consumer queue is empty. This process does not require pausing the producer thread, ensuring the continuity of message processing and the maximum utilization of resources. This mechanism cleverly solves the potential waiting problem in multi-consumer scenarios and is the key to improving system response speed and throughput.
[0066] This application also avoids the performance bottleneck caused by traditional global locks by performing fine-grained lock control on queue operations, can accurately control the timing of lock acquisition and release, and ensure efficient coordination of lock operations and data access.
[0067] The above step S103 is the consumer thread execution process of the embodiment of the present application, which is specifically:
[0068] 3.1. Check and acquire lock: The consumer first checks whether the consumer queue C is empty. If the C queue is empty, the consumer will execute the queue exchange logic. If the C queue is not empty, the consumer acquires the lock LC.
[0069] 3.2. Queue exchange logic: If the C queue is empty, the consumer will execute the queue exchange logic, which includes swapping the roles of the producer queue P and the consumer queue C, and swapping the locks LP and LC accordingly. In this way, the original producer queue now becomes the new consumer queue, and the original consumer queue becomes the new producer queue.
[0070] 3.3. Consuming messages: Once the lock LC is acquired, the consumer can take a message from the current consumer queue (which may be the queue after the exchange) for processing. The processing process may involve parsing the message, executing business logic, updating the database status, etc.
[0071] 3.4. Release the lock: After processing the message, the consumer releases the lock LC so that other consumers or producers can continue to use the queue.
[0072] Continuous operation: Steps S102 and S103 are continuously repeated, so that producers can continuously add messages to the queue, and consumers can take messages from the queue and process them. This loop execution mechanism ensures high throughput and low latency response of the system while maintaining good resource utilization.
[0073] The message processing method based on message queues provided in the embodiments of the present application can achieve a variety of technical effects by using a producer-consumer model with dual queues and lock exchange, which together improve the performance, reliability and scalability of the system. The following is an analysis of the specific technical effects:
[0074] 1. Improve concurrent performance:
[0075] Producers and consumers can work in parallel without waiting for each other, which greatly improves the concurrent processing capability of the system. Producers can focus on generating data, while consumers focus on processing data, and the two will not waste CPU time due to synchronous waiting.
[0076] 2. Ensure thread safety:
[0077] Each queue has an independent lock, which ensures the atomicity of queue operations in a multi-threaded environment, avoiding data contention and inconsistency issues. The use of locks ensures that only one thread can access the queue at a time, avoiding errors caused by simultaneous reading and writing.
[0078] 3. Dynamic load balancing:
[0079] The queue switching logic can dynamically adjust the load of producers and consumers. When the consumer queue C is empty, the consumer thread will automatically switch to the producer queue P for consumption, which helps to balance the task distribution in the system and avoid the situation where the consumer thread is idle.
[0080] 4. Reduce context switching overhead:
[0081] By using queue exchange instead of the traditional producer-wait-consumer or consumer-wait-producer design, unnecessary thread blocking and context switching times can be reduced, thereby reducing system overhead and improving efficiency.
[0082] 5. Smoothing peak loads:
[0083] The dual queues act as a buffer that can temporarily store data when producers generate data faster than consumers can process it, and vice versa. This mechanism helps smooth out peak loads on the system and ensures that the system remains stable even under high load.
[0084] 6. Easy to expand and maintain:
[0085] Since producers and consumers are decoupled, more producer or consumer threads can be easily added as needed to accommodate greater data throughput. At the same time, this design also simplifies system maintenance and fault recovery, because the failure of a single component will not affect the integrity of the overall architecture.
[0086] 7. Flexible message processing strategy:
[0087] The dual queue pattern allows different consumers to process messages of different types or priorities. For example, one consumer queue can be dedicated to processing high-priority messages, while another queue processes regular or low-priority messages.
[0088] In summary, the present application not only improves the concurrent processing capability and response speed of the system, but also enhances the flexibility and reliability of the system, enabling the system to more effectively cope with various workloads and complex scenarios.
[0089] In the above embodiment, a message processing method based on a message queue is provided. Correspondingly, the present application also provides a message processing device based on a message queue. The message processing device based on a message queue provided in the embodiment of the present application can implement the above message processing method based on a message queue. The message processing device based on a message queue can be implemented by software, hardware, or a combination of software and hardware. For example, the message processing device based on a message queue can include integrated or separate functional modules or units to perform the corresponding steps in the above methods. Please refer to Figure 2 As shown, the message queue-based message processing device 10 of the present application includes:
[0090] A creation module 101 is used to pre-create two message queues, which are respectively recorded as a first message queue and a second message queue, wherein one of the first message queue and the second message queue is used as a producer queue, and the other is used as a consumer queue;
[0091] The control module 102 is used to control the producer thread to operate the producer queue according to the producer rules; and control the consumer thread to operate the consumer queue according to the consumer rules.
[0092] In a possible implementation, the producer rule includes: acquiring a lock of a producer queue, adding a message to the producer queue, and releasing the lock of the producer queue after the operation is completed.
[0093] In a possible implementation, the consumer rule includes: checking whether the consumer queue is empty, determining whether to execute the queue exchange logic according to the check result, acquiring the lock of the consumer queue, taking a message from the current consumer queue for processing, and releasing the lock of the consumer queue after processing the message;
[0094] The queue exchange logic is: replacing the consumer queue from the first message queue to the second message queue, or replacing the consumer queue from the second message queue to the first message queue.
[0095] In a possible implementation, the consumer rules specifically include:
[0096] If the consumer queue is empty, the queue exchange logic is executed. After executing the queue exchange logic, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0097] If the consumer queue is not empty, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
[0098] The message processing device based on message queue provided in the embodiment of the present application and the message processing method based on message queue provided in the embodiment of the present application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented therein.
[0099] An embodiment of the present application also provides an electronic device corresponding to the method provided in the aforementioned embodiment, and the electronic device may be an electronic device for a client, such as a mobile phone, a laptop computer, a tablet computer, a desktop computer, etc., to execute the above-mentioned message queue-based message processing method.
[0100] Please refer to Figure 3 , which shows a schematic diagram of an electronic device provided by some embodiments of the present application. Figure 3As shown, the electronic device 20 includes: a processor 200, a memory 201, a bus 202 and a communication interface 203, and the processor 200, the communication interface 203 and the memory 201 are connected via the bus 202; the memory 201 stores a computer program that can be run on the processor 200, and when the processor 200 runs the computer program, it executes the message queue-based message processing method provided in any of the aforementioned embodiments of the present application.
[0101] The memory 201 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk memory. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 203 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.
[0102] The bus 202 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory 201 is used to store programs, and the processor 200 executes the programs after receiving the execution instruction. The message processing method based on the message queue disclosed in any implementation of the embodiment of the present application may be applied to the processor 200, or implemented by the processor 200.
[0103] The processor 200 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 200. The above processor 200 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a readily available programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application can be directly embodied as a hardware decoding processor to be executed, or the hardware and software modules in the decoding processor can be executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 201, and the processor 200 reads the information in the memory 201 and completes the steps of the above method in combination with its hardware.
[0104] The electronic device provided in the embodiment of the present application and the message queue-based message processing method provided in the embodiment of the present application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented therein.
[0105] An embodiment of the present application also provides a computer-readable storage medium corresponding to the message queue-based message processing method provided in the aforementioned embodiment, on which a computer program (i.e., a program product) is stored. When the computer program is run by a processor, it will execute the message queue-based message processing method provided in any of the aforementioned embodiments.
[0106] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical or magnetic storage media, which are not listed here one by one.
[0107] The computer-readable storage medium provided by the above-mentioned embodiments of the present application and the message queue-based message processing method provided by the embodiments of the present application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the application programs stored therein.
[0108] Finally, although this application is based on the dual-queue model, its design philosophy and methodology lay the foundation for possible future expansion (such as adding more queues to cope with higher concurrency requirements). The construction idea of this scalable framework, especially how to maintain the overall efficiency and stability of the system while increasing queues, is also an important part of this application.
[0109] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application, and they should all be included in the scope of the claims and specification of the present application.
Claims
1. A message processing method based on a message queue, characterized in that: include: Two message queues are created in advance, which are respectively recorded as a first message queue and a second message queue, wherein one of the first message queue and the second message queue is used as a producer queue, and the other is used as a consumer queue; Control the producer thread to operate the producer queue according to the producer rules; Control the consumer thread to operate the consumer queue according to consumer rules.
2. The method according to claim 1, characterized in that The producer rules include: acquiring the lock of the producer queue, adding the message to the producer queue, and releasing the lock of the producer queue after completing the operation.
3. The method according to claim 1, characterized in that The consumer rule includes: checking whether the consumer queue is empty, determining whether to execute the queue exchange logic according to the check result, acquiring the lock of the consumer queue, taking a message from the current consumer queue for processing, and releasing the lock of the consumer queue after processing the message; The queue exchange logic is: replacing the consumer queue from the first message queue to the second message queue, or replacing the consumer queue from the second message queue to the first message queue.
4. The method according to claim 3, characterized in that The step of determining whether to execute the queue switching logic according to the inspection result includes: If the consumer queue is empty, the queue exchange logic is executed. After executing the queue exchange logic, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed. If the consumer queue is not empty, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
5. A message processing device based on a message queue, characterized in that: include: A creation module, used for pre-creating two message queues, which are respectively recorded as a first message queue and a second message queue, wherein one of the first message queue and the second message queue is used as a producer queue, and the other is used as a consumer queue; The control module is used to control the producer thread to operate the producer queue according to the producer rules; and control the consumer thread to operate the consumer queue according to the consumer rules.
6. The device according to claim 5, characterized in that The producer rules include: acquiring the lock of the producer queue, adding the message to the producer queue, and releasing the lock of the producer queue after completing the operation.
7. The device according to claim 5, characterized in that The consumer rule includes: checking whether the consumer queue is empty, determining whether to execute the queue exchange logic according to the check result, acquiring the lock of the consumer queue, taking a message from the current consumer queue for processing, and releasing the lock of the consumer queue after processing the message; The queue exchange logic is: replacing the consumer queue from the first message queue to the second message queue, or replacing the consumer queue from the second message queue to the first message queue.
8. The device according to claim 7, characterized in that The consumer rules specifically include: If the consumer queue is empty, the queue exchange logic is executed. After executing the queue exchange logic, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed. If the consumer queue is not empty, the lock of the consumer queue is acquired, a message is taken out from the current consumer queue for processing, and the lock of the consumer queue is released after the message is processed.
9. An electronic device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 4 when executing the computer program.
10. A computer-readable storage medium, characterized in that: Computer-readable instructions are stored thereon, and the computer-readable instructions can be executed by a processor to implement the method according to any one of claims 1 to 4.
Citation Information
Cited By
Message processing method and system
CN120639569A
Message processing method and device
CN121858482A
Full-link decoupling asynchronous processing system based on double queues
CN122240170A