org.eclipse.paho.client mqtt客户端连接超时

时间:2019-09-02 19:06:21   收藏:0   阅读:3105

客户端订阅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());
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!