Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 3 additions & 5 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 @@ -177,8 +177,7 @@ protected void close()
}
catch (Exception ex)
{
logger.log(Level.WARNING, "Failed to unsubscribe PV from topic " + topicStr);
ex.printStackTrace();
logger.log(Level.WARNING, "Failed to unsubscribe PV from topic " + topicStr, ex);
}
}

Expand All @@ -205,9 +204,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(), ex);
}
}

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