MqttPahoMessageDrivenChannelAdapter Lost connection: Disconnected; solution for retrying...
1. Background description
Springboot integrates MQTT. After the server is started, the console keeps reporting that MQTT has been disconnected and is retrying. The problem is that the MQTT client frequently disconnects and comes online.
The error content is as follows:
MQTT Rec: xxxxx org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter Lost connection: Disconnected; retrying…
2. Cause analysis
Reason 1: The clientID is the same, that is, the clientId is repeated.
Since my colleagues and I developed in parallel and used the same set of code, the clientId of our two MQTT connections resulted in duplication.
Reason 2: The current user does not have Topic permissions
Reason three: An exception was encountered during business processing within the callback function and was not captured.
3. Solution
3.1 Option 1
The solution to reason one is to use the clientID globally to ensure uniqueness, which can be done by UUID/timestamp, etc.
There are many ways to implement it. The following code is just one of them, for reference:
After modification, we can view the clientId from the EMQ Dashboard page, as shown below:
3.2 Option 2
The solution to the second reason is that if the acl permission is configured, check whether the currently logged in mqtt user has the permission to subscribe to the corresponding topic. If there is no permission, it will cause frequent disconnection and reconnection. If there is no permission, the corresponding permission needs to be added.
3.3 Option 3
The solution to reason three is to perform try-catch capture in the statement block where exceptions may occur.
This article is finished!