MQTT设置自动重连后,无法自动订阅以前的主题
一、业务场景我们在使用MQTT的时候在设置客户端参数的时候设置的自动重连属性但是断开重连后无法订阅断开之前订阅的消息。二、异常处理在设置MQTT客户端参数配置的时候记得设置不要清除Session即可/** * MQTT连接参数设置 */ private MqttConnectOptions mqttConnectOptions(String userName, String passWord) throws MqttException { mqttClient new MqttClient(HOST, clientId, new MemoryPersistence()); MqttConnectOptions options new MqttConnectOptions(); options.setUserName(userName); options.setPassword(passWord.toCharArray()); options.setConnectionTimeout(10);///默认30 options.setAutomaticReconnect(true);//默认false options.setCleanSession(false);//默认true //options.setKeepAliveInterval(20);//默认60 return options; }设置AutomaticReconnect(true)和CleanSession(false)这样MQTT客户端在断开重连后还能继续收到之前的消息