Skip to main content



      Home
Home » Eclipse Projects » Paho » Is MqttClient thread safe?
Is MqttClient thread safe? [message #1693648] Mon, 27 April 2015 05:26 Go to next message
Eclipse UserFriend
Hi, I am using Paho for Java 1.0.2. My application subscribes events from devices and send back reply as device commands. So I want to use MqttClient instance in callback MqttCallback.messageArrived(String topic, MqttMessage message).

private volatile MqttClient client;

client = create client...
client.setCallback(new MqttCallback() {
  public void messageArrived(String topic, MqttMessage message) {
    client.publish(...); // Send out command.
  }
...
});


I assume the callback is called by other thread than the thread that create MqttClient instance. Is it safe to use client in messageArrived method?
Re: Is MqttClient thread safe? [message #1693950 is a reply to message #1693648] Wed, 29 April 2015 08:52 Go to previous messageGo to next message
Eclipse UserFriend
Yes, the callback is called by another thread.

From the Javadoc for MqttCallback:

1) This method is invoked synchronously by the MQTT client. An acknowledgment is not sent back to the server until this method returns cleanly.

2) 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.

So, yes, you should be able to call publish() within the callback. The resulting message will be sent before any acknowledgement to the incoming message.
Re: Is MqttClient thread safe? [message #1711199 is a reply to message #1693950] Wed, 14 October 2015 02:26 Go to previous message
Eclipse UserFriend
FYI I have just reproduced a deadlock caused by publishing from the subscription callback. I resolved this by publishing in a new thread.
Previous Topic:WebSocket network error: The operation couldn't be completed. (OSStatus error -9807.)
Next Topic:openwrt: stdinpub: waitfor unexpectedly is NULL
Goto Forum:
  


Current Time: Sun Jul 06 14:24:15 EDT 2025

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

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

Back to the top