20
20
import java .net .URI ;
21
21
22
22
import org .apache .http .client .HttpClient ;
23
+ import org .apache .http .client .config .RequestConfig ;
24
+ import org .apache .http .client .methods .Configurable ;
23
25
import org .apache .http .client .methods .HttpUriRequest ;
26
+ import org .apache .http .client .protocol .HttpClientContext ;
27
+ import org .apache .http .impl .client .CloseableHttpClient ;
28
+ import org .apache .http .impl .nio .client .CloseableHttpAsyncClient ;
24
29
import org .apache .http .impl .nio .client .HttpAsyncClients ;
25
30
import org .apache .http .nio .client .HttpAsyncClient ;
26
31
import org .apache .http .nio .reactor .IOReactorStatus ;
32
+ import org .apache .http .protocol .HttpContext ;
27
33
import org .springframework .beans .factory .InitializingBean ;
28
34
import org .springframework .http .HttpMethod ;
29
35
import org .springframework .util .Assert ;
@@ -41,23 +47,23 @@ public class HttpComponentsAsyncClientHttpRequestFactory
41
47
extends HttpComponentsClientHttpRequestFactory
42
48
implements AsyncClientHttpRequestFactory , InitializingBean {
43
49
44
- private HttpAsyncClient httpAsyncClient ;
50
+ private CloseableHttpAsyncClient httpAsyncClient ;
45
51
46
52
47
53
/**
48
54
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
49
55
* with a default {@link HttpAsyncClient} and {@link HttpClient}.
50
56
*/
51
57
public HttpComponentsAsyncClientHttpRequestFactory () {
52
- this (HttpAsyncClients .createDefault ());
58
+ this (HttpAsyncClients .createSystem ());
53
59
}
54
60
55
61
/**
56
62
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
57
63
* with the given {@link HttpAsyncClient} instance and a default {@link HttpClient}.
58
64
* @param httpAsyncClient the HttpAsyncClient instance to use for this request factory
59
65
*/
60
- public HttpComponentsAsyncClientHttpRequestFactory (HttpAsyncClient httpAsyncClient ) {
66
+ public HttpComponentsAsyncClientHttpRequestFactory (CloseableHttpAsyncClient httpAsyncClient ) {
61
67
super ();
62
68
Assert .notNull (httpAsyncClient , "'httpAsyncClient' must not be null" );
63
69
this .httpAsyncClient = httpAsyncClient ;
@@ -69,8 +75,8 @@ public HttpComponentsAsyncClientHttpRequestFactory(HttpAsyncClient httpAsyncClie
69
75
* @param httpClient the HttpClient instance to use for this request factory
70
76
* @param httpAsyncClient the HttpAsyncClient instance to use for this request factory
71
77
*/
72
- public HttpComponentsAsyncClientHttpRequestFactory (HttpClient httpClient ,
73
- HttpAsyncClient httpAsyncClient ) {
78
+ public HttpComponentsAsyncClientHttpRequestFactory (CloseableHttpClient httpClient ,
79
+ CloseableHttpAsyncClient httpAsyncClient ) {
74
80
super (httpClient );
75
81
Assert .notNull (httpAsyncClient , "'httpAsyncClient' must not be null" );
76
82
this .httpAsyncClient = httpAsyncClient ;
@@ -80,15 +86,15 @@ public HttpComponentsAsyncClientHttpRequestFactory(HttpClient httpClient,
80
86
* Set the {@code HttpClient} used for
81
87
* {@linkplain #createAsyncRequest(java.net.URI, org.springframework.http.HttpMethod) asynchronous execution}.
82
88
*/
83
- public void setHttpAsyncClient (HttpAsyncClient httpAsyncClient ) {
89
+ public void setHttpAsyncClient (CloseableHttpAsyncClient httpAsyncClient ) {
84
90
this .httpAsyncClient = httpAsyncClient ;
85
91
}
86
92
87
93
/**
88
94
* Return the {@code HttpClient} used for
89
95
* {@linkplain #createAsyncRequest(URI, HttpMethod) asynchronous execution}.
90
96
*/
91
- public HttpAsyncClient getHttpAsyncClient () {
97
+ public CloseableHttpAsyncClient getHttpAsyncClient () {
92
98
return httpAsyncClient ;
93
99
}
94
100
@@ -98,7 +104,7 @@ public void afterPropertiesSet() {
98
104
}
99
105
100
106
private void startAsyncClient () {
101
- HttpAsyncClient asyncClient = getHttpAsyncClient ();
107
+ CloseableHttpAsyncClient asyncClient = getHttpAsyncClient ();
102
108
if (asyncClient .getStatus () != IOReactorStatus .ACTIVE ) {
103
109
asyncClient .start ();
104
110
}
@@ -111,8 +117,23 @@ public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
111
117
startAsyncClient ();
112
118
HttpUriRequest httpRequest = createHttpUriRequest (httpMethod , uri );
113
119
postProcessHttpRequest (httpRequest );
114
- return new HttpComponentsAsyncClientHttpRequest (asyncClient , httpRequest ,
115
- createHttpContext (httpMethod , uri ));
120
+ HttpContext context = createHttpContext (httpMethod , uri );
121
+ if (context == null ) {
122
+ context = HttpClientContext .create ();
123
+ }
124
+ // Request configuration not set in the context
125
+ if (context .getAttribute (HttpClientContext .REQUEST_CONFIG ) == null ) {
126
+ // Use request configuration given by the user, when available
127
+ RequestConfig config = null ;
128
+ if (httpRequest instanceof Configurable ) {
129
+ config = ((Configurable ) httpRequest ).getConfig ();
130
+ }
131
+ if (config == null ) {
132
+ config = RequestConfig .DEFAULT ;
133
+ }
134
+ context .setAttribute (HttpClientContext .REQUEST_CONFIG , config );
135
+ }
136
+ return new HttpComponentsAsyncClientHttpRequest (asyncClient , httpRequest , context );
116
137
}
117
138
118
139
@ Override
@@ -121,7 +142,7 @@ public void destroy() throws Exception {
121
142
super .destroy ();
122
143
}
123
144
finally {
124
- getHttpAsyncClient ().shutdown ();
145
+ getHttpAsyncClient ().close ();
125
146
}
126
147
}
127
148
}
0 commit comments