I have an application that continues to call api for json data, and pretty quickly I saw that this was an exception waiting for a connection from the pool, I googled around and found that the connection was leaking due to non-consuming content, so I updated the code to close the input stream after consuming jsondata, but still got this connection timeout exception, here is my code:
InputStream is = ApiUtil.getAsStream(Api.get(bookUrl).param("limit","500").param("bookId", bid).enable(Options.LongRunning), 3); List<JsonBook> books = mapper.readValue(is, BOOK_TYPE); is.close() Api: private JsonHttpClient client; public APIForGet get(String endpoint) { return new APIForGet(this.client, endpoint); } ApiUtil: public static InputStream getAsStream(APIForGet get, Iterable<Long>retries) { return get.asStream(); } APIForGet: private JsonHttpClient client; public InputStream asStream() { return this.client.getAsStream(this.hostname, this.port, this.endpoint, params, optionsArray()); } JsonHttpClientImpl: public InputStream getAsStream(Optional<String> host, Optional<Integer> port,String path, Multimap<String, String> param, Options ... options) { HttpResponse reponse; try { response = request.execute(); if (response.isSuccessStatusCode()) { return response.getContent(); } } catch (Exception e) { throw new Exception(); } }
the wrapping logic here is pretty complicated, but in the end I think closing the input stream should work, any thoughts? Thanks!
source share