0

paho-mqtt5:test多次使用相同时clientId,它会抛出异常Client not connected,但如果我clientId对每个使用不同的tofrom那么它会正常工作

2021-10-05 19:25:28,650 ERROR [org.apa.cam.pro.err.DefaultErrorHandler] (Camel (camel-1) thread #0 - timer://test) Failed delivery for (MessageId: 871E4623819E4FB-000000000000001B on ExchangeId: 871E4623819E4FB-000000000000001B). Exhausted after delivery attempt: 1 caught: Client is not connected (32104)

Message History (complete message history is disabled)
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [from[timer://test?period=1000]                                                ] [         0]
    ...
[route1            ] [to1               ] [paho:test                                                                     ] [         0]

Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
: Client is not connected (32104)
    at org.eclipse.paho.mqttv5.client.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:32)
    at org.eclipse.paho.mqttv5.client.internal.ClientComms.sendNoWait(ClientComms.java:231)
    at org.eclipse.paho.mqttv5.client.MqttAsyncClient.publish(MqttAsyncClient.java:1530)
    at org.eclipse.paho.mqttv5.client.MqttClient.publish(MqttClient.java:564)
    at org.apache.camel.component.paho.mqtt5.PahoMqtt5Producer.process(PahoMqtt5Producer.java:55)
    at org.apache.camel.support.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:66)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:172)
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:463)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:179)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:64)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:184)
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:398)
    at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:210)
    at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76)
    at java.base/java.util.TimerThread.mainLoop(Timer.java:556)
    at java.base/java.util.TimerThread.run(Timer.java:506)

这是我抛出异常的代码

@ApplicationScoped
class TestRouter : RouteBuilder() {
    override fun configure() {
        val mqtt5Component = PahoMqtt5Component()
        mqtt5Component.configuration = PahoMqtt5Configuration().apply {
            brokerUrl = "tcp://192.168.99.101:1883"
            clientId = "paho123"
            isCleanStart = true
        }
        context.addComponent("paho-mqtt5", mqtt5Component)

        from("timer:test?period=1000").setBody(constant("Testing timer2")).to("paho-mqtt5:test")
  
        from("paho-mqtt5:test").process { e ->
            val body = (e.`in`?.body as? ByteArray)?.let { String(it) }
            println("test body 1 => $body")
        }
               
    }
}
4

1 回答 1

0

@William,这是预期
的行为消息代理使用客户端ID来区分客户端,因此它可以为不再使用的客户端连接执行内务管理
此外,客户端可能具有代理保留的“最后遗嘱和遗嘱”踪迹

可以在当前“clientId”的末尾附加一个随机数,因为它可能没有人,但你会关心这个
如果你可以访问个人登录,你也可以使用它,但你仍然想要使每个会话独一无二,以防它们运行多个会话

也许我不明白您的问题是什么
每个客户端都必须有一个唯一的 ID
您观察到什么让您认为它正在为单个客户端创建多个连接?
您是否有可能打开多个窗口并且每个窗口都生成不同的 clientId?

这是通过监视服务器所看到的内容来诊断问题的好方法
我的 paho-mqtt 客户端(Javascript)正在连接为“webclient”,并且我附加了一个随机数(webclient173)来识别该客户端
要进行故障排除,我建议您关闭客户端上的所有连接并监控MQTT进程的日志
当监控器到位时,从当前没有连接的客户端打开一个连接

这是我的 Mosquitto 日志文件的示例连接

$ tail -f /var/log/mosquitto/mosquitto.log
1635169943:没有指定消息。
1635169943: Sending CONNACK to webclient173 (0, 0)
1635169943: Received SUBSCRIBE from webclient173
1635169943: testtopic (QoS 0)
1635169943: Sending SUBACK to webclient173
1635170003: Received PINGREQ from webclient173
1635170003: Sending PINGRESP to webclient173
1635170003: Received PINGREQ from webclient173
1635170003:将 PINGRESP 发送到 webclient173

你的日志显示什么?

于 2021-10-05T20:57:35.730 回答