public MQTTClient(MQTTConnectionInfo conn, ConnectionMgr conmgr) throws Exception {
try {
clientId = conn.getClientId();
mqttClient = ClientFactory.getConnection(conn, conmgr);
username =conn.getUsername();
log.info("-- inside MqttClient == While Connecting : "+username.toString() + mqttClient.getState());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}此资源并非在所有情况下都被正确关闭和释放。如何关闭getConnection()
发布于 2021-12-13 19:55:12
您可以使用在尝试或捕获之后执行的finally块:
try {
mqttClient = ClientFactory.getConnection(conn, conmgr);
...
} catch (Exception e) {
...
} finally {
mqttClient.close();
}或者,如果客户端类实现了java.lang.AutoClosable,则可以使用一个try-with语句:
try (MQTTClient mqttClient = ClientFactory.getConnection(conn, conmgr)) {
mqttClient.doSomething()
} catch (Exception e) {
...
}https://stackoverflow.com/questions/70258494
复制相似问题