客户端订阅mqtt 的时候 显示超时。第二次重起服务可能就好了,但是可能会丢消息。
报错如下
org.eclipse.paho.client.mqttv3.MqttException: 等待来自服务器的响应时超时 at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31) ~[org.eclipse.paho.client.mqttv3-1.2.1.jar!/:na] at org.eclipse.paho.client.mqttv3.internal.ClientState.checkForActivity(ClientState.java:741) ~[org.eclipse.paho.client.mqttv3-1.2.1.jar!/:na] at org.eclipse.paho.client.mqttv3.internal.ClientComms.checkForActivity(ClientComms.java:816) ~[org.eclipse.paho.client.mqttv3-1.2.1.jar!/:na] at org.eclipse.paho.client.mqttv3.internal.ClientComms.checkForActivity(ClientComms.java:802) ~[org.eclipse.paho.client.mqttv3-1.2.1.jar!/:na] at org.eclipse.paho.client.mqttv3.TimerPingSender$PingTask.run(TimerPingSender.java:79) ~[org.eclipse.paho.client.mqttv3-1.2.1.jar!/:na] at java.util.TimerThread.mainLoop(Timer.java:555) ~[na:1.8.0_221] at java.util.TimerThread.run(Timer.java:505) ~[na:1.8.0_221]
解决办法:
log.info("try connect to mqtt");//调用connect 后,一定要再调用 一下token.waitForCompletion();这样应该就可以了。原因不明。
IMqttToken token = client.connect(connOpts);token.waitForCompletion();log.info("connect mqtt success");client.subscribe(topic, 1, new MqttMsgListener());
原文地址:https://www.cnblogs.com/zhangyy3/p/11448130.html
时间: 2024-11-02 07:36:08