55
55
import java .util .concurrent .CompletableFuture ;
56
56
import java .util .concurrent .CompletionStage ;
57
57
import java .util .concurrent .ExecutionException ;
58
+ import java .util .concurrent .ExecutorService ;
59
+ import java .util .concurrent .Executors ;
58
60
import java .util .concurrent .TimeUnit ;
59
61
import java .util .function .Supplier ;
60
62
import java .util .logging .Level ;
65
67
public class JdkHttpClient implements HttpClient {
66
68
public static final Logger LOG = Logger .getLogger (JdkHttpClient .class .getName ());
67
69
private final JdkHttpMessages messages ;
68
- private final java .net .http .HttpClient client ;
70
+ private java .net .http .HttpClient client ;
71
+ private WebSocket websocket ;
72
+ private final ExecutorService executorService ;
69
73
private final Duration readTimeout ;
70
74
71
75
JdkHttpClient (ClientConfig config ) {
@@ -74,9 +78,12 @@ public class JdkHttpClient implements HttpClient {
74
78
this .messages = new JdkHttpMessages (config );
75
79
this .readTimeout = config .readTimeout ();
76
80
81
+ executorService = Executors .newCachedThreadPool ();
82
+
77
83
java .net .http .HttpClient .Builder builder = java .net .http .HttpClient .newBuilder ()
78
84
.connectTimeout (config .connectionTimeout ())
79
- .followRedirects (ALWAYS );
85
+ .followRedirects (ALWAYS )
86
+ .executor (executorService );
80
87
81
88
Credentials credentials = config .credentials ();
82
89
String info = config .baseUri ().getUserInfo ();
@@ -196,7 +203,7 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) {
196
203
197
204
java .net .http .WebSocket underlyingSocket = webSocketCompletableFuture .join ();
198
205
199
- return new WebSocket () {
206
+ this . websocket = new WebSocket () {
200
207
@ Override
201
208
public WebSocket send (Message message ) {
202
209
Supplier <CompletableFuture <java .net .http .WebSocket >> makeCall ;
@@ -252,6 +259,7 @@ public void close() {
252
259
}
253
260
}
254
261
};
262
+ return this .websocket ;
255
263
}
256
264
257
265
private URI getWebSocketUri (HttpRequest request ) {
@@ -295,6 +303,15 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
295
303
296
304
}
297
305
306
+ @ Override
307
+ public void close () {
308
+ executorService .shutdownNow ();
309
+ if (this .websocket != null ) {
310
+ this .websocket .close ();
311
+ }
312
+ this .client = null ;
313
+ }
314
+
298
315
@ AutoService (HttpClient .Factory .class )
299
316
@ HttpClientName ("jdk-http-client" )
300
317
public static class Factory implements HttpClient .Factory {
0 commit comments