eclipse-paho / eclipse-paho/paho.mqtt.java

Message Order Not Preserved

Open
#966 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.3k
Forks
919
PR merge metrics
No merged PRs in 30d

Description

While using NanoMQ when the client is disconnected and connected back again the order in which the messages are published is not preserved. It is reading data in some random order. But when the client is connected the messages are received in proper order. How to receive messages in proper order?

I was sending a sequence of messages when the client was disconnected. And when the client is connected back again I am expecting the messages to be received in the proper order they are send.

Publisher Code
public class Publisher { public static void main(String[] args) {

try {
//publishing a message
MqttClient client=new MqttClient("tcp://192.168.2.8:1885",MqttClient.generateClientId(),new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setUserName("user1");
options.setPassword("password".toCharArray());
client.connect(options);

MqttMessage message=new MqttMessage();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

for(int i=1;i<=20;i++) {
Date date = new Date();
String str=simpleDateFormat.format(date)+" message "+i;
message.setPayload(str.getBytes());
message.setQos(2);
message.setRetained(true);
client.publish("iot_data_1",message);
}

client.disconnect();

} catch(MqttException me) {
System.out.println(me);
}

}
}

Consumer Code
public class Subscriber3 { public static void main(String[]args) throws MqttException { MqttClient client =new MqttClient("tcp://192.168.2.8:1885","subscriber004",new MemoryPersistence()); MqttConnectOptions options=new MqttConnectOptions(); options.setCleanSession(false); options.setUserName("user009"); options.setPassword("password008".toCharArray()); client.connect(options);

client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
System.out.println("Connection lost");
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println(new String(message.getPayload()));
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("Delivery Complete");
}
});

client.subscribe("iot_data_1");
//client.disconnect();
}
}

But messages are received in some random order. All the messages are received. But I need them in the order they are published.

NanoMQ latest version
Java Client in Windows and NanoMq on ubuntu centos in docker container
Java- Eclipse Paho
Publishing a sequence of messages when the client is disconnected and when client is connected back need persisted messages in order

I need messages in the order in which they are published not in some random order.

Contributor guide

Open the contributing guide

Research direction

Reproduce the report using the provided Publisher and Subscriber classes, MqttClient, MqttConnectOptions, QoS 2, and clean sessions against NanoMQ in Docker. First inspect how the Eclipse Paho Java client handles queued messages after reconnect, then compare that behavior with MQTT ordering expectations. Done means identifying whether ordering is guaranteed and documenting or testing the observed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java
Domain
distributed-systems, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.