Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Is MqttClient thread safe?
Is MqttClient thread safe? [message #1693648] Mon, 27 April 2015 09:26 Go to next message
Shisei Hanai is currently offline Shisei HanaiFriend
Messages: 1
Registered: April 2015
Junior Member
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 12:52 Go to previous messageGo to next message
Ian Craggs is currently offline Ian CraggsFriend
Messages: 47
Registered: March 2013
Member
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 06:26 Go to previous message
paul stanton is currently offline paul stantonFriend
Messages: 82
Registered: July 2009
Member
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:Listening to token from connect call
Next Topic:Paho Javascript client
Goto Forum:
  


Current Time: Wed Nov 22 05:53:27 GMT 2017

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

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