Skip to content

Commit

Permalink
#1240 client = null and call gc()
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Oct 14, 2021
1 parent 1dfaa98 commit 8f27722
Showing 1 changed file with 68 additions and 58 deletions.
126 changes: 68 additions & 58 deletions self-core-impl/src/main/java/com/selfxdsd/core/JsonResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,17 @@ public Resource get(
final Supplier<Map<String, List<String>>> headers
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
HttpClient client = this.newHttpClient(exec);
try {
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
"GET",
headers.get(),
HttpRequest.BodyPublishers.noBody()
),
HttpResponse.BodyHandlers.ofString()
);
final HttpResponse<String> response = client.send(
this.request(
uri,
"GET",
headers.get(),
HttpRequest.BodyPublishers.noBody()
),
HttpResponse.BodyHandlers.ofString()
);
return new JsonResponse(
response.statusCode(),
response.body(),
Expand All @@ -293,6 +293,8 @@ public Resource get(
);
} finally {
exec.shutdownNow();
client = null;
System.gc();
}
}

Expand All @@ -303,19 +305,19 @@ public Resource post(
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
HttpClient client = this.newHttpClient(exec);
try {
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
"POST",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
final HttpResponse<String> response = client.send(
this.request(
uri,
"POST",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
return new JsonResponse(
response.statusCode(),
response.body(),
Expand All @@ -329,6 +331,8 @@ public Resource post(
);
} finally {
exec.shutdownNow();
client = null;
System.gc();
}
}

Expand All @@ -339,19 +343,19 @@ public Resource patch(
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
HttpClient client = this.newHttpClient(exec);
try {
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
"PATCH",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
final HttpResponse<String> response = client.send(
this.request(
uri,
"PATCH",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
return new JsonResponse(
response.statusCode(),
response.body(),
Expand All @@ -365,6 +369,8 @@ public Resource patch(
);
} finally {
exec.shutdownNow();
client = null;
System.gc();
}
}

Expand All @@ -375,19 +381,19 @@ public Resource put(
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
HttpClient client = this.newHttpClient(exec);
try {
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
"PUT",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
final HttpResponse<String> response = client.send(
this.request(
uri,
"PUT",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
return new JsonResponse(
response.statusCode(),
response.body(),
Expand All @@ -401,6 +407,8 @@ public Resource put(
);
} finally {
exec.shutdownNow();
client = null;
System.gc();
}
}

Expand All @@ -411,19 +419,19 @@ public Resource delete(
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
HttpClient client = this.newHttpClient(exec);
try {
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
"DELETE",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
final HttpResponse<String> response = client.send(
this.request(
uri,
"DELETE",
headers.get(),
HttpRequest.BodyPublishers.ofString(
body.toString()
)
),
HttpResponse.BodyHandlers.ofString()
);
return new JsonResponse(
response.statusCode(),
response.body(),
Expand All @@ -437,6 +445,8 @@ public Resource delete(
);
} finally {
exec.shutdownNow();
client = null;
System.gc();
}
}

Expand Down

0 comments on commit 8f27722

Please sign in to comment.