diff options
| author | Chris Lu <chris.lu@gmail.com> | 2018-11-26 00:00:55 -0800 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2018-11-26 00:00:55 -0800 |
| commit | b089f1a49272badc23af3af97b40e6433731c0fd (patch) | |
| tree | 3c043549d208425c4b33c3681a7408c2305c3cdd | |
| parent | 11eb014311144e8b3e450c70fb3b1877bd02d534 (diff) | |
| download | seaweedfs-b089f1a49272badc23af3af97b40e6433731c0fd.tar.xz seaweedfs-b089f1a49272badc23af3af97b40e6433731c0fd.zip | |
SeaweedWrite close http client
| -rw-r--r-- | other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedWrite.java | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedWrite.java b/other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedWrite.java index 227c9bde8..a360d828a 100644 --- a/other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedWrite.java +++ b/other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedWrite.java @@ -2,10 +2,10 @@ package seaweed.hdfs; import org.apache.hadoop.fs.Path; import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import seaweedfs.client.FilerGrpcClient; import seaweedfs.client.FilerProto; @@ -60,7 +60,7 @@ public class SeaweedWrite { final byte[] bytes, final long bytesOffset, final long bytesLength) throws IOException { - HttpClient client = HttpClientBuilder.create().setUserAgent("hdfs-client").build(); + CloseableHttpClient client = HttpClientBuilder.create().setUserAgent("hdfs-client").build(); InputStream inputStream = new ByteArrayInputStream(bytes, (int) bytesOffset, (int) bytesLength); @@ -71,14 +71,19 @@ public class SeaweedWrite { .addBinaryBody("upload", inputStream) .build()); - HttpResponse response = client.execute(post); + try { + HttpResponse response = client.execute(post); - String etag = response.getLastHeader("ETag").getValue(); + String etag = response.getLastHeader("ETag").getValue(); - if (etag != null && etag.startsWith("\"") && etag.endsWith("\"")) { - etag = etag.substring(1, etag.length() - 1); + if (etag != null && etag.startsWith("\"") && etag.endsWith("\"")) { + etag = etag.substring(1, etag.length() - 1); + } + + return etag; + } finally { + client.close(); } - return etag; } } |
