MQTT client message guarantee method and system under multiple Brokers

The independent thread and message temporary queue mechanism handles the Broker switching of the MQTT client in a multi-Broker environment, solving the reliability problem of message delivery, realizing automatic reconnection and resending of messages, and improving system stability and production efficiency. Reduced operating expenses.

CN118784453AActive Publication Date: 2024-10-15SHANGHAI ELANOVA ENERGY STORAGE TECH CO LTD +1
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202411126259.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-16
Publication Date
2024-10-15
Estimated Expiration
2044-08-16

AI Technical Summary

Technical Problem

In a multi-Broker environment, when the MQTT client fails to switch over the Broker, the continuity and reliability of message delivery are affected, resulting in data loss and service interruption, and the original message cannot be simply resent and recognized by the new Broker.

Method used

Through independent threads and message temporary queue mechanisms, it obtains available Broker addresses, establishes connections, handles authentication and message sending, ensures that messages are not lost during Broker switching, and automatically reconnects and resends unconfirmed messages in the event of network failure.

Benefits of technology

It improves the reliability of message delivery and system stability, enhances fault tolerance, reduces resource waste, ensures reliable transmission of production data, reduces downtime and maintenance costs, and improves production efficiency and customer satisfaction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118784453A_ABST
    Figure CN118784453A_ABST
Patent Text Reader

Abstract

The invention discloses an MQTT client message guarantee method and system. The method comprises the following steps: S1, obtaining an available Broker address; s2, establishing a connection between a single point MQTT client and an available Broker; s3, the independent thread waits for the authentication result of the available Broker on the MQTT client, if the authentication succeeds, the step S4 is executed, and if the authentication fails, the step S1 is executed; s4, judging whether historical unverified service messages exist in the message temporary storage queue or not, if yes, entering S5, and if not, entering S6; s5, taking out historical unverified service messages and sending the historical unverified service messages to the available Broker, and entering S7; s6, waiting for a new service message, sending the new service message to the available Broker, temporarily storing the new service message id as a keyword in the message temporary storage queue, and entering S7; and S7, waiting for receiving a sending completion callback notification containing the message id, clearing the service message of the message id in the message temporary storage queue when the notification is received, and repeating the step S6.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technology, and in particular to a method and system for ensuring MQTT client messages in a multi-Broker environment. Background Art

[0002] With the rapid development of the Internet of Things (IoT) and messaging services, the MQTT protocol has become the first choice for communication between many devices due to its lightweight, low bandwidth consumption and high efficiency.

[0003] MQTT clients usually establish a connection with a single Broker. Under the existing offline conditions, the reliability of message transmission can generally be guaranteed. However, when a single Broker fails, the continuity and reliability of message delivery will be affected, which may lead to data loss and service interruption. In a multi-Broker environment that includes high availability, disaster recovery, and load balancing, since there is a mechanism for verifying the identity after switching Brokers, the original messages during the interruption period cannot be recognized by the new Broker simply by resending them.

[0004] In view of the above problems, the present invention proposes an MQTT client solution, which allows the client to access different available Brokers successively, and ensures the complete delivery of messages under any circumstances through reliable message management and failover strategies, thereby significantly improving the reliability and performance of the system. Summary of the invention

[0005] In view of the problems and shortcomings of the prior art, the present invention provides a method and system for ensuring MQTT client messages in a multi-Broker environment.

[0006] The present invention solves the above technical problems through the following technical solutions:

[0007] The present invention provides a method for guaranteeing MQTT client messages under multiple Brokers, which is characterized in that it comprises the following steps:

[0008] S1. Obtain an available Broker address and its related configuration information from the Broker address management platform;

[0009] S2. Based on the address of the available Broker returned and its related configuration information, a single-point MQTT client is created, and a connection is established between the single-point MQTT client and the available Broker;

[0010] S3, the independent thread waits for the authentication result of the available Broker to the single-point MQTT client. If the authentication result is successful, the process proceeds to step S4. If the authentication result is failed, the process repeats step S1.

[0011] S4, the independent thread determines whether there is a historical unverified business message in the temporary storage queue of the to-be-verified message, if yes, proceeds to step S5, if no, proceeds to step S6;

[0012] S5. The independent thread retrieves the historical unverified business message and sends it to the available Broker, and then proceeds to step S7.

[0013] S6, the independent thread waits for the new business message of the MQTT client and sends it to the available Broker, and temporarily stores the message ID of the new business message as a keyword in the temporary storage queue of the message to be verified, and proceeds to step S7;

[0014] S7, the independent thread waits for receiving the sending completion callback notification containing the message id, and when receiving it, clears the business message of the message id from the temporary storage queue of the message to be verified with the message id, and repeats step S6;

[0015] In step S7, if the independent thread encounters a network failure while waiting for receipt, it proceeds to step S1.

[0016] Preferably, after step S7, when a set clearing cycle is reached, the independent thread clears out the business messages whose temporary storage time in the temporary storage queue of the to-be-verified messages reaches the set temporary storage time.

[0017] The present invention also provides an MQTT client message guarantee system under multiple Brokers, which is characterized in that it includes an acquisition module, a creation module and an independent thread module;

[0018] The acquisition module is used to obtain an available Broker address and related configuration information from the Broker address management platform;

[0019] The creation module is used to create a single-point MQTT client based on the address of the available Broker returned and its related configuration information, and establish a connection between the single-point MQTT client and the available Broker;

[0020] The independent thread module is used to use an independent thread to wait for the authentication result of the available Broker for the single-point MQTT client, and when the authentication result is successful, determine whether there is a historical unverified business message in the temporary storage queue of the message to be verified, and when there is a historical unverified business message, take out the historical unverified business message and send it to the available Broker, and when there is no historical unverified business message, wait for the new business message of the MQTT client and send it to the available Broker, and temporarily store the message id of the new business message as a keyword in the temporary storage queue of the message to be verified, and call the acquisition module again when the authentication result is a failed authentication;

[0021] The independent thread module is also used to use an independent thread to wait for a sending completion callback notification containing a message id, and when received, clear the business message of the message id from the temporary storage queue of messages to be verified with the message id, and wait again for a new business message to be sent;

[0022] The independent thread module is also used to call the acquisition module if a network failure occurs during the process of waiting for the independent thread to receive.

[0023] Preferably, the independent thread module is further used to use the independent thread to clear out the business messages whose temporary storage time in the temporary storage queue of the to-be-verified messages reaches the set temporary storage time when a set cleaning cycle is reached.

[0024] The positive and progressive effects of the present invention are:

[0025] The present invention improves the reliability of message transmission, ensures that messages will not be lost or sent repeatedly during the switching process of multiple Brokers, and enhances the stability of the MQTT client. In addition, the system fault tolerance is enhanced, and the independent thread handles the reconnection logic, so that the client can automatically reconnect after the connection is interrupted and resend unconfirmed messages to ensure the continuity and reliability of the system. Furthermore, resource utilization is optimized, and resources are effectively managed through independent threads and message temporary queue mechanisms to reduce resource waste caused by unstable connections.

[0026] The present invention can protect the transmission of production data in economic production, ensure that key data between sensors and control systems can be transmitted reliably, and avoid production accidents or losses caused by data loss or delays. In addition, production efficiency is improved, and the stable message transmission mechanism reduces production stagnation caused by system failure or message loss during reconnection, thereby improving overall production efficiency. Furthermore, by reducing downtime and maintenance costs caused by message loss or system failure, enterprises can significantly reduce operating expenses. Enterprises with high-reliability message transmission mechanisms can provide better services, improve customer satisfaction and market competitiveness. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 The present invention is a flowchart of a method for guaranteeing MQTT client messages under multiple brokers according to a preferred embodiment of the present invention. DETAILED DESCRIPTION

[0028] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0029] like Figure 1 As shown, this embodiment provides a message assurance method for MQTT clients under multiple Brokers, with independent threads as the core, and uses the sending callback and disconnection callback characteristics of a single-point MQTT client to determine the message status and link status, and establish a message temporary storage queue mechanism. When processing the switching of multiple Brokers, the reconnection logic is used to ensure that the message can be resent, thereby achieving reliability assurance of message delivery.

[0030] This method includes the following steps:

[0031] Step 101: Obtain an available Broker address and related configuration information from the Broker address management platform.

[0032] Step 102: Based on the address of the available Broker returned and its related configuration information, a single-point MQTT client is created, and a connection between the single-point MQTT client and the available Broker is established.

[0033] Step 103, the independent thread waits for the authentication result of the available Broker to the single-point MQTT client. If the authentication result is successful, proceed to step 104; if the authentication result is failed, repeat step 101.

[0034] Step 104 , the independent thread determines whether there is any historical unverified business message in the temporary storage queue of the to-be-verified message, if yes, proceeds to step 105 , if no, proceeds to step 106 .

[0035] Step 105 : The independent thread retrieves the historical unverified business message and sends it to the available Broker, and then proceeds to step 107 .

[0036] Step 106 , the independent thread waits for a new service message from the MQTT client and sends it to the available Broker, and temporarily stores the message ID of the new service message as a keyword in a temporary storage queue for messages to be verified, and then proceeds to step 107 .

[0037] Step 107, the independent thread waits for receiving the sending completion callback notification containing the message id, and upon receiving it, clears the business message of the message id from the temporary storage queue of messages to be verified with the message id, and repeats step 106.

[0038] In step 107 , if the independent thread encounters a network failure while waiting for receipt, it proceeds to step 101 .

[0039] In this embodiment, if a network failure occurs and the service message sent during the period is not completely sent, no callback notification will be generated and the temporarily stored message will not be cleared.

[0040] In this embodiment, after reconnection, identity authentication is required again. After successful identity authentication, all temporarily stored messages are retrieved and sent. If the message fails to be sent again, the failed message will continue to flow back to the temporary message queue and wait for the next sending. After identity authentication fails, a new available Broker needs to be obtained from the Broker address management platform.

[0041] After step 107, when a set clearing cycle is reached, the independent thread clears out the business messages whose temporary storage time in the temporary storage queue of the to-be-verified messages reaches the set temporary storage time.

[0042] In this embodiment, because the sending and completion callback responses of the message are very rapid, usually, the length of the message temporary storage queue is close to 0, and the resulting memory consumption is extremely low.

[0043] The message storage queue has a set cleanup cycle, which will clean up messages that have been stored for too long. The storage period can be adjusted based on actual conditions.

[0044] This message guarantee mechanism is mainly aimed at solving the problem of message loss when switching between different Brokers in an environment where multiple Brokers are available, achieving message reliability without losing performance.

[0045] This embodiment also provides an MQTT client message assurance system under multiple Brokers, which includes an acquisition module, a creation module and an independent thread module.

[0046] The acquisition module is used to obtain an available Broker address and its related configuration information from the Broker address management platform.

[0047] The creation module is used to create a single-point MQTT client based on the address of the available Broker returned and its related configuration information, and establish a connection between the single-point MQTT client and the available Broker.

[0048] The independent thread module is used to use an independent thread to wait for the authentication result of the available Broker for the single-point MQTT client. When the authentication result is successful, it determines whether there are historical unverified business messages in the temporary storage queue of messages to be verified. If yes, it takes out the historical unverified business messages and sends them to the available Broker. If no, it waits for the new business message of the MQTT client and sends it to the available Broker, and temporarily stores the message id of the new business message as the keyword in the temporary storage queue of messages to be verified. When the authentication result is failed, the acquisition module is called again.

[0049] The independent thread module is also used to use an independent thread to wait for a sending completion callback notification containing a message id, and when received, clear the business message of the message id from the temporary storage queue of messages to be verified with the message id, and wait again for a new business message to be sent.

[0050] The independent thread module is also used to call the acquisition module if a network failure occurs during the waiting process of receiving using the independent thread.

[0051] The independent thread module is also used to use the independent thread to clean up the business messages whose temporary storage time in the temporary storage queue of the to-be-verified messages reaches the set temporary storage time when a set cleaning cycle is reached.

[0052] Although the specific embodiments of the present invention are described above, those skilled in the art should understand that these are only examples, and the protection scope of the present invention is defined by the appended claims. Those skilled in the art may make various changes or modifications to these embodiments without departing from the principles and essence of the present invention, but these changes and modifications all fall within the protection scope of the present invention.

Claims

1. A method for guaranteeing MQTT client messages under multiple brokers, characterized in that: It includes the following steps: S1. Obtain an available Broker address and its related configuration information from the Broker address management platform; S2. Based on the address of the available Broker returned and its related configuration information, a single-point MQTT client is created, and a connection is established between the single-point MQTT client and the available Broker; S3, the independent thread waits for the authentication result of the available Broker to the single-point MQTT client. If the authentication result is successful, the process proceeds to step S4. If the authentication result is failed, the process repeats step S1. S4, the independent thread determines whether there is a historical unverified business message in the temporary storage queue of the to-be-verified message, if yes, proceeds to step S5, if no, proceeds to step S6; S5. The independent thread retrieves the historical unverified business message and sends it to the available Broker, and then proceeds to step S7. S6, the independent thread waits for the new business message of the MQTT client and sends it to the available Broker, and temporarily stores the message ID of the new business message as a keyword in the temporary storage queue of the message to be verified, and proceeds to step S7; S7, the independent thread waits for receiving the sending completion callback notification containing the message id, and when receiving it, clears the business message of the message id from the temporary storage queue of the message to be verified with the message id, and repeats step S6; In step S7, if the independent thread encounters a network failure while waiting for receipt, it proceeds to step S1.

2. The MQTT client message assurance method under multiple Brokers as claimed in claim 1, characterized in that: After step S7, when a set clearing cycle is reached, the independent thread clears out the business messages whose temporary storage time in the temporary storage queue of the to-be-verified messages reaches the set temporary storage time.

3. A multi-Broker MQTT client message assurance system, characterized in that: It includes an acquisition module, a creation module and an independent thread module; The acquisition module is used to obtain an available Broker address and related configuration information from the Broker address management platform; The creation module is used to create a single-point MQTT client based on the address of the available Broker returned and its related configuration information, and establish a connection between the single-point MQTT client and the available Broker; The independent thread module is used to use an independent thread to wait for the authentication result of the available Broker for the single-point MQTT client, and when the authentication result is successful, determine whether there is a historical unverified business message in the temporary storage queue of the message to be verified, and when there is a historical unverified business message, take out the historical unverified business message and send it to the available Broker, and when there is no historical unverified business message, wait for the new business message of the MQTT client and send it to the available Broker, and temporarily store the message id of the new business message as a keyword in the temporary storage queue of the message to be verified, and call the acquisition module again when the authentication result is a failed authentication; The independent thread module is also used to use an independent thread to wait for a sending completion callback notification containing a message id, and when received, clear the business message of the message id from the temporary storage queue of messages to be verified with the message id, and wait again for a new business message to be sent; The independent thread module is also used to call the acquisition module if a network failure occurs during the process of waiting for the independent thread to receive.

4. The MQTT client message guarantee system under multiple Brokers as claimed in claim 3, characterized in that: The independent thread module is also used to use the independent thread to clean up the business messages whose temporary storage time in the temporary storage queue of the to-be-verified messages reaches the set temporary storage time when a set cleaning cycle is reached.

Citation Information

Patent Citations

  • Message routing method based on MQTT and Kafka high-concurrency scene

    CN108768826A

  • Method for realizing load balancing by multiple Brokers based on an MQTT protocol

    CN113452800A

  • MQTT-based information transmission method, system and communication device

    CN114025002A

  • Cluster load balancing method based on message middleware AOP

    CN115883560A

  • Distributed message communication method and system based on Java and MQ

    CN117354117A