将资源移出 try-with
Move resource out of try-with
我想创建一个 class 包装资源的实例。问题:当构造函数抛出时,资源丢失。我正试图为此找到解决方案。 Try-with-resource 是一个看起来不错的构造,但我无法将资源移出它。
例如,包装 HTTP 客户端的服务客户端:
class ServiceClient implements Closeable {
ServiceClient(ClosableHTTPClient client) { /* ... */ }
public close() { client.close() }
public ServiceClient create(String url) throws IOException {
try (ClosableHTTPClient client = createHttpClient(url)) {
return new ServiceClient(client);
} // make try-with do not close `client` on success
}
public ClosableHTTPClient createHttpClient(String url) {
return HttpClientBuilder.create()
.setConnectionManager(createClosableConnectionManager()) // must be closed, when `build` throws
.build();
}
}
构造函数抛出异常。
class Foo implements AutoClosable {
private final Bar someResource;
/**
* @param someResource whose ownership is taken over.
*/
Foo(Bar someResource) {
this.someResource = someResource;
try {
...
} catch (Throwable e) {
someResource.close(); // If this.close is not final.
throw e;
}
}
@Override public void close() { someResource.close(); }
...
唯一的解决办法是处理构造函数中抛出的任何异常。
事件标准 java 类 有时会忘记(具有负容量的 BufferedReader 不会关闭其包装的 reader)。
我想创建一个 class 包装资源的实例。问题:当构造函数抛出时,资源丢失。我正试图为此找到解决方案。 Try-with-resource 是一个看起来不错的构造,但我无法将资源移出它。
例如,包装 HTTP 客户端的服务客户端:
class ServiceClient implements Closeable {
ServiceClient(ClosableHTTPClient client) { /* ... */ }
public close() { client.close() }
public ServiceClient create(String url) throws IOException {
try (ClosableHTTPClient client = createHttpClient(url)) {
return new ServiceClient(client);
} // make try-with do not close `client` on success
}
public ClosableHTTPClient createHttpClient(String url) {
return HttpClientBuilder.create()
.setConnectionManager(createClosableConnectionManager()) // must be closed, when `build` throws
.build();
}
}
构造函数抛出异常。
class Foo implements AutoClosable {
private final Bar someResource;
/**
* @param someResource whose ownership is taken over.
*/
Foo(Bar someResource) {
this.someResource = someResource;
try {
...
} catch (Throwable e) {
someResource.close(); // If this.close is not final.
throw e;
}
}
@Override public void close() { someResource.close(); }
...
唯一的解决办法是处理构造函数中抛出的任何异常。 事件标准 java 类 有时会忘记(具有负容量的 BufferedReader 不会关闭其包装的 reader)。