Handle subscribe exceptions
This commit is contained in:
@@ -8,8 +8,8 @@ public class Logger {
|
||||
}
|
||||
|
||||
public static void displayError(String message){
|
||||
String GREEN = "\u001B[32m";
|
||||
String RESET = "\u001B[0m";
|
||||
System.out.println(GREEN + "[Error] --- " + message + RESET);
|
||||
final String RED = "\u001B[31m";
|
||||
final String RESET = "\u001B[0m";
|
||||
System.out.println(RED + "[Error] --- " + message + RESET);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class MqttService {
|
||||
connect(subscriber, options);
|
||||
|
||||
subscriber.subscribe(topic, 1);
|
||||
} catch (MqttException e) {
|
||||
} catch (Exception e) {
|
||||
Logger.displayError("An error occurred while subscribing : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -101,12 +101,16 @@ public class MqttService {
|
||||
}
|
||||
|
||||
private void connect(MqttClient client, MqttConnectOptions options) throws MqttException {
|
||||
if (client.isConnected()) return;
|
||||
if (options == null) {
|
||||
client.connect();
|
||||
} else {
|
||||
client.connect(options);
|
||||
try {
|
||||
if (client.isConnected()) return;
|
||||
if (options == null) {
|
||||
client.connect();
|
||||
} else {
|
||||
client.connect(options);
|
||||
}
|
||||
Logger.displayInfo("Connected to " + client.getCurrentServerURI());
|
||||
} catch (Exception e){
|
||||
Logger.displayError("Unable to connect to broker : " + e.getMessage());
|
||||
}
|
||||
Logger.displayInfo("Connected to " + client.getCurrentServerURI());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user