我正在尝试检查给定的IP和端口是否运行,如果没有,那么我必须将异常返回给客户端。
这是我的代码,
MqttMessage message2 = new MqttMessage();
MQTT mqtt_connect = new MQTT();
mqtt_connect.setHost(Host_Address, Integer.parseInt(port));
String topic = "/call/MQTT_Config";
mqtt_connect.setClientId("MQTT_Config");
mqtt_connect.setWillRetain(false);
mqtt_connect.isWillRetain();
mqtt_connect.setWillTopic(topic);
BlockingConnection m_publisher = mqtt_connect.blockingConnection();
m_publisher.connect();
if(connection establish){
// here how to check the condition
do publish here
}
else{
return "connection failure";
}
有人告诉我代码中的验证。
答案 0 :(得分:0)
在try / catch中包含您的代码,如下所示
try{
MqttMessage message2 = new MqttMessage();
MQTT mqtt_connect = new MQTT();
mqtt_connect.setHost(Host_Address, Integer.parseInt(port));
String topic = "/call/MQTT_Config";
mqtt_connect.setClientId("MQTT_Config");
mqtt_connect.setWillRetain(false);
mqtt_connect.isWillRetain();
mqtt_connect.setWillTopic(topic);
BlockingConnection m_publisher = mqtt_connect.blockingConnection();
m_publisher.connect();
}
catch(Exception e){
add message for connection not established
}