MQTT - persistence not working [message #1845062] |
Tue, 05 October 2021 06:44  |
Eclipse User |
|
|
|
Hi, I've started MQTT broker (Mosquitto) and Java application that subscribes topic "orders".
I'm using mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.mqttv5.client/1.2.5
and the client is started by code:
public connectAndSubscribe(String topic, IMqttMessageListener messageListener) throws MqttException {
MqttClient client = new MqttClient(mosquittoConfig.getBrokerURI(), "testClientId");
connectClientIfNeeded(client);
MqttSubscription subscription = new MqttSubscription(topic, mosquittoConfig.getDefaultQos());
return client.subscribe(new MqttSubscription[]{subscription}, new IMqttMessageListener[]{messageListener});
}
private void connectClientIfNeeded(MqttClient client) throws MqttException {
if (!client.isConnected()) {
client.connect(getConnectOptions());
}
}
private MqttConnectionOptions getConnectOptions() {
MqttConnectionOptions options = new MqttConnectionOptions();
options.setCleanStart(false);
options.setUserName(mosquittoConfig.getUser());
options.setPassword(mosquittoConfig.getPassword().getBytes(StandardCharsets.UTF_8));
options.setAutomaticReconnect(true);
options.setRequestProblemInfo(true);
options.setRequestResponseInfo(true);
return options;
}
As you can see I set CleanStart to false and the clientId is constant.
But I have the following problem:
1. Mosquitto started
2. Java application started and testClientId subscribes topic "orders"
3. some client publish message "Message A" on topic "orders"
4. testClientId receive message "Message A" and execute IMqttMessageListener
5. Java application is stopped
6. some client publish message "Message B" on topic "orders"
7. Java application started and testClientId subscribes topic "orders"
8. testClientId does not receive message "Message B"
When I perform the same scenario using mosquitto it works as expected. Subscriber after reconnect receives messages sent in the meantime
mosquitto_sub -V mqttv5 -d -u <user> -P <password>-t orders -q 2 -c -i subscriberClient
|
|
|
Re: MQTT - persistence not working [message #1845080 is a reply to message #1845062] |
Wed, 06 October 2021 03:21  |
Eclipse User |
|
|
|
I added options.setSessionExpiryInterval(EXPIRY_INTERVAL);
and it helped, mosquitto treats Java app client as a session present -> CONNACK(1, 0).
However I've another issue. Messages are sent to paho client but the messages are not processed by any registered listeners.
Sometimes while debugging I noticed created persistence files that were processed later (which is ok), but it was random, normally none of messages sent after connecting are processed by paho.
|
|
|
Powered by
FUDForum. Page generated in 0.17912 seconds