subscribe in connectComplete causing race condition when there are messages [message #1866690] |
Wed, 05 June 2024 03:10 |
Eclipse User |
|
|
|
While using paho 1.2.5 with MqttCallbackExtended, when calling client.subscribe(topic) in the connectComplete callback is resulting in the system hanging (possible race condition) when there are offline messages to be delivered.
If I move the subscribe method to connect() then it works fine.
Scenario is like this.
# Have mosquitto/MQTT broker running with persistence enabled
# My subcription is Implementing MqttCallbackExtended
# Using setCleanSession(false) to get offline message upon connect back
# Calling subscribe(topic) inside connectComplete
# When the offline messages are less than 10 messages then it works, if it is more then it hangs/gets into a race condition and never be able to deliver offline messages nor new messages.
I don't know all the ill effects of using subscribe within connect() than connectComplete.
I saw from the source the below code causing this problem. If someone can help address/fix this, it would be great.
Class: org.eclipse.paho.client.mqttv3.internal.CommsCallback
public void messageArrived(MqttPublish sendMessage) {
final String methodName = "messageArrived";
if (mqttCallback != null || !callbacksWildcards.isEmpty() || !callbacksDirect.isEmpty()) {
// If we already have enough messages queued up in memory, wait
// until some more queue space becomes available. This helps
// the client protect itself from getting flooded by messages
// from the server.
synchronized (spaceAvailable) {
while (isRunning() && !isQuiescing() && messageQueue.size() >= INBOUND_QUEUE_SIZE) {
try {
// @TRACE 709=wait for spaceAvailable
log.fine(CLASS_NAME, methodName, "709");
spaceAvailable.wait(200);
} catch (InterruptedException ex) {
}
}
}
if (!isQuiescing()) {
messageQueue.addElement(sendMessage);
// Notify the CommsCallback thread that there's work to do...
synchronized (workAvailable) {
// @TRACE 710=new msg avail, notify workAvailable
log.fine(CLASS_NAME, methodName, "710");
workAvailable.notifyAll();
}
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03580 seconds