我有以下方法,它使用 Apache Commons Http 客户端向给定的 URI 发送异步 GET 并返回 Future 和响应。
CloseableHttpAsyncClient 实现 Closeable 因此我使用 try/resource 结构。
public static Future<HttpResponse> sendAsyncGet(String uri) throws IOException {
try (CloseableHttpAsyncClient asyncHttpClient = HttpAsyncClients.createDefault()) {
asyncHttpClient.start();
HttpGet httpGet = new HttpGet(uri);
return asyncHttpClient.execute(httpGet, null);
}
下面你可能会看到用法:
Future<HttpResponse> future = sendAsyncGet("http://www.apache.org");
future.get(3, TimeUnit.SECONDS);
问题是,当我在未来调用 get 时,它不会返回所需的 HttpResponse。如果我使用重载的 get() 方法,它会一直等到超时或永远。我想这是因为 try/resource 没有正确释放。
如何改进给定的方法/代码以便能够正确使用:Future with try/resource structure包含在方法主体中?
更新:
这是maven依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.1</version>
<scope>test</scope>
</dependency>