AutoCloseable简单用法
解决方法:
public class RPCClient implements AutoCloseable {
private Connection connection;//假设这个连接使用完要关闭
public RPCClient() throws IOException, TimeoutException {
connection = factory.newConnection();
}
public static void main(String[] argv) {
try (RPCClient fibonacciRpc = new RPCClient()) {
//业务处理代码
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() throws IOException {
//执行完try catch中的代码后就会执行close方法,这里可以关闭你想关闭的资源
connection.close();
}
}