Uploaded image for project: 'Tamaya'
  1. Tamaya
  2. TAMAYA-141

org.apache.tamaya.etcd.EtcdAccessor should use try with resources when performing http requests

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 0.2-incubating
    • Fix Version/s: 0.2-incubating
    • Component/s: None
    • Labels:
      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.

        Attachments

        1. TAMAYA-141.diff
          37 kB
          Philipp Ottlinger

          Activity

            People

            • Assignee:
              pottlinger Philipp Ottlinger
              Reporter:
              pottlinger Philipp Ottlinger
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: