[MQTT exception] mqtt.inbound.MqttPahoMessageDrivenChannelAdapter Lost connection: Disconnected; retrying...

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…

Insert image description here

2. Cause analysis

Reason 1: The clientID is the same, that is, the clientId is repeated.

Insert image description here

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:

Insert image description here
Insert image description here
After modification, we can view the clientId from the EMQ Dashboard page, as shown below:

Insert image description here

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.

Insert image description here

This article is finished!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/132832845