使用java paho客户端,在程序内创建了两个客户端,总是出现连接断开

  • EMQX 版本: 4.3.2
  • java mqtt客户端:org.eclipse.paho.client.mqttv5:1.2.5
    这个服务里面两个客户端id是不一样,两个客户端连接的是同一个emqx,两个客户端都会断开连接,每天都会断个一次的样子。是其他服务里面一个客户端从来不会断。现在没有思路,大概从哪些方面定位?
    客户端日志如下:
    [2024-06-11 21:31:28.695] ERROR[MQTT Ping: server1718065579910]org.eclipse.paho.mqttv5.client.internal.ClientState[210] sig_server1718065579910: Timed out as no write activity, keepAlive=60,000,000,000 lastOutboundActivity=36,019,990,853,495,040 lastInboundActivity=36,019,930,920,185,297 time=36,020,130,633,033,528 lastPing=0
    [2024-06-11 21:31:28.696] ERROR[MQTT Snd: server1718065579910]org.eclipse.paho.mqttv5.client.internal.CommsSender[210] sig_server1718065579910: exception java.net.SocketException: Socket closed
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:118)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at org.eclipse.paho.mqttv5.client.wire.MqttOutputStream.flush(MqttOutputStream.java:53)
    at org.eclipse.paho.mqttv5.client.internal.CommsSender.run(CommsSender.java:147)
    at java.lang.Thread.run(Thread.java:748)

[2024-06-11 21:31:28.696] ERROR[pool-662076-thread-1]com.signal.domain.mqtt.MqttV5[278] 发布消息失败 org.eclipse.paho.mqttv5.common.MqttException: Timed out while waiting to write messages to the server
at org.eclipse.paho.mqttv5.client.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:32)
at org.eclipse.paho.mqttv5.client.internal.ClientState.checkForActivity(ClientState.java:753)
at org.eclipse.paho.mqttv5.client.internal.ClientComms.checkForActivity(ClientComms.java:875)
at org.eclipse.paho.mqttv5.client.internal.ClientComms.checkForActivity(ClientComms.java:863)
at org.eclipse.paho.mqttv5.client.TimerPingSender$PingTask.run(TimerPingSender.java:105)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)

参考:ClientState: C5180518000011: Timed out as no activity, keepAlive=60,000 lastOutboundActivity=1,534,923,567,979 lastInboundActivity=1,534,923,507,945 time=1,534,923,627,951 lastPing=1,534,923,567,979 · Issue #576 · eclipse/paho.mqtt.java · GitHub

I also encountered the same problem, I solved it by upgrading org.eclipse.paho.client.mqttv3 from 1.2.4 to 1.2.5

有人说 paho 升级到 1.2.5 就解决了

找到原因了,其实是代码里面还有第三个客户端,这个客户端连的是另外一套emqx一直连接不上,当收到业务请求后发现没连接上会一直创建客户端,当新创建客户端的端口与运行正常的两个客户端的本地端口一样时,端口会占用,导致链接断开;
第三个客户端有开关配置,但是以前的人代码写反的,配的enable=flase坑爹的表示运行,一开始看了配置以为enable=flase不会运行。接手别人的屎山代码 :joy:,这问题定位了一周,mmp