Skip to content

Commit b6b5366

Browse files
poutsmajhoeller
authored andcommitted
Make HttpComponentsAsyncClientHttpRequest abortable
This commit aborts the HttpComponentsAsyncClientHttpRequest whenever the returned Future is canceled. Issue: SPR-14845 (cherry picked from commit 8f84446)
1 parent edf1df3 commit b6b5366

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders heade
9292
entityEnclosingRequest.setEntity(requestEntity);
9393
}
9494

95-
final HttpResponseFutureCallback callback = new HttpResponseFutureCallback();
95+
final HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
9696
final Future<HttpResponse> futureResponse =
9797
this.httpClient.execute(this.httpRequest, this.httpContext, callback);
9898
return new ClientHttpResponseFuture(futureResponse, callback);
@@ -101,9 +101,15 @@ protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders heade
101101

102102
private static class HttpResponseFutureCallback implements FutureCallback<HttpResponse> {
103103

104+
private final HttpUriRequest httpRequest;
105+
104106
private final ListenableFutureCallbackRegistry<ClientHttpResponse> callbacks =
105107
new ListenableFutureCallbackRegistry<ClientHttpResponse>();
106108

109+
public HttpResponseFutureCallback(HttpUriRequest httpRequest) {
110+
this.httpRequest = httpRequest;
111+
}
112+
107113
public void addCallback(ListenableFutureCallback<? super ClientHttpResponse> callback) {
108114
this.callbacks.addCallback(callback);
109115
}
@@ -128,6 +134,7 @@ public void failed(Exception ex) {
128134

129135
@Override
130136
public void cancelled() {
137+
this.httpRequest.abort();
131138
}
132139
}
133140

spring-web/src/test/java/org/springframework/http/client/AbstractAsyncHttpRequestFactoryTestCase.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -222,4 +222,14 @@ protected void assertHttpMethod(String path, HttpMethod method) throws Exception
222222
}
223223
}
224224

225+
@Test
226+
public void cancel() throws Exception {
227+
URI uri = new URI(baseUrl + "/status/notfound");
228+
AsyncClientHttpRequest request = this.factory.createAsyncRequest(uri, HttpMethod.GET);
229+
Future<ClientHttpResponse> futureResponse = request.executeAsync();
230+
futureResponse.cancel(true);
231+
assertTrue(futureResponse.isCancelled());
232+
}
233+
234+
225235
}

0 commit comments

Comments
 (0)