A method and system for inter-process communication of MQTT communication agent
By adopting a fixed topic inter-process communication method in the MQTT communication agent, the problem of degradation of the proxy process processing efficiency is solved, stable communication connection and message publishing accuracy is achieved, and the stability of MQTT server connection and message of business processes is ensured. Processing efficiency.
Patent Information
- Application Number
- CN202311834313.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-27
- Publication Date
- 2024-11-12
- Estimated Expiration
- 2043-12-27
AI Technical Summary
In embedded systems with multi-process architecture, the MQTT proxy process needs to frequently deal with new topics, resulting in a decrease in processing efficiency and affecting communication stability.
Using the inter-process communication method of fixed topics, the proxy process subscribes to fixed proxy topics, and the business process subscribes to fixed business topics, maintains the connection with the MQTT server through a one-to-many communication model, and ensures message release through the connection status and message TOKEN mechanism. accuracy and efficiency.
Avoid the proxy process directly affecting the processing efficiency due to the addition of new business processes, ensure the stability of communication connections, ensure the accurate release and efficiency of messages, and re-establish the connection status after the process starts or crashes, avoiding offline messages being left unprocessed. The situation.
Smart Images

Figure CN117793187B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of network communication technology, and in particular to an inter-process communication method and system of an MQTT communication agent. Background Art
[0002] MQTT (Message Queuing Telemetry Transport) is a message protocol based on the publish / subscribe paradigm under the ISO standard (ISO / IEC PRF 20922). It is a client-server based message publish / subscribe transport protocol. Each message consists of a topic and a payload.
[0003] At present, MQTT is widely used in the automotive field. In the automotive field, each vehicle is equivalent to a client. The target vehicle can communicate with the server through a transmission network (such as a wireless communication network), and implement the tasks of subscribing to topics and publishing messages based on MQTT, and receive messages that match the subscribed topics; that is, clients on multiple different devices can simultaneously connect to the same server with their respective client identifiers. Clients can subscribe to topics and publish messages, and receive messages that match the subscribed topics sent by the server. In an embedded system with a multi-process architecture, the proxy process and the business process are both running in the client on the car. In order to enable multiple business processes to send and receive messages through an MQTT client, it is necessary to set up a proxy process to be responsible for the connection between the MQTT client and the MQTT server. Other business processes and the proxy process indirectly send and receive MQTT communication messages through inter-process communication.
[0004] However, in the above solution, since each business process may be associated with different communication topics, the proxy process needs to continuously increase the processing of new topics, which will directly affect the processing efficiency of the proxy process. Summary of the invention
[0005] In view of this, an embodiment of the present application provides an inter-process communication method of an MQTT communication agent, which adopts a fixed topic and will not directly affect the agent process even if a new business process is added.
[0006] In a first aspect, the present application provides an inter-process communication method of an MQTT communication agent, the method comprising:
[0007] The proxy process enables the MQTT client and the MQTT server to communicate according to the MQTT protocol; the proxy process and multiple business processes constitute an inter-process communication library of the publish-subscribe model to perform inter-process communication; the proxy process and the business process both run in the MQTT client;
[0008] The proxy process subscribes to a fixed proxy topic and publishes an inter-process communication message to the business process with a fixed business topic;
[0009] The business process subscribes to a fixed business topic and publishes an inter-process communication message to the proxy process with a fixed proxy topic; the proxy process and the business process have a one-to-many inter-process communication.
[0010] According to the above-mentioned technical means, the proxy process in this application subscribes to a fixed proxy topic, and the business process subscribes to a fixed business topic. By adopting a fixed topic, even if a new business process is added, it will not directly affect the proxy process; and the inter-process communication between the proxy process and the business process is a one-to-many communication. As long as there is a business process that needs to send and receive MQTT communication messages, the proxy process will maintain a connection with the MQTT server, thereby ensuring the stability of the communication connection.
[0011] In combination with the first aspect, in one implementation, the inter-process communication message includes a message header and a message body;
[0012] The message header includes a message function identifier and a publisher identifier; the message body is composed of one or more integer data and character string data arranged in a fixed order according to the different message function identifiers.
[0013] According to the above technical means, the inter-process communication message of the present application defines a variety of messages in a structured data format, and the publication of the inter-process communication message realizes the indirect sending and receiving of MQTT communication messages through the proxy process.
[0014] In combination with the first aspect, in one implementation, the proxy process and the service process both store connection status information of MQTT;
[0015] The proxy process periodically publishes inter-process communication messages of connection status;
[0016] After receiving the connection status published by the proxy process, the business process publishes an inter-process communication message of an MQTT connection request or an MQTT disconnection request when the connection status of the proxy process is inconsistent with the connection status stored in the business process;
[0017] After receiving the MQTT connection request or the MQTT disconnection request, the proxy process sets or clears the bit flag of the business process corresponding to the message publisher information according to the message publisher information;
[0018] The proxy process initiates an MQTT connection request in all business processes, or initiates a communication connection with the MQTT server after a first preset time has passed;
[0019] When no business process initiates an MQTT connection request, the proxy process disconnects from the MQTT server.
[0020] According to the above-mentioned technical means, in this application, the proxy process and the business process can re-establish inter-process communication under conditions such as different startup orders, or restart after the process crashes; and, the proxy process sends periodic connection status notifications to the business process, so that the connection status can be re-established after the death and restart of both processes. After the business process is ready, the proxy process initiates a connection with the MQTT server to avoid the situation where offline messages received when the connection is just successful are not processed by anyone.
[0021] In combination with the first aspect, in one implementation, the business process publishes an inter-process communication message of an MQTT publishing request in an MQTT connected state;
[0022] After receiving the MQTT publishing request, the proxy process sends the MQTT publishing request to the MQTT server, obtains a publishing token TOKEN, and publishes an inter-process communication message of the MQTT publishing confirmation;
[0023] After receiving the MQTT publishing confirmation, the business process waits for the inter-process communication message published by MQTT according to the publishing token TOKEN;
[0024] After receiving the MQTT published message published by the MQTT server, the proxy process obtains the published token TOKEN and publishes the inter-process communication message published by MQTT;
[0025] After receiving the inter-process communication message published by MQTT, the business process confirms the completion of the publication based on the published token TOKEN;
[0026] If the business process does not receive the MQTT publishing confirmation and the MQTT published communication message within the second preset time, it is considered that the publishing has failed.
[0027] According to the above technical means, in this application, if multiple business processes publish messages separately, the proxy process can enable multiple business processes to confirm the publication of messages separately through the mechanism of publishing confirmation, published messages and message TOKEN, thereby improving the accuracy and efficiency of message publishing.
[0028] In combination with the first aspect, in one embodiment, the proxy process and the business process both store an MQTT subscription request table, and the proxy process also stores an MQTT subscribed topic table; before the proxy process starts the MQTT connection for the first time or restarts the MQTT connection, the proxy process clears the MQTT subscribed topic table and the MQTT subscription request table in the proxy process.
[0029] In combination with the first aspect, in one implementation, the business process publishes an inter-process communication message of an MQTT subscription request when the MQTT connection state changes;
[0030] After receiving the MQTT subscription request, the proxy process adds the subscription topic of the MQTT subscription request to the MQTT subscription request table if the subscription topic of the MQTT subscription request to be subscribed is not in the MQTT subscribed topic table;
[0031] The proxy process sequentially takes out the subscription topics of the MQTT subscription request table, and sends a subscription request to the MQTT server. After the subscription is successful, the subscription topic is appended to the MQTT subscribed topic table.
[0032] In combination with the first aspect, in one implementation, after the MQTT subscription request published by the business process is successfully subscribed, when the MQTT server receives new information of the subscription topic, it publishes an MQTT received message;
[0033] After receiving the MQTT received message published by the MQTT server, the proxy process publishes the MQTT received inter-process communication message;
[0034] After receiving the inter-process communication message received by the MQTT, the business process matches the MQTT subscription request table stored in the business process according to the subscription topic, and confirms receipt of the MQTT message.
[0035] According to the above-mentioned technical means, in the present application, when multiple business processes subscribe to topics separately, the proxy process can ensure that the same topic will not be subscribed repeatedly through the subscription request table and the subscribed topic table; the present application is built on the basis of the inter-process communication model of subscription and publication, and the number of business processes can be increased as needed without affecting the proxy process; in addition, in the present application, the proxy process needs to merge and process the MQTT topic subscription requests issued by the business process, and will only initiate one subscription request for a specific MQTT topic during the connection period with the MQTT server.
[0036] In a second aspect, the present application provides an inter-process communication system of an MQTT communication agent, wherein the system is used to execute an inter-process communication method of an MQTT communication agent as described above;
[0037] The system includes an MQTT client and an MQTT server; a proxy process and multiple business processes are running in the MQTT client;
[0038] The proxy process is used to implement communication between the MQTT client and the MQTT server according to the MQTT protocol; the proxy process and the business process also constitute an inter-process communication library of the publish-subscribe model to perform inter-process communication;
[0039] The proxy process is also used to subscribe to a fixed proxy topic and publish an inter-process communication message to the business process with a fixed business topic;
[0040] The business process is also used to subscribe to a fixed business topic and publish an inter-process communication message to the proxy process with a fixed proxy topic; the proxy process and the business process are in one-to-many inter-process communication.
[0041] In a third aspect, the present application provides a computer device, comprising a processor and a memory, wherein the memory stores at least one instruction, and the at least one instruction is loaded and executed by the processor to implement the above-mentioned inter-process communication method of an MQTT communication agent.
[0042] In a fourth aspect, the present application provides a computer-readable storage medium, wherein at least one instruction is stored in the storage medium, and the at least one instruction is loaded and executed by a processor to implement the above-mentioned inter-process communication method of an MQTT communication agent.
[0043] The technical solution provided by this application may have the following beneficial effects:
[0044] In the above scheme, the proxy process subscribes to a fixed proxy topic, and the business process subscribes to a fixed business topic. With fixed topics, the number of business processes can be increased as needed without affecting the proxy process.
[0045] The inter-process communication between the proxy process and the business process is one-to-many communication. As long as there is a business process that needs to send or receive MQTT communication messages, the proxy process will maintain a connection with the MQTT server, ensuring the stability of the communication connection.
[0046] Based on the inter-process communication messages of the connection status, the proxy process and the business process can re-establish inter-process communication under conditions such as different startup orders or restart after a process crash; and the proxy process sends periodic connection status notifications to the business process, so that the connection status can be re-established after the two processes die and restart. After the business process is ready, the proxy process will initiate a connection with the MQTT server to avoid the situation where offline messages received when the connection is just successful are not processed. In addition, if multiple business processes are publishing messages separately, the proxy process can enable multiple business processes to confirm the publishing of messages by publishing confirmations, published messages and message TOKENs, thereby improving the accuracy and efficiency of message publishing;
[0047] When multiple business processes of the present application subscribe to topics separately, the proxy process can ensure that the same topic will not be subscribed repeatedly through the subscription request table and the subscribed topic table; and, based on the subscribed topic table, the proxy process of the present application can merge and process the MQTT topic subscription requests issued by the business processes, and will only initiate one subscription request for a specific MQTT topic during the connection period with the MQTT server. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the specific implementation methods of the present application or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0049] Figure 1 It is a structural diagram of an inter-process communication system of an MQTT communication agent according to an exemplary embodiment.
[0050] Figure 2 It is a method flow chart of an inter-process communication method of an MQTT communication agent according to an exemplary embodiment.
[0051] Figure 3 It is a method flow chart of an inter-process communication method of an MQTT communication agent according to an exemplary embodiment.
[0052] Figure 4 A structural block diagram of a computer device shown in an exemplary embodiment of the present application is shown. DETAILED DESCRIPTION
[0053] The technical solution of the present application will be described clearly and completely below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present application.
[0054] Figure 1 It is a schematic diagram of the structure of an inter-process communication system of an MQTT communication agent according to an exemplary embodiment. The system is used to execute an inter-process communication method of an MQTT communication agent as described below;
[0055] like Figure 1 As shown, the system includes an MQTT client 120 and an MQTT server 110; a proxy process and multiple business processes are running in the MQTT client 120;
[0056] The proxy process is used to implement communication between the MQTT client 120 and the MQTT server 110 according to the MQTT protocol; the proxy process and the business process also constitute an inter-process communication library of the publish-subscribe model to perform inter-process communication;
[0057] The proxy process is also used to subscribe to a fixed proxy topic and publish inter-process communication messages to the business process with a fixed business topic;
[0058] The business process is also used to subscribe to a fixed business topic and publish an inter-process communication message to the proxy process with a fixed proxy topic; the proxy process and the business process have a one-to-many inter-process communication.
[0059] Furthermore, in the vehicle-mounted field, the MQTT client 120 in the embodiment of the present application can be a vehicle-mounted client. The MQTT client 120 and the MQTT server 110 implement topic subscription and message publishing through the MQTT transmission protocol. MQTT is a client-server based message publishing and subscription transmission protocol. Each message consists of a topic and a payload. In actual application scenarios, the number of the MQTT clients 120 is large. As a vehicle-mounted client, the MQTT client 120 can subscribe to topics and publish messages to the MQTT server 110 based on the MQTT transmission protocol, and receive messages published by the MQTT server 110 that match the subscribed topics.
[0060] Furthermore, the MQTT client 120 runs multiple business processes. In an embedded system that adopts a multi-process architecture, in order to enable multiple business processes to send and receive messages through one MQTT client 120, it is necessary to set up a proxy process to be responsible for the connection between the MQTT client 120 and the MQTT server 110. Other business processes and the proxy process indirectly send and receive MQTT communication messages through inter-process communication.
[0061] Furthermore, the proxy process acts as a data transfer station, receiving messages from publishers and forwarding them to subscribers. At the same time, it is also responsible for processing connection requests from subscribers and publishers, as well as requests to subscribe to and unsubscribe from topics. In other words, the MQTT proxy provides a communication bridge between business processes, through which business processes can exchange and share data.
[0062] Furthermore, the communication protocol layer between the proxy process and the business process needs to package the topic and payload of the MQTT message in order to implement the behavior of topic subscription, message sending and message receiving. In other words, the proxy process and the business process also implement topic subscription and message publishing through the MQTT transmission protocol.
[0063] Furthermore, since each MQTT client 120 runs multiple business processes, the inter-process communication between the proxy process and the business process is one-to-many communication. As long as there is a business process that needs to send and receive MQTT communication messages, the proxy process needs to maintain a connection with the MQTT server.
[0064] Furthermore, when the proxy process and the business process implement inter-process communication, their inter-process communication method is built on the basis of the inter-process communication model of the publish and subscribe mechanism; the proxy process and the business process are in a one-to-many relationship; the proxy process subscribes to a fixed proxy topic and publishes inter-process communication messages with a fixed business topic; the business process subscribes to a fixed business topic and publishes inter-process communication messages with a fixed proxy topic; and the inter-process communication message defines a variety of messages in a structured data format.
[0065] In summary, the proxy process subscribes to a fixed proxy topic, and the business process subscribes to a fixed business topic. With fixed topics, the number of business processes can be increased as needed without affecting the proxy process.
[0066] The inter-process communication between the proxy process and the business process is one-to-many communication. As long as there is a business process that needs to send or receive MQTT communication messages, the proxy process will maintain a connection with the MQTT server, ensuring the stability of the communication connection.
[0067] Based on the inter-process communication messages of the connection status, the proxy process and the business process can re-establish inter-process communication under different startup orders, or restart after the process crashes; and the proxy process sends periodic connection status notifications to the business process, so that the connection status can be re-established after the two processes die and restart. After the business process is ready, the proxy process will initiate a connection with the MQTT server to avoid the situation where no one handles the offline messages received when the connection is successful. In addition, if multiple business processes are publishing messages separately, the proxy process can enable multiple business processes to confirm the release of messages by publishing confirmations, published messages, and message TOKEN mechanisms, thereby improving the accuracy and efficiency of message publishing.
[0068] Figure 2 FIG. 1 is a method flow chart showing an inter-process communication method of an MQTT communication agent according to an exemplary embodiment. Figure 2 As shown, the method may include the following steps:
[0069] Step S201, the proxy process implements the communication between the MQTT client and the MQTT server according to the MQTT protocol; the proxy process and multiple business processes constitute an inter-process communication library of the publish-subscribe model to perform inter-process communication; the proxy process and the business process both run in the MQTT client.
[0070] In one possible implementation, since each MQTT client runs multiple business processes, in order to enable multiple business processes to send and receive messages through one MQTT client, it is necessary to set up a proxy process to be responsible for the connection between the MQTT client and the MQTT server. Other business processes and the proxy process indirectly send and receive MQTT communication messages through inter-process communication.
[0071] Step S202: The proxy process subscribes to a fixed proxy topic, and publishes an inter-process communication message to the business process with a fixed business topic.
[0072] In one possible implementation, the proxy process subscribing to a fixed proxy topic means that the proxy process receives messages through a specific subscription topic; the proxy process is a bridge connecting publishers and subscribers. After subscribing to a topic, the proxy process will receive all messages published to this topic, and then forward the messages to all subscribers who have subscribed to it, that is, business processes, according to this topic.
[0073] Step S203: The business process subscribes to a fixed business topic, and publishes an inter-process communication message to the proxy process with a fixed proxy topic; the proxy process and the business process perform one-to-many inter-process communication.
[0074] In one possible implementation, a business process subscribing to a fixed business topic refers to messages received and processed by a business processing process by subscribing to a specific business topic. In actual applications, a business process is usually implemented by a business processing system or business processing software, and a business topic can be regarded as a message category or business field that the business processing process is concerned about. By subscribing to a fixed business topic, a business process can receive and process specific types of messages in real time, thereby achieving real-time response and processing for specific business fields.
[0075] In summary, the proxy process subscribes to a fixed proxy topic, and the business process subscribes to a fixed business topic. With fixed topics, the number of business processes can be increased as needed without affecting the proxy process.
[0076] The inter-process communication between the proxy process and the business process is one-to-many communication. As long as there is a business process that needs to send or receive MQTT communication messages, the proxy process will maintain a connection with the MQTT server, ensuring the stability of the communication connection.
[0077] Based on the inter-process communication messages of the connection status, the proxy process and the business process can re-establish inter-process communication under different startup orders, or restart after the process crashes; and the proxy process sends periodic connection status notifications to the business process, so that the connection status can be re-established after the two processes die and restart. After the business process is ready, the proxy process will initiate a connection with the MQTT server to avoid the situation where no one handles the offline messages received when the connection is successful. In addition, if multiple business processes are publishing messages separately, the proxy process can enable multiple business processes to confirm the release of messages by publishing confirmations, published messages, and message TOKEN mechanisms, thereby improving the accuracy and efficiency of message publishing.
[0078] Figure 3 FIG. 1 is a method flow chart showing an inter-process communication method of an MQTT communication agent according to an exemplary embodiment. Figure 3 As shown, the method may include the following steps:
[0079] Step S301, the proxy process implements the communication between the MQTT client and the MQTT server according to the MQTT protocol; the proxy process and multiple business processes constitute an inter-process communication library of the publish-subscribe model to perform inter-process communication; the proxy process and the business process both run in the MQTT client.
[0080] In a possible implementation, the inter-process communication message includes a message header and a message body;
[0081] The message header includes a message function identifier and a publisher identifier; the message body is composed of one or more integer data and character string data arranged in a fixed order according to the different message function identifiers.
[0082] Furthermore, the message function identifier and the publisher identifier are both enumerated quantities, and the message function identifier identifies each message type in sequence using a sequence number starting with 0;
[0083] The publisher identifier uses 0 to identify the agent process and uses a sequence number starting from 1 to identify each business process.
[0084] Furthermore, the message function identifiers are ordinal numbers starting from 0 to identify various message types, including functions such as "connect", "disconnect", "subscribe", "publish", "publish successfully", "receive", "connection status" and "publish confirmation". The message body varies according to each message function identifier, and is composed of one or more integer data and string data arranged in a fixed order.
[0085] Step S302: The proxy process subscribes to a fixed proxy topic, and publishes an inter-process communication message to the business process with a fixed business topic.
[0086] Step S303: The business process subscribes to a fixed business topic, and publishes an inter-process communication message to the proxy process with a fixed proxy topic; the proxy process and the business process perform one-to-many inter-process communication.
[0087] Furthermore, the inter-process communication between the proxy process and the business process is a one-to-many communication. As long as there is a business process that needs to send and receive MQTT communication messages, the proxy process will maintain a connection with the MQTT server. Based on the inter-process communication model built on the publish and subscribe mechanism, the number of business processes can be increased as needed without affecting the proxy process.
[0088] Step S304: The proxy process and the business process both store MQTT connection status information; the proxy process periodically publishes inter-process communication messages of the connection status.
[0089] In a possible implementation, the connection status information includes three states: disconnected, connected, and connecting, and the initial state is disconnected. The periodic release can be released once every second, and the proxy process sends periodic connection status notifications to the service process, so that the connection state can be reestablished after the two processes die and restart;
[0090] Step S305: After receiving the connection status published by the proxy process, the business process publishes an inter-process communication message of an MQTT connection request or an MQTT disconnection request when the connection status of the proxy process is inconsistent with the connection status stored in the business process.
[0091] Step S306: After receiving the MQTT connection request or the MQTT disconnection request, the proxy process sets or clears the bit flag of the business process corresponding to the message publisher information according to the message publisher information.
[0092] Step S307: The proxy process initiates an MQTT connection request in all business processes, or initiates a communication connection with the MQTT server after a first preset time has passed.
[0093] Furthermore, the first preset time can be set according to actual application scenarios.
[0094] Step S308: When no business process initiates an MQTT connection request, the proxy process disconnects from the MQTT server.
[0095] Furthermore, first, the proxy process publishes an inter-process communication message of the connection status once per second; after receiving the communication message of the connection status, when the business process finds that it is inconsistent with the connection status stored in itself, it publishes an inter-process communication message of an MQTT connection request or an MQTT disconnection request; then, after receiving the communication messages of the MQTT connection request and the MQTT disconnection request, the proxy process sets or clears the bit flag of the business process according to the information of the message publisher; the proxy process initiates a connection request when all business processes initiate a connection request, or after a preset time has passed, the proxy process initiates a connection with the MQTT server; finally, when no business process has an MQTT connection request, the proxy process disconnects from the MQTT server, at this point, the synchronization of the proxy process and the business process is completed, and the proxy process realizes the connection with the MQTT server. Based on this, the proxy process and the business process can re-establish inter-process communication in different startup sequences, or restarting after a process crash, etc. After the business process is ready, the proxy process initiates a connection with the MQTT server to avoid the situation where no one handles the offline message received when the connection is just successful.
[0096] In a possible implementation, when the business process is in an MQTT connected state, the business process publishes an inter-process communication message of an MQTT publishing request;
[0097] After receiving the MQTT publishing request, the proxy process sends the MQTT publishing request to the MQTT server, obtains the publishing token TOKEN, and publishes an inter-process communication message of the MQTT publishing confirmation;
[0098] After receiving the MQTT publishing confirmation, the business process waits for the inter-process communication message published by MQTT according to the publishing token TOKEN;
[0099] After receiving the MQTT published message published by the MQTT server, the proxy process obtains the published token TOKEN and publishes the inter-process communication message published by MQTT;
[0100] After receiving the inter-process communication message published by MQTT, the business process confirms the completion of the publication based on the published token TOKEN;
[0101] If the business process does not receive the MQTT publishing confirmation and the MQTT published communication message within the second preset time, it is considered that the publishing has failed.
[0102] Furthermore, when the business process publishes an MQTT publishing request, first, the business process publishes the inter-process communication message of the MQTT publishing request in the MQTT connected state, and then waits for the process communication message of the MQTT publishing confirmation; after receiving the communication message of the MQTT publishing request, the proxy process sends a publishing request to the MQTT server, obtains the publishing token TOKEN, and then publishes the process communication message of the MQTT publishing confirmation; then, after receiving the communication message of the MQTT publishing confirmation, the business process waits for the process communication message published by MQTT according to the publishing token TOKEN; after receiving the MQTT published message issued by the MQTT server, the proxy process obtains the published token TOKEN and publishes the inter-process communication message published by MQTT; finally, after receiving the inter-process communication message published by MQTT, the business process confirms the completion of this publishing according to the published token TOKEN. If the business process does not receive the MQTT publishing confirmation and the MQTT published communication message within the specified time limit, it is considered that this publishing has failed. In the communication process of publishing MQTT publishing requests, when multiple business processes publish messages respectively, the proxy process enables multiple business processes to confirm the publishing of messages respectively through the mechanism of publishing confirmation, published messages and message TOKEN.
[0103] In a possible implementation, the proxy process and the service process both store an MQTT subscription request table, and the proxy process also stores an MQTT subscribed topic table;
[0104] Before starting the MQTT connection for the first time or restarting the MQTT connection, the proxy process clears the MQTT subscribed topic table and the MQTT subscription request table in the proxy process.
[0105] In a possible implementation, the business process publishes an inter-process communication message of an MQTT subscription request when the MQTT connection state changes;
[0106] After receiving the MQTT subscription request, the proxy process adds the subscription topic of the MQTT subscription request to the MQTT subscription request table if the subscription topic of the MQTT subscription request to be subscribed is not in the MQTT subscribed topic table;
[0107] The proxy process sequentially takes out the subscription topics of the MQTT subscription request table, and sends a subscription request to the MQTT server. After the subscription is successful, the subscription topic is appended to the MQTT subscribed topic table.
[0108] Furthermore, before the proxy process starts or restarts the MQTT connection for the first time, it clears the MQTT subscribed topic table and the MQTT subscription request table; when the business process needs to publish an MQTT subscription request, the business process publishes the inter-process communication message of the MQTT subscription request when the MQTT connection state changes; secondly, after the proxy process receives the communication message of the MQTT subscription request, if the topic to be subscribed is not in the MQTT subscribed topic table, it is added to the MQTT subscription request table; after the proxy process successfully connects to the MQTT server, it takes out the subscription topics in the MQTT subscription request table in turn, and sends a subscription request to the MQTT server. After the subscription is successful, the subscription topic is added to the MQTT subscribed topic table. In the communication of publishing the MQTT subscription request, the proxy process merges and processes the MQTT topic subscription requests issued by the business process, and only initiates one subscription request for a specific MQTT topic during the connection period with the MQTT server; that is, when multiple business processes subscribe to their own topics, the proxy process can ensure that the same topic will not be subscribed repeatedly through the subscription request table and the subscribed topic table.
[0109] In a possible implementation, after the MQTT subscription request published by the business process is successfully subscribed, when the MQTT server receives new information of the subscription topic, it publishes an MQTT received message;
[0110] After receiving the MQTT received message published by the MQTT server, the proxy process publishes the MQTT received inter-process communication message;
[0111] After receiving the inter-process communication message received by the MQTT, the business process matches the MQTT subscription request table stored in the business process according to the subscription topic, and confirms the receipt of the MQTT message.
[0112] Furthermore, after the MQTT subscription request published by the business process is successfully subscribed, the MQTT server publishes an MQTT received message when it receives new information of the subscription topic; the proxy process publishes an MQTT received inter-process communication message after receiving the MQTT received message published by the MQTT server; thereafter, after receiving the MQTT received inter-process communication message, the business process matches the MQTT subscription request table according to the subscription topic and confirms receipt of the MQTT message.
[0113] In summary, the proxy process subscribes to a fixed proxy topic, and the business process subscribes to a fixed business topic. With fixed topics, the number of business processes can be increased as needed without affecting the proxy process.
[0114] The inter-process communication between the proxy process and the business process is one-to-many communication. As long as there is a business process that needs to send or receive MQTT communication messages, the proxy process will maintain a connection with the MQTT server, ensuring the stability of the communication connection.
[0115] Based on the inter-process communication messages of the connection status, the proxy process and the business process can re-establish inter-process communication under conditions such as different startup orders or restart after a process crash; and the proxy process sends periodic connection status notifications to the business process, so that the connection status can be re-established after the two processes die and restart. After the business process is ready, the proxy process will initiate a connection with the MQTT server to avoid the situation where offline messages received when the connection is just successful are not processed. In addition, if multiple business processes are publishing messages separately, the proxy process can enable multiple business processes to confirm the publishing of messages by publishing confirmations, published messages and message TOKENs, thereby improving the accuracy and efficiency of message publishing;
[0116] When multiple business processes of the present application subscribe to topics separately, the proxy process can ensure that the same topic will not be subscribed repeatedly through the subscription request table and the subscribed topic table; and, based on the subscribed topic table, the proxy process of the present application can merge and process the MQTT topic subscription requests issued by the business processes, and will only initiate one subscription request for a specific MQTT topic during the connection period with the MQTT server.
[0117] See also Figure 4 , which is a schematic diagram of a computer device provided according to an exemplary embodiment of the present application, the computer device includes a memory and a processor, the memory is used to store a computer program, and when the computer program is executed by the processor, the above-mentioned inter-process communication method of the MQTT communication agent is implemented.
[0118] The processor may be a central processing unit (CPU). The processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or a combination of the above chips.
[0119] As a non-transitory computer-readable storage medium, the memory can be used to store non-transitory software programs, non-transitory computer executable programs and modules, such as program instructions / modules corresponding to the method in the implementation of the present application. The processor executes various functional applications and data processing of the processor by running the non-transitory software programs, instructions and modules stored in the memory, that is, implementing the method in the above method implementation.
[0120] The memory may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created by the processor, etc. In addition, the memory may include a high-speed random access memory, and may also include a non-transitory memory, such as at least one disk storage device, a flash memory device, or other non-transitory solid-state storage device. In some embodiments, the memory may optionally include a memory remotely disposed relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0121] In an exemplary embodiment, a computer-readable storage medium is also provided, which is used to store at least one computer program, and the at least one computer program is loaded and executed by a processor to implement all or part of the steps in the above method. For example, the computer-readable storage medium can be a read-only memory (ROM), a random access memory (RAM), a compact disc read-only memory (CD-ROM), a magnetic tape, a floppy disk, an optical data storage device, etc.
[0122] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the application disclosed herein. The present application is intended to cover any variations, uses or adaptations of the present application, which follow the general principles of the present application and include common knowledge or customary techniques in the art that are not disclosed in the present application. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the following claims.
[0123] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.
Claims
1. An inter-process communication method of an MQTT communication agent, characterized in that: The method comprises: The proxy process enables the MQTT client and the MQTT server to communicate according to the MQTT protocol; the proxy process and multiple business processes constitute an inter-process communication library of the publish-subscribe model to perform inter-process communication; the proxy process and the business process both run in the MQTT client; the proxy process is used to indicate the connection between the MQTT client and the MQTT server; the business process and the proxy process indirectly send and receive MQTT communication messages through inter-process communication; The proxy process subscribes to a fixed proxy topic and publishes an inter-process communication message to the business process with a fixed business topic; the inter-process communication message includes a message header and a message body; the message header includes a message function identifier and a publisher identifier; the message function identifier identifies various message types in sequence with a sequence number starting from 0, including connection, disconnection, subscription, publication, successful publication, reception, connection status and publication confirmation; The business process subscribes to a fixed business topic and publishes an inter-process communication message to the proxy process with a fixed proxy topic; the proxy process and the business process have a one-to-many inter-process communication.
2. The method according to claim 1, characterized in that The message body is composed of one or more integer data and character string data arranged in a fixed order according to different message function identifiers.
3. The method according to claim 1, characterized in that The proxy process and the business process both store the connection status information of MQTT; the method further includes: The proxy process periodically publishes inter-process communication messages of connection status; After receiving the connection status published by the proxy process, the business process publishes an inter-process communication message of an MQTT connection request or an MQTT disconnection request when the connection status of the proxy process is inconsistent with the connection status stored in the business process; After receiving the MQTT connection request or the MQTT disconnection request, the proxy process sets or clears the bit flag of the business process corresponding to the message publisher information according to the message publisher information; The proxy process initiates an MQTT connection request in all business processes, or initiates a communication connection with the MQTT server after a first preset time has passed; When no business process initiates an MQTT connection request, the proxy process disconnects from the MQTT server.
4. The method according to claim 3, characterized in that The method further comprises: When the business process is in the MQTT connected state, the business process publishes an inter-process communication message of the MQTT publishing request; After receiving the MQTT publishing request, the proxy process sends the MQTT publishing request to the MQTT server, obtains a publishing token TOKEN, and publishes an inter-process communication message of the MQTT publishing confirmation; After receiving the MQTT publishing confirmation, the business process waits for the inter-process communication message published by MQTT according to the publishing token TOKEN; After receiving the MQTT published message published by the MQTT server, the proxy process obtains the published token TOKEN and publishes the inter-process communication message published by MQTT; After receiving the inter-process communication message published by MQTT, the business process confirms the completion of the publication based on the published token TOKEN; If the business process does not receive the MQTT publishing confirmation and the MQTT published communication message within the second preset time, it is considered that the publishing has failed.
5. The method according to any one of claims 1 to 3, characterized in that: The proxy process and the business process both store an MQTT subscription request table, and the proxy process also stores an MQTT subscribed topic table; the method further includes: Before the proxy process starts the MQTT connection for the first time or restarts the MQTT connection, the proxy process clears the MQTT subscribed topic table and the MQTT subscription request table in the proxy process.
6. The method according to claim 5, characterized in that The method further comprises: When the MQTT connection state changes, the business process publishes an inter-process communication message of an MQTT subscription request; After receiving the MQTT subscription request, the proxy process adds the subscription topic of the MQTT subscription request to the MQTT subscription request table if the subscription topic of the MQTT subscription request to be subscribed is not in the MQTT subscribed topic table; The proxy process sequentially takes out the subscription topics of the MQTT subscription request table, and sends a subscription request to the MQTT server. After the subscription is successful, the subscription topic is appended to the MQTT subscribed topic table.
7. The method according to claim 6, characterized in that After the MQTT subscription request published by the business process is successfully subscribed, the method further includes: When the MQTT server receives new information of the subscribed topic, it publishes an MQTT received message; After receiving the MQTT received message published by the MQTT server, the proxy process publishes the MQTT received inter-process communication message; After receiving the inter-process communication message received by the MQTT, the business process matches the MQTT subscription request table stored in the business process according to the subscription topic, and confirms receipt of the MQTT message.
8. An inter-process communication system of an MQTT communication agent, characterized in that: The system is used to execute an inter-process communication method of an MQTT communication agent as described in any one of claims 1 to 7; The system includes an MQTT client and an MQTT server; a proxy process and multiple business processes are running in the MQTT client; The proxy process is used to implement communication between the MQTT client and the MQTT server according to the MQTT protocol; the proxy process and the business process also constitute an inter-process communication library of the publish-subscribe model to perform inter-process communication; the proxy process is also used to indicate the connection between the MQTT client and the MQTT server; The proxy process is also used to subscribe to a fixed proxy topic and publish an inter-process communication message to the business process with a fixed business topic; The business process is also used to subscribe to a fixed business topic and publish inter-process communication messages to the proxy process with a fixed proxy topic; the proxy process and the business process are one-to-many inter-process communication; the business process is also used to indirectly send and receive MQTT communication messages with the proxy process through inter-process communication; the inter-process communication message includes a message header and a message body; the message header includes a message function identifier and a publisher identifier; the message function identifier uses a sequence number starting from 0 to identify various message types in sequence, including connection, disconnection, subscription, publication, successful publication, reception, connection status and publication confirmation.
9. A computer device, characterized in that: The computer device includes a processor and a memory, wherein the memory stores at least one instruction, and the at least one instruction is loaded and executed by the processor to implement an inter-process communication method of an MQTT communication agent as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The storage medium stores at least one instruction, and the at least one instruction is loaded and executed by the processor to implement an inter-process communication method of an MQTT communication agent as described in any one of claims 1 to 7.
