Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Enhance MQTT client connection status check. #3309

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions core/pv-mqtt/src/main/java/org/phoebus/pv/mqtt/MQTT_PV.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ public void messageArrived(String topic, MqttMessage msg) throws Exception
}
catch (Exception ex)
{
logger.log(Level.SEVERE, "Could not parse message: '" + new_value + "' to " + getName());

throw new Exception("Failed to parse message", ex);
notifyListenersOfDisconnect();
logger.log(Level.WARNING, "Could not parse message: '" + new_value + "' to " + getName());
}
}

Expand Down
25 changes: 11 additions & 14 deletions core/pv-mqtt/src/main/java/org/phoebus/pv/mqtt/MQTT_PVConn.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
Expand All @@ -34,8 +33,6 @@
@SuppressWarnings("nls")
public class MQTT_PVConn implements MqttCallback
{
private final AtomicBoolean connected = new AtomicBoolean();

MqttClient myClient;
MqttConnectOptions connOpt;

Expand Down Expand Up @@ -158,16 +155,15 @@ public void publishTopic(String topicStr, String pubMsg, int pubQoS, boolean ret
}
}

private void disconnect()
private synchronized void disconnect()
{
if (! connected.compareAndSet(true, false))
return; // Already disconnected
if (!myClient.isConnected())
{
return;
}

try
{
if (! myClient.isConnected())
throw new Exception("Already disconnected");

// wait to ensure subscribed messages are delivered
Thread.sleep(100);
myClient.disconnect();
Expand All @@ -178,10 +174,12 @@ private void disconnect()
}
}

private boolean connect()
private synchronized boolean connect()
{
if (! connected.compareAndSet(false, true))
return true; // Already connected
if (myClient != null && myClient.isConnected())
{
return true;
}

generateClientID();
setOptions();
Expand All @@ -196,10 +194,9 @@ private boolean connect()
catch (MqttException ex)
{
PV.logger.log(Level.SEVERE, "Could not connect to MQTT broker " + brokerURL, ex);
connected.set(false);
}

return connected.get();
return myClient.isConnected();
}

private void generateClientID()
Expand Down