Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
0.2-incubating
-
None
-
None
Description
org.apache.tamaya.etcd.EtcdAccessor should use tryWithResources when doing httpClient-calls. It's just a small improvement, that modernizes the code.
try-with-resources makes sure that the underlying HttpResponse is closed if the method is left.
Before
CloseableHttpResponse response = null; try{ ,.... response = httpclient.execute(get); } catch(final Exception e){ LOG.log(Level.INFO, "Error reading properties for '"+directory+"' from etcd: " + serverURL, e); result.put("_ERROR", "Error reading properties for '"+directory+"' from etcd: " + serverURL + ": " + e.toString()); } finally { if(response!=null){ try { response.close(); } catch (final IOException e) { LOG.log(Level.WARNING, "Failed to close http response", e); } } }
After
try { final HttpGet httpGet = new HttpGet(serverURL + "/v2/keys/"+key); httpGet.setConfig(RequestConfig.copy(RequestConfig.DEFAULT) .setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { .... SNIP ... } catch(final Exception e){ LOG.log(Level.INFO, "Error reading key '"+key+"' from etcd: " + serverURL, e); result.put("_ERROR", "Error reading key '"+key+"' from etcd: " + serverURL + ": " + e.toString()); }
IMHO this would improve readability of the class since this kind of exchange happens in nearly all methods of the class.