Data transceiving message processing method and device, computer equipment and storage medium
By introducing parallel frameworks and event processing threads into the message processing system, the encapsulated message events are solved, and the response and data loss caused by shared threads in the prior art are solved, thereby achieving more efficient message processing and stable interface response.
Patent Information
- Application Number
- CN202311132453.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-04
- Publication Date
- 2025-03-04
AI Technical Summary
In the prior art, data transmission and reception and interface processing are performed in one thread, resulting in slow or stuck interface response when the amount of communication data is large, and data loss may be caused.
By detecting the amount and quantity of data of the pending message, it is decided whether to encapsulate it as a message event, and the parallel framework uses independent event processing threads to avoid sharing with the interface processing threads.
It effectively improves the efficiency of message processing, avoids data loss, and prevents interface jamming.
Smart Images

Figure CN119557116A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of message processing, and in particular to a method, device, computer equipment and storage medium for processing data receiving and sending messages. Background Art
[0002] At present, in some message sending and receiving processing systems, the underlying communication uses Qt's own mqtt class QMqttClient. This communication method has disadvantages: data sending and receiving and the interface are in one thread, and data sending and receiving are single-threaded. When the amount of communication data is large, on the one hand, it causes the interface to respond slowly or even freeze, and on the other hand, it causes some data loss, affecting the implementation of business logic. Summary of the invention
[0003] Based on this, it is necessary to provide a data sending and receiving message processing method, device, computer equipment and storage medium to address the above technical problems.
[0004] A data sending and receiving message processing method, comprising:
[0005] Check whether there are pending messages;
[0006] When it is detected that there is the to-be-processed message, encapsulating the to-be-processed message as a message event;
[0007] Determine at least one thread other than the thread independent of the interface processing as an event processing thread;
[0008] The parallel framework processes the message event using the event processing thread.
[0009] In one embodiment, when the existence of the to-be-processed message is detected, the step of encapsulating the to-be-processed message into a message event includes:
[0010] When it is detected that there is the message to be processed, detecting whether the data volume of the message to be processed is greater than a preset data volume, and / or detecting whether the number of the messages to be processed is greater than a preset number;
[0011] When the data volume of the to-be-processed message is greater than the preset data volume or when the number of the to-be-processed messages is greater than the preset number, the to-be-processed message is encapsulated as a message event.
[0012] In one embodiment, the method further comprises:
[0013] When the data amount of the message to be processed is less than or equal to the preset data amount, the message to be processed is processed using a preset thread.
[0014] In one embodiment, the method further comprises:
[0015] When the number of the pending messages is less than or equal to the preset number, after a preset time, the number of the pending messages is detected again to see whether it is greater than the preset number. When the number of the pending messages is less than or equal to the preset number, the pending messages are processed using a preset thread; when the number of the pending messages is greater than the preset number, the pending messages are encapsulated as message events.
[0016] In one embodiment, the step of detecting whether there is a message to be processed includes:
[0017] Call the preset function to transfer the message processing process to the background;
[0018] Check in the background whether there are pending messages.
[0019] In one of the embodiments, the to-be-processed messages include to-be-received messages and to-be-sent messages;
[0020] The step of processing the message event by the parallel framework using the event processing thread includes:
[0021] The parallel framework processes the message event using the event processing thread, receives the to-be-received message, and / or sends the to-be-sent message.
[0022] A data transmission and reception message processing device, comprising:
[0023] A pending message detection module is used to detect whether there is a pending message;
[0024] A message encapsulation module, used for encapsulating the to-be-processed message into a message event when detecting the existence of the to-be-processed message;
[0025] A thread determination module, used to determine a thread other than a thread independent of the interface processing as an event processing thread;
[0026] The message processing module is used for the parallel framework to process the message event using the event processing thread.
[0027] In one embodiment, the message encapsulation module includes:
[0028] a message detection unit, configured to, when detecting the existence of the to-be-processed message, detect whether the data volume of the to-be-processed message is greater than a preset data volume, and / or detect whether the number of the to-be-processed messages is greater than a preset number;
[0029] The message encapsulation unit is used to encapsulate the to-be-processed message into a message event when the data volume of the to-be-processed message is greater than the preset data volume or when the number of the to-be-processed messages is greater than the preset number.
[0030] A computer device comprises a memory and a processor, wherein the memory stores a computer program, and wherein the processor implements the following steps when executing the computer program:
[0031] Check whether there are pending messages;
[0032] When it is detected that there is the to-be-processed message, encapsulating the to-be-processed message as a message event;
[0033] Determine at least one thread other than the thread independent of the interface processing as an event processing thread;
[0034] The parallel framework processes the message event using the event processing thread.
[0035] A computer-readable storage medium stores a computer program, which, when executed by a processor, implements the following steps:
[0036] Check whether there are pending messages;
[0037] When it is detected that there is the to-be-processed message, encapsulating the to-be-processed message as a message event;
[0038] Determine at least one thread other than the thread independent of the interface processing as an event processing thread;
[0039] The parallel framework processes the message event using the event processing thread.
[0040] The above-mentioned data sending and receiving message processing method, device, computer equipment and storage medium, when there are messages to be processed, use an event processing thread other than the interface processing thread. In this way, the interface processing and message processing can be processed by different threads respectively, thereby improving the efficiency of message processing, avoiding data loss, and avoiding interface freezing. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Figure 1 A schematic diagram of an application scenario of a method for processing data sending and receiving messages in an embodiment;
[0042] Figure 2 A schematic diagram of a flow chart of a method for processing data sending and receiving messages in one embodiment;
[0043] Figure 3 It is a structural block diagram of a data transmission and reception message processing device in one embodiment;
[0044] Figure 4 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0045] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0046] Embodiment 1
[0047] The data transmission and reception message processing method provided in this application can be applied to Figure 1 In the application environment shown. The computer 102 communicates with the server 104 through the network. The computer 102 may be, but is not limited to, various personal computers, servers, laptops, smart phones, tablet computers, and portable wearable devices, and the server 104 may be implemented as an independent server or a server cluster consisting of multiple servers. The server 104 sends a message to the computer 102, or receives a message from the computer 102. When the server 104 has a message to be sent or a message to be received, it is determined that there is a message to be processed.
[0048] The server 104 detects in real time whether there are pending messages; when it is detected that there are pending messages, the pending messages are encapsulated as message events; at least one thread other than the thread independent of the interface processing is determined as an event processing thread; and the parallel framework uses the event processing thread to process the message event.
[0049] Embodiment 2
[0050] In this embodiment, Figure 2 As shown, a method for processing data sending and receiving messages is provided, which includes:
[0051] Step 210: Check whether there is a message to be processed.
[0052] In this embodiment, the message refers to a message of the MQTT (Message Queuing Telemetry Transport) class. The pending messages include pending messages and pending messages. The pending messages are messages that need to be sent and are ready to be sent, and the pending messages are messages that are ready to be received. In this embodiment, the message queue is detected in real time to detect whether there are messages that need to be sent or messages that are ready to be received in the message queue.
[0053] Step 220: When it is detected that there is the message to be processed, encapsulate the message to be processed into a message event.
[0054] In this embodiment, when it is detected that there are pending messages, each pending message is encapsulated separately and encapsulated into an independent event, which is a message event. The pending messages are encapsulated into a message event, which is a structure capable of storing and transmitting data.
[0055] Step 230 , determining at least one thread other than the thread independent of the interface processing as an event processing thread.
[0056] In this embodiment, the interface processing thread is a thread for processing interface interaction information and interface display information, and at least one thread independent of the interface processing thread refers to a thread other than the interface processing thread. In this embodiment, one thread other than the multiple interface processing threads is selected as the event processing thread, and the event processing thread is used to specifically process message events.
[0057] Step 240: The parallel framework processes the message event using the event processing thread.
[0058] In this embodiment, the message event is sent to the parallel framework for processing, and the parallel framework uses the event processing thread to process the message event. Since the event processing thread is independent of the interface processing thread, the processing efficiency and speed of the message event can be effectively improved. In addition, the parallel framework uniformly schedules the parsing data, which greatly improves the efficiency and speed of message processing.
[0059] In the above embodiment, when there are messages to be processed, an event processing thread other than the interface processing thread is used. In this way, the interface processing and message processing can be processed by different threads respectively, which improves the efficiency of message processing, avoids data loss, and avoids interface freezing.
[0060] In one embodiment, when the existence of the message to be processed is detected, the step of encapsulating the message to be processed as a message event includes: when the existence of the message to be processed is detected, detecting whether the data volume of the message to be processed is greater than the preset data volume, and / or detecting whether the number of the messages to be processed is greater than the preset number; when the data volume of the message to be processed is greater than the preset data volume or when the number of the messages to be processed is greater than the preset number, encapsulating the message to be processed as a message event.
[0061] In one embodiment, when the existence of the message to be processed is detected, it is detected whether the data volume of the message to be processed is greater than the preset data volume; when the data volume of the message to be processed is greater than the preset data volume, the message to be processed is encapsulated as a message event.
[0062] In one embodiment, when the existence of the to-be-processed messages is detected, it is detected whether the number of the to-be-processed messages is greater than a preset number; when the number of the to-be-processed messages is greater than the preset number, the to-be-processed messages are encapsulated as message events.
[0063] In this embodiment, when it is detected that there are messages to be processed, the total data volume of the messages to be processed is detected, or the total number of messages to be processed is detected. When the total data volume of the messages to be processed is greater than the preset data volume, or when the total number of messages to be processed is large, it indicates that the data volume of the current messages to be processed is large or the number of messages is large. If the traditional method of processing the same thread as the interface processing is adopted, the interface will be stuck. Therefore, in this embodiment, when the data volume of the message to be processed is greater than the preset data volume or when the number of the messages to be processed is greater than the preset number, the message to be processed is encapsulated as a message event, and the message event is handed over to the parallel framework, and the parallel framework uses the event processing thread to process the message event. In this way, when the concurrent message data volume is large or the data is large, the parallel framework can be used to uniformly schedule and parse the data, which greatly improves the efficiency and speed of message processing.
[0064] In one embodiment, the method further includes: when the data volume of the message to be processed is less than or equal to the preset data volume, processing the message to be processed using a preset thread.
[0065] In this embodiment, the preset thread is a thread pre-assigned to message processing. In this embodiment, when the data volume of the message to be processed is small, processing the message to be processed will not occupy more resources, and will not cause much impact on the interface interaction and interface display. Therefore, the original preset thread can be used to process the message to be processed. In one embodiment, the preset thread is the same process as the thread of interface processing, that is, the thread of interface processing is the thread of interface processing. Since the amount of data contained in the message to be processed is small, the resources occupied by processing the message are small, and the same thread as the interface processing can be directly used for processing. In this way, the thread of interface processing can be less affected, avoiding interface jamming.
[0066] In one embodiment, the method further comprises:
[0067] When the number of the pending messages is less than or equal to the preset number, after a preset time, the number of the pending messages is detected again to see whether it is greater than the preset number. When the number of the pending messages is less than or equal to the preset number, the pending messages are processed using a preset thread; when the number of the pending messages is greater than the preset number, the pending messages are encapsulated as message events.
[0068] In this embodiment, when the number of pending messages is less than or equal to the preset number, a strategy different from that when the data volume of the pending messages is less than or equal to the preset data volume is adopted. Specifically, when the number of pending messages is less than or equal to the preset number, it indicates that the number (number of messages) of pending messages is small. Therefore, in order to avoid affecting the process of interface interaction, first wait for a period of time, that is, wait for the preset time. After the preset time, the number of pending messages is detected again. When the number of pending messages is still less than or equal to the preset number, it indicates that the pending messages are still small. Therefore, it is beneficial for the preset thread to process the pending messages to improve the processing efficiency. If the number of pending messages increases after the preset time, and increases to more than the preset number, the pending messages are encapsulated as message events and the message events are handed over to the parallel framework, and the parallel framework uses the event processing thread to process the message events. In this way, the parallel processing efficiency of a large number of messages can be improved to avoid affecting the interface interaction. It should be understood that the preset time can be set to be shorter, so that the pending messages can be prevented from being overly delayed. Through the above process, when the number of pending messages is small, the preset thread can be used to process the pending messages. When the number of pending messages accumulates to a large number, they are uniformly processed by the parallel framework through the event processing thread. Without affecting the timeliness of message processing, it can not only improve the processing efficiency, but also reduce the impact on interface interaction, thereby improving the flexibility of message processing.
[0069] In one embodiment, when the number of pending messages is less than or equal to the preset number, the resource consumption level of the interface interaction thread is detected, and the preset time is determined according to the resource consumption level of the interface interaction thread. After the preset time, it is detected again whether the number of pending messages is greater than the preset number. When the number of pending messages is less than or equal to the preset number, the pending messages are processed using the preset thread; when the number of pending messages is greater than the preset number, the pending messages are encapsulated as message events.
[0070] In this embodiment, the resource consumption degree of the thread of interface interaction is used to indicate the degree of various computer resources such as CPU (Central Processing Unit), memory, cache, etc. occupied by the thread of interface interaction. If the resource consumption degree of the thread of interface interaction is high, it means that the interface interaction occupies more computer resources, and if the resource consumption degree of the thread of interface interaction is low, it means that the interface interaction occupies less computer resources. The preset time is determined according to the resource consumption degree of the thread of interface interaction. The preset time and the resource consumption degree of the thread of interface interaction are positively correlated functionally. When the resource consumption degree of the thread of interface interaction is high, the corresponding preset time is longer, so that the number of pending messages can be detected again in as long a time as possible. If more pending messages are accumulated in a longer time, the accumulated pending messages are processed by threads other than the interface interaction to avoid affecting the thread of the interactive interface. If there are no more pending messages accumulated in a longer time, the preset thread can be directly used for processing. On the contrary, when the resource consumption of the thread of interface interaction is low, the corresponding preset time is shorter. In this way, by detecting the number of pending messages again after a shorter time interval, more pending messages can be accumulated, and the accumulated pending messages can be processed by threads other than interface interaction. It should be understood that the longer the preset time, the more pending messages are accumulated under the same circumstances, the easier it is to trigger the use of threads other than interface interaction to process the accumulated pending messages, and the smaller the probability of affecting the thread of interface interaction. Therefore, by setting the relationship between the preset time and the resource consumption of the thread of interface interaction as a positive correlation function relationship, the impact on threads other than interface interaction can be further reduced, thereby further avoiding the interface from being stuck.
[0071] In one embodiment, the step of detecting whether there is a message to be processed includes:
[0072] Call the preset function to transfer the message processing process to the background;
[0073] Check in the background whether there are pending messages.
[0074] In this embodiment, the preset function is used to transfer the process that needs to be transferred to the background to run into the background. In this embodiment, the preset function is used to transfer the process of message processing into the background. By calling this preset function, the process of message processing can be transferred to the background to avoid affecting the interaction and display of the current interface. Specifically, first create a class responsible for sending and receiving MQTT messages: admqttcommun, which is only responsible for communication. When this class is constructed, a mosquitto instance is created and initialized, and then the moveToThread function is called to make the message processing enter the background thread to avoid blocking the interface. In this embodiment, since the process of real-time detection of messages to be processed is transferred to the background, the occupation of resources can be effectively reduced, thereby avoiding too much impact on interface interaction and interface display.
[0075] In one embodiment, the messages to be processed include messages to be received and messages to be sent; the step of the parallel framework using the event processing thread to process the message event includes: the parallel framework using the event processing thread to process the message event, receiving the messages to be received and / or sending the messages to be sent.
[0076] In this embodiment, the messages to be processed include messages to be sent and messages to be received. In this embodiment, the parallel framework is used to process the messages to be processed using the event processing thread, perform data analysis, receive the messages to be received, and send the messages to be sent.
[0077] Embodiment 3
[0078] In this embodiment, a data transmission and reception message processing method is provided, including: (1) firstly creating a class specifically responsible for the transmission and reception of mqtt messages: admqttcommun, which is only responsible for communication. When the admqttcommun class is constructed, firstly a mosquitto instance is created and initialized, and then the moveToThread function is called to enter the background thread to avoid blocking the interface. (2) Real-time detection of mqtt messages. When sending a large number of mqtt messages, firstly encapsulate a large number of messages into events (structures used to store and transmit data), and then all the encapsulated events are handed over to the parallel framework, which schedules and finally sends the mqtt message, thereby improving the speed and efficiency of message sending. (3) When receiving an mqtt message, similarly encapsulate the received mqtt message into a corresponding event and hand it over to the parallel framework, which schedules and parses the data uniformly, thereby greatly improving the efficiency and speed of message processing. (4) Separating the data transmission and reception and message parsing processing services, which not only improves the reliability and robustness of the data transmission and reception part, but also improves the clarity of the program architecture, facilitating maintenance and expansion. The specific method is to write the data parsing function in the above data communication module as a virtual function, create a new business processing class adumtcommun inherited from the admqttcommun class, and rewrite the message parsing function in the business processing class. If you need to process another business, you only need to recreate another business processing class (the base class is the admqttcommun class), process the business in the new business processing class, and the communication class does not need to make any changes.
[0079] The following is part of the implementation code of the constructed Admqttcommun class and Adumtcommun class:
[0080] 1. Admqttcommun (data sending and receiving class)
[0081] / / Constructor
[0082] admqttCommun_Private::admqttCommun_Private(admqttCommun*adcomm,admqttCommun_PrivateData*ptr,QObject*p)
[0083] :QObject(p),d_data(ptr),m_admqttComm(adcomm)
[0084] {
[0085] / *Create a Mosquitto instance* /
[0086] d_data->m_worker.reset(new admqttcommunWorker(adcomm));
[0087] / * Initialize the Mosquitto instance * /
[0088] d_data->m_worker->init();
[0089] / *Put the Mosquitto instance in the background thread* /
[0090] d_data->m_worker->moveToThread(&d_data->workerThread);
[0091] d_data->workerThread.start();
[0092] }
[0093] 2. Adumtcommun (business processing class, the base class is Admqttcommun)
[0094] / / The base class of adumtCommun is admqttCommun
[0095] class ADUMTCOMMUN_EXPORT adumtCommun:public admqttCommun
[0096] {
[0097] Q_OBJECT
[0098] public:
[0099] Q_SIGNALS:
[0100] / / Business related
[0101] void sig_getStatusData(); / / / <Notify the client to get the node status data
[0102] void sig_getDeviceList(); / / / <Notify the client to get the node ID list
[0103] private:
[0104] adumtCommun(QObject*p=nullptr);
[0105] virtual~adumtCommun();
[0106] / **
[0107] *@brief parse the received mqtt message---re-message parsing function
[0108] *@param topic[topic]
[0109] *@param message[message]
[0110] * /
[0111] void messageParser(const QString&topic,const QByteArray&message)override;
[0112] private:
[0113] class PrivateData;
[0114] PrivateData*d_data=nullptr;
[0115] };
[0116] It should be understood that although Figure 2 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 2 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.
[0117] Embodiment 4
[0118] In this embodiment, Figure 3 As shown, a data transmission and reception message processing device is provided, comprising:
[0119] A pending message detection module 310 is used to detect whether there is a pending message;
[0120] The message encapsulation module 320 is used to encapsulate the to-be-processed message into a message event when detecting the existence of the to-be-processed message;
[0121] A thread determination module 330, used to determine a thread other than a thread independent of the interface processing as an event processing thread;
[0122] The message processing module 340 is used for the parallel framework to process the message event using the event processing thread.
[0123] In one embodiment, the message encapsulation module includes:
[0124] a message detection unit, configured to, when detecting the existence of the to-be-processed message, detect whether the data volume of the to-be-processed message is greater than a preset data volume, and / or detect whether the number of the to-be-processed messages is greater than a preset number;
[0125] The message encapsulation unit is used to encapsulate the to-be-processed message into a message event when the data volume of the to-be-processed message is greater than the preset data volume or when the number of the to-be-processed messages is greater than the preset number.
[0126] In one embodiment, the message encapsulation module further includes:
[0127] The preset thread processing unit is used to process the message to be processed by using a preset thread when the data amount of the message to be processed is less than or equal to the preset data amount.
[0128] In one embodiment, the message encapsulation module also includes: a delay processing unit, which is used to, when the number of the pending messages is less than or equal to the preset number, detect again whether the number of the pending messages is greater than the preset number after a preset time; when the number of the pending messages is less than or equal to the preset number, use a preset thread to process the pending messages; when the number of the pending messages is greater than the preset number, encapsulate the pending messages as message events.
[0129] In one embodiment, the pending message detection module includes:
[0130] The function calling unit is used to call the preset function and transfer the message processing process to the background;
[0131] The background detection unit is used to detect whether there are pending messages in the background.
[0132] In one embodiment, the to-be-processed messages include to-be-received messages and to-be-sent messages;
[0133] The message processing module is also used for the parallel framework to process the message event using the event processing thread, receive the to-be-received message and / or send the to-be-sent message.
[0134] For the specific definition of the data transceiver message processing device, please refer to the definition of the data transceiver message processing method above, which will not be repeated here. Each unit in the above-mentioned data transceiver message processing device can be implemented in whole or in part by software, hardware and a combination thereof. The above-mentioned units can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above-mentioned units.
[0135] Embodiment 5
[0136] In this embodiment, a computer device is provided. Its internal structure diagram can be shown as follows: Figure 4 As shown. The computer device includes a processor, a memory, a network interface, a display screen and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program, and the non-volatile storage medium is deployed with a database, which is used to store messages. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with other computer devices deployed with application software. When the computer program is executed by the processor, a method for processing data sending and receiving messages is implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covered on the display screen, or a key, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.
[0137] Those skilled in the art will understand that Figure 4 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0138] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0139] Step 210: Check whether there is a message to be processed.
[0140] In this embodiment, the message refers to a message of the MQTT (Message Queuing Telemetry Transport) class. The pending messages include pending messages and pending messages. The pending messages are messages that need to be sent and are ready to be sent, and the pending messages are messages that are ready to be received. In this embodiment, the message queue is detected in real time to detect whether there are messages that need to be sent or messages that are ready to be received in the message queue.
[0141] Step 220: When it is detected that there is the message to be processed, encapsulate the message to be processed into a message event.
[0142] In this embodiment, when it is detected that there are pending messages, each pending message is encapsulated separately and encapsulated into an independent event, which is a message event. The pending messages are encapsulated into a message event, which is a structure capable of storing and transmitting data.
[0143] Step 230 , determining at least one thread other than the thread independent of the interface processing as an event processing thread.
[0144] In this embodiment, the interface processing thread is a thread for processing interface interaction information and interface display information, and at least one thread independent of the interface processing thread refers to a thread other than the interface processing thread. In this embodiment, one thread other than the multiple interface processing threads is selected as the event processing thread, and the event processing thread is used to specifically process message events.
[0145] Step 240: The parallel framework processes the message event using the event processing thread.
[0146] In this embodiment, the message event is sent to the parallel framework for processing, and the parallel framework uses the event processing thread to process the message event. Since the event processing thread is independent of the interface processing thread, the processing efficiency and speed of the message event can be effectively improved. In addition, the parallel framework uniformly schedules the parsing data, which greatly improves the efficiency and speed of message processing.
[0147] In the above embodiment, when there are messages to be processed, an event processing thread other than the interface processing thread is used. In this way, the interface processing and message processing can be processed by different threads respectively, which improves the efficiency of message processing and avoids interface freezing.
[0148] In one embodiment, when the processor executes the computer program, the following steps are also implemented:
[0149] When the existence of the message to be processed is detected, whether the data volume of the message to be processed is greater than the preset data volume, and / or whether the number of the messages to be processed is greater than the preset number is detected; when the data volume of the message to be processed is greater than the preset data volume or when the number of the messages to be processed is greater than the preset number, the message to be processed is encapsulated as a message event.
[0150] In one embodiment, when the existence of the message to be processed is detected, it is detected whether the data volume of the message to be processed is greater than the preset data volume; when the data volume of the message to be processed is greater than the preset data volume, the message to be processed is encapsulated as a message event.
[0151] In one embodiment, when the existence of the to-be-processed messages is detected, it is detected whether the number of the to-be-processed messages is greater than a preset number; when the number of the to-be-processed messages is greater than the preset number, the to-be-processed messages are encapsulated as message events.
[0152] In this embodiment, when it is detected that there are messages to be processed, the total data volume of the messages to be processed is detected, or the total number of messages to be processed is detected. When the total data volume of the messages to be processed is greater than the preset data volume, or when the total number of messages to be processed is large, it indicates that the data volume of the current messages to be processed is large or the number of messages is large. If the traditional method of processing the same thread as the interface processing is adopted, the interface will be stuck. Therefore, in this embodiment, when the data volume of the message to be processed is greater than the preset data volume or when the number of the messages to be processed is greater than the preset number, the message to be processed is encapsulated as a message event, and the message event is handed over to the parallel framework, and the parallel framework uses the event processing thread to process the message event. In this way, when the concurrent message data volume is large or the data is large, the parallel framework can be used to uniformly schedule and parse the data, which greatly improves the efficiency and speed of message processing.
[0153] In one embodiment, when the processor executes the computer program, the following steps are also implemented:
[0154] When the data amount of the message to be processed is less than or equal to the preset data amount, the message to be processed is processed using a preset thread.
[0155] In this embodiment, the preset thread is a thread pre-assigned to message processing. In this embodiment, when the data volume of the message to be processed is small, processing the message to be processed will not occupy more resources, and will not cause much impact on the interface interaction and interface display. Therefore, the original preset thread can be used to process the message to be processed. In one embodiment, the preset thread is the same process as the thread of interface processing, that is, the thread of interface processing is the thread of interface processing. Since the amount of data contained in the message to be processed is small, the resources occupied by processing the message are small, and the same thread as the interface processing can be directly used for processing. In this way, the thread of interface processing can be less affected, avoiding interface jamming.
[0156] In one embodiment, when the processor executes the computer program, the following steps are also implemented:
[0157] When the number of the pending messages is less than or equal to the preset number, after a preset time, the number of the pending messages is detected again to see whether it is greater than the preset number. When the number of the pending messages is less than or equal to the preset number, the pending messages are processed using a preset thread; when the number of the pending messages is greater than the preset number, the pending messages are encapsulated as message events.
[0158] In this embodiment, when the number of pending messages is less than or equal to the preset number, a strategy different from that when the data volume of the pending messages is less than or equal to the preset data volume is adopted. Specifically, when the number of pending messages is less than or equal to the preset number, it indicates that the number (number of messages) of pending messages is small. Therefore, in order to avoid affecting the process of interface interaction, first wait for a period of time, that is, wait for the preset time. After the preset time, the number of pending messages is detected again. When the number of pending messages is still less than or equal to the preset number, it indicates that the pending messages are still small. Therefore, it is beneficial for the preset thread to process the pending messages to improve the processing efficiency. If the number of pending messages increases after the preset time, and increases to more than the preset number, the pending messages are encapsulated as message events and the message events are handed over to the parallel framework, and the parallel framework uses the event processing thread to process the message events. In this way, the parallel processing efficiency of a large number of messages can be improved to avoid affecting the interface interaction. It should be understood that the preset time can be set to be shorter, so that the pending messages can be prevented from being overly delayed. Through the above process, when the number of pending messages is small, the preset thread can be used to process the pending messages. When the number of pending messages accumulates to a large number, they are uniformly processed by the parallel framework through the event processing thread. Without affecting the timeliness of message processing, it can not only improve the processing efficiency, but also reduce the impact on interface interaction, thereby improving the flexibility of message processing.
[0159] In one embodiment, when the processor executes the computer program, the following steps are also implemented:
[0160] When the number of pending messages is less than or equal to the preset number, the resource consumption level of the interface interaction thread is detected, and the preset time is determined according to the resource consumption level of the interface interaction thread. After the preset time, it is detected again whether the number of pending messages is greater than the preset number. When the number of pending messages is less than or equal to the preset number, the pending messages are processed using the preset thread; when the number of pending messages is greater than the preset number, the pending messages are encapsulated as message events.
[0161] In this embodiment, the resource consumption degree of the thread of interface interaction is used to indicate the degree of various computer resources such as CPU (Central Processing Unit), memory, cache, etc. occupied by the thread of interface interaction. If the resource consumption degree of the thread of interface interaction is high, it means that the interface interaction occupies more computer resources, and if the resource consumption degree of the thread of interface interaction is low, it means that the interface interaction occupies less computer resources. The preset time is determined according to the resource consumption degree of the thread of interface interaction. The preset time and the resource consumption degree of the thread of interface interaction are positively correlated functionally. When the resource consumption degree of the thread of interface interaction is high, the corresponding preset time is longer, so that the number of pending messages can be detected again in as long a time as possible. If more pending messages are accumulated in a longer time, the accumulated pending messages are processed by threads other than the interface interaction to avoid affecting the thread of the interactive interface. If there are no more pending messages accumulated in a longer time, the preset thread can be directly used for processing. On the contrary, when the resource consumption of the thread of interface interaction is low, the corresponding preset time is shorter. In this way, by detecting the number of pending messages again after a shorter time interval, more pending messages can be accumulated, and the accumulated pending messages can be processed by threads other than interface interaction. It should be understood that the longer the preset time, the more pending messages are accumulated under the same circumstances, the easier it is to trigger the use of threads other than interface interaction to process the accumulated pending messages, and the smaller the probability of affecting the thread of interface interaction. Therefore, by setting the relationship between the preset time and the resource consumption of the thread of interface interaction as a positive correlation function relationship, the impact on threads other than interface interaction can be further reduced, thereby further avoiding the interface from being stuck.
[0162] In one embodiment, when the processor executes the computer program, the following steps are also implemented:
[0163] Call the preset function to transfer the message processing process to the background;
[0164] Check in the background whether there are pending messages.
[0165] In this embodiment, the preset function is used to transfer the process that needs to be transferred to the background to run into the background. In this embodiment, the preset function is used to transfer the process of message processing into the background. By calling this preset function, the process of message processing can be transferred to the background to avoid affecting the interaction and display of the current interface. Specifically, first create a class responsible for sending and receiving MQTT messages: admqttcommun, which is only responsible for communication. When this class is constructed, a mosquitto instance is created and initialized, and then the moveToThread function is called to make the message processing enter the background thread to avoid blocking the interface. In this embodiment, since the process of real-time detection of messages to be processed is transferred to the background, the occupation of resources can be effectively reduced, thereby avoiding too much impact on interface interaction and interface display.
[0166] In one embodiment, the to-be-processed messages include to-be-received messages and to-be-sent messages;
[0167] When the processor executes the computer program, the following steps are also implemented: the step of the parallel framework using the event processing thread to process the message event includes: the parallel framework using the event processing thread to process the message event, receiving the message to be received and / or sending the message to be sent.
[0168] In this embodiment, the messages to be processed include messages to be sent and messages to be received. In this embodiment, the parallel framework is used to process the messages to be processed using the event processing thread, perform data analysis, receive the messages to be received, and send the messages to be sent.
[0169] Embodiment 6
[0170] In this embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented:
[0171] Step 210: Check whether there is a message to be processed.
[0172] In this embodiment, the message refers to a message of the MQTT (Message Queuing Telemetry Transport) class. The pending messages include pending messages and pending messages. The pending messages are messages that need to be sent and are ready to be sent, and the pending messages are messages that are ready to be received. In this embodiment, the message queue is detected in real time to detect whether there are messages that need to be sent or messages that are ready to be received in the message queue.
[0173] Step 220: When it is detected that there is the message to be processed, encapsulate the message to be processed into a message event.
[0174] In this embodiment, when it is detected that there are pending messages, each pending message is encapsulated separately and encapsulated into an independent event, which is a message event. The pending messages are encapsulated into a message event, which is a structure capable of storing and transmitting data.
[0175] Step 230 , determining at least one thread other than the thread independent of the interface processing as an event processing thread.
[0176] In this embodiment, the interface processing thread is a thread for processing interface interaction information and interface display information, and at least one thread independent of the interface processing thread refers to a thread other than the interface processing thread. In this embodiment, one thread other than the multiple interface processing threads is selected as the event processing thread, and the event processing thread is used to specifically process message events.
[0177] Step 240: The parallel framework processes the message event using the event processing thread.
[0178] In this embodiment, the message event is sent to the parallel framework for processing, and the parallel framework uses the event processing thread to process the message event. Since the event processing thread is independent of the interface processing thread, the processing efficiency and speed of the message event can be effectively improved. In addition, the parallel framework uniformly schedules the parsing data, which greatly improves the efficiency and speed of message processing.
[0179] In the above embodiment, when there are messages to be processed, an event processing thread other than the interface processing thread is used. In this way, the interface processing and message processing can be processed by different threads respectively, which improves the efficiency of message processing and avoids interface freezing.
[0180] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0181] When the existence of the message to be processed is detected, whether the data volume of the message to be processed is greater than the preset data volume, and / or whether the number of the messages to be processed is greater than the preset number is detected; when the data volume of the message to be processed is greater than the preset data volume or when the number of the messages to be processed is greater than the preset number, the message to be processed is encapsulated as a message event.
[0182] In one embodiment, when the existence of the message to be processed is detected, it is detected whether the data volume of the message to be processed is greater than the preset data volume; when the data volume of the message to be processed is greater than the preset data volume, the message to be processed is encapsulated as a message event.
[0183] In one embodiment, when the existence of the to-be-processed messages is detected, it is detected whether the number of the to-be-processed messages is greater than a preset number; when the number of the to-be-processed messages is greater than the preset number, the to-be-processed messages are encapsulated as message events.
[0184] In this embodiment, when it is detected that there are messages to be processed, the total data volume of the messages to be processed is detected, or the total number of messages to be processed is detected. When the total data volume of the messages to be processed is greater than the preset data volume, or when the total number of messages to be processed is large, it indicates that the data volume of the current messages to be processed is large or the number of messages is large. If the traditional method of processing the same thread as the interface processing is adopted, the interface will be stuck. Therefore, in this embodiment, when the data volume of the message to be processed is greater than the preset data volume or when the number of the messages to be processed is greater than the preset number, the message to be processed is encapsulated as a message event, and the message event is handed over to the parallel framework, and the parallel framework uses the event processing thread to process the message event. In this way, when the concurrent message data volume is large or the data is large, the parallel framework can be used to uniformly schedule and parse the data, which greatly improves the efficiency and speed of message processing.
[0185] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0186] When the data amount of the message to be processed is less than or equal to the preset data amount, the message to be processed is processed using a preset thread.
[0187] In this embodiment, the preset thread is a thread pre-assigned to message processing. In this embodiment, when the data volume of the message to be processed is small, processing the message to be processed will not occupy more resources, and will not cause much impact on the interface interaction and interface display. Therefore, the original preset thread can be used to process the message to be processed. In one embodiment, the preset thread is the same process as the thread of interface processing, that is, the thread of interface processing is the thread of interface processing. Since the amount of data contained in the message to be processed is small, the resources occupied by processing the message are small, and the same thread as the interface processing can be directly used for processing. In this way, the thread of interface processing can be less affected, avoiding interface jamming.
[0188] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0189] When the number of the pending messages is less than or equal to the preset number, after a preset time, the number of the pending messages is detected again to see whether it is greater than the preset number. When the number of the pending messages is less than or equal to the preset number, the pending messages are processed using a preset thread; when the number of the pending messages is greater than the preset number, the pending messages are encapsulated as message events.
[0190] In this embodiment, when the number of pending messages is less than or equal to the preset number, a strategy different from that when the data volume of the pending messages is less than or equal to the preset data volume is adopted. Specifically, when the number of pending messages is less than or equal to the preset number, it indicates that the number (number of messages) of pending messages is small. Therefore, in order to avoid affecting the process of interface interaction, first wait for a period of time, that is, wait for the preset time. After the preset time, the number of pending messages is detected again. When the number of pending messages is still less than or equal to the preset number, it indicates that the pending messages are still small. Therefore, it is beneficial for the preset thread to process the pending messages to improve the processing efficiency. If the number of pending messages increases after the preset time, and increases to more than the preset number, the pending messages are encapsulated as message events and the message events are handed over to the parallel framework, and the parallel framework uses the event processing thread to process the message events. In this way, the parallel processing efficiency of a large number of messages can be improved to avoid affecting the interface interaction. It should be understood that the preset time can be set to be shorter, so that the pending messages can be prevented from being overly delayed. Through the above process, when the number of pending messages is small, the preset thread can be used to process the pending messages. When the number of pending messages accumulates to a large number, they are uniformly processed by the parallel framework through the event processing thread. Without affecting the timeliness of message processing, it can not only improve the processing efficiency, but also reduce the impact on interface interaction, thereby improving the flexibility of message processing.
[0191] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0192] When the number of pending messages is less than or equal to the preset number, the resource consumption level of the interface interaction thread is detected, and the preset time is determined according to the resource consumption level of the interface interaction thread. After the preset time, it is detected again whether the number of pending messages is greater than the preset number. When the number of pending messages is less than or equal to the preset number, the pending messages are processed using the preset thread; when the number of pending messages is greater than the preset number, the pending messages are encapsulated as message events.
[0193] In this embodiment, the resource consumption degree of the thread of interface interaction is used to indicate the degree of various computer resources such as CPU (Central Processing Unit), memory, cache, etc. occupied by the thread of interface interaction. If the resource consumption degree of the thread of interface interaction is high, it means that the interface interaction occupies more computer resources, and if the resource consumption degree of the thread of interface interaction is low, it means that the interface interaction occupies less computer resources. The preset time is determined according to the resource consumption degree of the thread of interface interaction. The preset time and the resource consumption degree of the thread of interface interaction are positively correlated functionally. When the resource consumption degree of the thread of interface interaction is high, the corresponding preset time is longer, so that the number of pending messages can be detected again in as long a time as possible. If more pending messages are accumulated in a longer time, the accumulated pending messages are processed by threads other than the interface interaction to avoid affecting the thread of the interactive interface. If there are no more pending messages accumulated in a longer time, the preset thread can be directly used for processing. On the contrary, when the resource consumption of the thread of interface interaction is low, the corresponding preset time is shorter. In this way, by detecting the number of pending messages again after a shorter time interval, more pending messages can be accumulated, and the accumulated pending messages can be processed by threads other than interface interaction. It should be understood that the longer the preset time, the more pending messages are accumulated under the same circumstances, the easier it is to trigger the use of threads other than interface interaction to process the accumulated pending messages, and the smaller the probability of affecting the thread of interface interaction. Therefore, by setting the relationship between the preset time and the resource consumption of the thread of interface interaction as a positive correlation function relationship, the impact on threads other than interface interaction can be further reduced, thereby further avoiding the interface from being stuck.
[0194] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0195] Call the preset function to transfer the message processing process to the background;
[0196] Check in the background whether there are pending messages.
[0197] In this embodiment, the preset function is used to transfer the process that needs to be transferred to the background to run into the background. In this embodiment, the preset function is used to transfer the process of message processing into the background. By calling this preset function, the process of message processing can be transferred to the background to avoid affecting the interaction and display of the current interface. Specifically, first create a class responsible for sending and receiving MQTT messages: admqttcommun, which is only responsible for communication. When this class is constructed, a mosquitto instance is created and initialized, and then the moveToThread function is called to make the message processing enter the background thread to avoid blocking the interface. In this embodiment, since the process of real-time detection of messages to be processed is transferred to the background, the occupation of resources can be effectively reduced, thereby avoiding too much impact on interface interaction and interface display.
[0198] In one embodiment, the to-be-processed messages include to-be-received messages and to-be-sent messages;
[0199] When the computer program is executed by the processor, the following steps are also implemented: the step of processing the message event by the parallel framework using the event processing thread includes: processing the message event by the parallel framework using the event processing thread, receiving the message to be received and / or sending the message to be sent.
[0200] In this embodiment, the messages to be processed include messages to be sent and messages to be received. In this embodiment, the parallel framework is used to process the messages to be processed using the event processing thread, perform data analysis, receive the messages to be received, and send the messages to be sent.
[0201] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0202] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0203] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A data sending and receiving message processing method, characterized in that: include: Check whether there are pending messages; When it is detected that there is the to-be-processed message, encapsulating the to-be-processed message as a message event; Determine at least one thread other than the thread independent of the interface processing as an event processing thread; The parallel framework processes the message event using the event processing thread.
2. The method according to claim 1, characterized in that: When the existence of the to-be-processed message is detected, the step of encapsulating the to-be-processed message into a message event comprises: When it is detected that there is the message to be processed, detecting whether the data volume of the message to be processed is greater than a preset data volume, and / or detecting whether the number of the messages to be processed is greater than a preset number; When the data volume of the to-be-processed message is greater than the preset data volume or when the number of the to-be-processed messages is greater than the preset number, the to-be-processed message is encapsulated as a message event.
3. The method according to claim 2, characterized in that The method further comprises: When the data amount of the message to be processed is less than or equal to the preset data amount, the message to be processed is processed using a preset thread.
4. The method according to claim 2, characterized in that: The method further comprises: When the number of the pending messages is less than or equal to the preset number, after a preset time, the number of the pending messages is detected again to see whether it is greater than the preset number. When the number of the pending messages is less than or equal to the preset number, the pending messages are processed using a preset thread; when the number of the pending messages is greater than the preset number, the pending messages are encapsulated as message events.
5. The method according to claim 4, characterized in that The step of detecting whether there is a message to be processed comprises: Call the preset function to transfer the message processing process to the background; Check in the background whether there are pending messages.
6. The method according to any one of claims 1 to 5, characterized in that: The messages to be processed include messages to be received and messages to be sent; The step of processing the message event by the parallel framework using the event processing thread includes: The parallel framework processes the message event using the event processing thread, receives the to-be-received message, and / or sends the to-be-sent message.
7. A data transmission and reception message processing device, characterized in that: include: A pending message detection module is used to detect whether there is a pending message; A message encapsulation module, used for encapsulating the to-be-processed message into a message event when detecting the existence of the to-be-processed message; A thread determination module, used to determine a thread other than a thread independent of the interface processing as an event processing thread; The message processing module is used for the parallel framework to process the message event using the event processing thread.
8. The device according to claim 7, characterized in that The message encapsulation module comprises: a message detection unit, configured to, when detecting the existence of the to-be-processed message, detect whether the data volume of the to-be-processed message is greater than a preset data volume, and / or detect whether the number of the to-be-processed messages is greater than a preset number; The message encapsulation unit is used to encapsulate the to-be-processed message into a message event when the data volume of the to-be-processed message is greater than the preset data volume or when the number of the to-be-processed messages is greater than the preset number.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.