gvasquez gvasquez -4 years ago 104
Java Question

MQTT Client repeating last message, when restarted

Whenever I restart the WAR project that runs the MQTT client, I get back the last message received by the client, thus getting duplicated. What could be causing this? Client Code? Server config?

Client side



OS: Debian GNU/Linux 8.2 (jessie); Java: java version "1.7.0_91", OpenJDK Runtime Environment (IcedTea 2.6.3) (7u91-2.6.3-1~deb8u1), OpenJDK 64-Bit Server VM (build 24.91-b01, mixed mode)

Apache Tomcat/8.0.14 (Debian)

MQTT lib from https://repo.eclipse.org/content/repositories/paho-releases/ Maven Repository using the following dependency:

<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.0.1</version>
</dependency>


Using this stub to receive messages, implementing org.eclipse.paho.client.mqttv3.MqttCallback:

@Override
public void messageArrived(String topic, MqttMessage msg) throws Exception {}


I see that there's also another method that can be overriden, but I'm not considering:

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub

}


What else should I provide?

Answer Source

This is working as designed.

The problem is not with the subscription end, it is beings caused by the publisher.

The messages are being published with the retained bit set to true.

There is a good description of retained messages on the hivemq website here, but basically the retained bit tells the broker to store the last published message with the flag for a given topic and to deliver this to any client at the point they subscribe to the topic. This includes when a client reconnects. The use case is to ensure a client always has the latest data.

The deliveryComplete function is called only when messages are published (it is the client library confirming that the message has left it's control based on the QOS level)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download