Publish message from callback messageArrived() [message #1729610] |
Sat, 16 April 2016 00:02 |
Dominik Pichler Messages: 1 Registered: April 2016 |
Junior Member |
|
|
Hey guys,
I'm new to MQTT and have the following question. Why does client.publish(...) block if it is invoked inside the MqttCallback-method messageArrived(...)? (The complete program is at the end). How can I avoid this?
The first two messages
client.publish("my/topic", ("message1 works").getBytes(), 2, false);
client.publish("my/topic", ("message2 works").getBytes(), 2, false);
What am I doing wrong?
The documentation to this method says:
Quote:It is possible to send a new message within an implementation of this callback (for example, a response to this message), but the implementation must not disconnect the client, as it will be impossible to send an acknowledgment for the message being processed, and a deadlock will occur. But how?
Here is the complete Code:
import org.eclipse.paho.client.mqttv3.*;
class SimpleClient implements MqttCallback{
private static final String MQTT_BROKER = "tcp://localhost:1883";
private MqttClient client;
SimpleClient(){
try {
client = new MqttClient(MQTT_BROKER, "myID");
client.connect();
client.publish("my/topic", ("message1 works").getBytes(), 2, false);
client.publish("my/topic", ("message2 works").getBytes(), 2, false);
client.setCallback(this);
client.subscribe("my/subscription/topic");
} catch (MqttException e) {
e.printStackTrace();
}
}
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println("Here I am");
client.publish("some/other/topic", ("message").getBytes(), 2, false);
System.out.println("I don't reach this point!");
}
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
// not needed
}
public void connectionLost(Throwable throwable) {
// not needed
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.02919 seconds