gpt4 book ai didi

java - 从 MQTT 获取已在使用中的持久性错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:52 29 4
gpt4 key购买 nike

当我尝试创建新的 MqttClient 时,我从 mqtt 代理收到以下异常。错误在这里---

Caused by: Persistence already in use (32200)
at org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:108) [mqtt-client-0.4.0.jar:]
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:273) [mqtt-client-0.4.0.jar:]
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:222) [mqtt-client-0.4.0.jar:]
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:134) [mqtt-client-0.4.0.jar:]
at com.ericsson.asdp.virtualassist.notification.messaging.MQTTHandler.createClient(MQTTHandler.java:61) [classes:]
at com.ericsson.asdp.virtualassist.notification.messaging.MQTTMessagingService.receieve(MQTTMessagingService.java:52) [classes:]
... 44 more

这是我尝试连接到 mqtt 的 java 类 receive() 方法的代码 ---

MqttClient subClient = null;
try {
subClient = mqttHandler.createClient(userId, brokerURL);
MQTTNotificationSubscriber notificationSub = new MQTTNotificationSubscriber(mqttHandler);
notificationSub.setUserId(userId);

subClient.setCallback(notificationSub);
mqttHandler.subscribe(subClient, userId);
// do something here
} catch (Exception e) {
logger.error("Error in receive " + e.getMessage());
throw new VirtualAssistServicesException(e.getMessage(), e);
} finally {
try {
mqttHandler.disconnect(subClient);
} catch (MqttException e) {
throw new VirtualAssistServicesException(e.getMessage(), e);
}
}

这里是 MQTTHandler 类 createClient() 方法 ---

MqttClient subClient = null;

try {
subClient = new MqttClient(brokerURL, clientId);

} catch (MqttException e) {

}

当我第一次为 userId 创建客户端时,它起作用了。从第二次开始,它因上述异常而失败。我在这里使用 clean-session=false

如果有人有任何想法,请告诉我。谢谢。

最佳答案

看起来两个客户端都在尝试使用相同的文件进行持久化。
MqttDefaultFilePersistence.open() 的 Javadocs说

Initialise the persistent store. If a persistent store exists for this client ID then open it, otherwise create a new one. If the persistent store is already open then just return. An application may use the same client ID to connect to many different servers, so the client ID in conjunction with the connection will uniquely identify the persistence store required.
Throws: MqttPersistenceException - if there was a problem opening the persistent store.

我想文件已经打开,您必须在代码中为每个 Mqtt 客户端使用不同的 clientId

关于java - 从 MQTT 获取已在使用中的持久性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623910/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com