Skip to content

Commit

Permalink
Merge pull request #1230 from amihaiemil/1229
Browse files Browse the repository at this point in the history
#1229 use and close our own ExecutorService for HTTP Requests
  • Loading branch information
amihaiemil committed Sep 29, 2021
2 parents ac4f1e2 + b9bbb98 commit 7156719
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions self-core-impl/src/main/java/com/selfxdsd/core/JsonResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -266,8 +269,9 @@ public Resource get(
final URI uri,
final Supplier<Map<String, List<String>>> headers
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
final HttpResponse<String> response = this.newHttpClient()
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
Expand All @@ -287,6 +291,8 @@ public Resource get(
"Couldn't GET [" + uri.toString() +"]",
ex
);
} finally {
exec.shutdownNow();
}
}

Expand All @@ -296,8 +302,9 @@ public Resource post(
final Supplier<Map<String, List<String>>> headers,
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
final HttpResponse<String> response = this.newHttpClient()
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
Expand All @@ -320,6 +327,8 @@ public Resource post(
+ " to [" + uri.toString() +"]",
ex
);
} finally {
exec.shutdownNow();
}
}

Expand All @@ -329,8 +338,9 @@ public Resource patch(
final Supplier<Map<String, List<String>>> headers,
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
final HttpResponse<String> response = this.newHttpClient()
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
Expand All @@ -353,6 +363,8 @@ public Resource patch(
+ " at [" + uri.toString() +"]",
ex
);
} finally {
exec.shutdownNow();
}
}

Expand All @@ -362,8 +374,9 @@ public Resource put(
final Supplier<Map<String, List<String>>> headers,
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
final HttpResponse<String> response = this.newHttpClient()
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
Expand All @@ -386,6 +399,8 @@ public Resource put(
+ " at [" + uri.toString() +"]",
ex
);
} finally {
exec.shutdownNow();
}
}

Expand All @@ -395,8 +410,9 @@ public Resource delete(
final Supplier<Map<String, List<String>>> headers,
final JsonValue body
) {
final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
final HttpResponse<String> response = this.newHttpClient()
final HttpResponse<String> response = this.newHttpClient(exec)
.send(
this.request(
uri,
Expand All @@ -419,6 +435,8 @@ public Resource delete(
+ " at [" + uri.toString() +"]",
ex
);
} finally {
exec.shutdownNow();
}
}

Expand Down Expand Up @@ -464,20 +482,25 @@ private HttpRequest request(

/**
* Creates a new http client.
* @param executor ExecutorService to use internally and close on end.
* @return HttpClient.
*/
private HttpClient newHttpClient() {
private HttpClient newHttpClient(final ExecutorService executor) {
final HttpClient client;
if (this.useOldHttpProtocol) {
client = HttpClient
.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(10))
.executor(executor)
.build();
} else {
client = HttpClient
.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(10))
.executor(executor)
.build();
}
return client;
Expand Down

0 comments on commit 7156719

Please sign in to comment.