Getting stability in work MQTT error

I get the following exception from the mqtt broker when I try to create a new MqttClient. The error here is ---

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 

Here is the code of my java class receive() method from which I am trying to connect to mqtt ---

 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); } } 

And here is the class method of MQTTHandler createClient() ---

 MqttClient subClient = null; try { subClient = new MqttClient(brokerURL, clientId); } catch (MqttException e) { } 

When I first create a client for userId, it works. From the second time he does not cope with the above exception. I use clean-session=false here.

If anyone has any ideas please let me know. Thanks.

+7
java mqtt
source share
2 answers

Both clients seem to be trying to use the same file for saving.
Javadocs for MqttDefaultFilePersistence.open () says

Initialize persistent storage. If persistent storage exists for this client ID, then open it, otherwise create a new one. If persistent storage is already open, just go back. An application can use the same client identifier to connect to many different servers, so the client identifier in combination with the connection uniquely identifies the persistence store. Throws: MqttPersistenceException - if there is a problem opening a persistent store.

I believe the file is already open, you should use a different clientId in your code for each of your Mqtt clients.

+8
source share

This is because in both clients you use the same persistence name.

 client = new MqttClient("tcp://192.168.1.100:1883", "One"); 

In the following thread, you use the same:

 client1 = new MqttClient("tcp://192.168.1.100:1883", "One"); 

The name to save should be different for each connection you want to create. You should make the following changes to client1:

 client = new MqttClient("tcp://192.168.1.100:1883", "Two"); 
+1
source share

All Articles