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