Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Lost messages using Paho(When publishing messages they are sometimes lost.)
icon13.gif  Lost messages using Paho [message #1751948] Tue, 17 January 2017 06:11 Go to next message
magnus persson is currently offline magnus perssonFriend
Messages: 3
Registered: January 2017
Junior Member
Razz
I made a program which publishes some messages and later tries to read 3 of them. When running the program the second time I would expect message 4-6 to be read. That does not happen.
What am I doing wrong ?

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class Main implements MqttCallback {

private MqttClient sampleClient;
private String topic = "MQTT Examples";
private int qos = 2;
private int received = 0;

public static void main(String[] args) throws MqttException, InterruptedException {
Main client;
client = new Main("c1");
client.close();

client = new Main("csend");
client.send();
client.close();

client = new Main("c1");
client.subscribe();
client.waitFor(3);
client.close();
}

public Main(String clientId) {
String broker = "tcp://localhost:1883";
// String broker = "tcp://iot.eclipse.org:1883";
MqttClientPersistence persistence = new MemoryPersistence();

try {
sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(false);
sampleClient.setCallback(this);

System.out.println("Connecting to broker: " + broker);
IMqttToken tok = sampleClient.connectWithResult(connOpts);
System.out.println("Connected sessionPresent:" + tok.getSessionPresent() + " complete:" + tok.isComplete());

} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}

public void close() throws MqttException {
sampleClient.disconnect();
System.out.println("Disconnected\n");
}

private void subscribe() throws MqttException {
sampleClient.subscribe(topic, qos);
}

private void send() throws MqttException {
for (int i = 0; i < 10; i++) {
send("hej" + i);
}
System.out.println("Message published");
}

public static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void send(String msg) throws MqttException {
System.out.println("Publishing message: " + msg);
MqttMessage message = new MqttMessage(msg.getBytes());
message.setQos(qos);
sampleClient.publish(topic, message);
}

@Override
public void connectionLost(Throwable cause) {
System.out.println("lost conn");
}

@Override
public synchronized void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("got msg:" + message);
received();
}

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

private synchronized void waitFor(int nr) throws InterruptedException {
do {
System.out.println("waitFor:" + nr + " received:" + received);
wait();
if (received >= nr) {
System.out.println("waitFor ret received:" + received);
return;
}
} while (true);
}

private synchronized void received() {
received++;
notifyAll();
}

}
  • Attachment: Main.java
    (Size: 3.56KB, Downloaded 32 times)
Re: Lost messages using Paho [message #1752005 is a reply to message #1751948] Tue, 17 January 2017 15:45 Go to previous message
James Sutton is currently offline James SuttonFriend
Messages: 71
Registered: July 2015
Member
It would likely be because you are not setting the retain flag when publishing messages. Unless the message is retained, or a client is subscribing to a topic it is being sent to, the broker will throw the message away.
Previous Topic:'Paho' is undefined
Next Topic:Reconnecting the Mqtt client after network failure - receives the previously received messages again
Goto Forum:
  


Current Time: Thu Nov 15 12:22:57 GMT 2018

Powered by FUDForum. Page generated in 0.01642 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top