18
18
19
19
import java .net .URI ;
20
20
import java .net .URISyntaxException ;
21
+ import java .time .Duration ;
21
22
import java .util .Map ;
22
- import java .util .concurrent .TimeUnit ;
23
23
24
24
import org .apache .hc .client5 .http .HttpRoute ;
25
25
import org .apache .hc .client5 .http .auth .AuthScope ;
31
31
import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
32
32
import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
33
33
import org .apache .hc .core5 .http .HttpHost ;
34
-
34
+ import org . apache . hc . core5 . util . Timeout ;
35
35
import org .springframework .beans .factory .FactoryBean ;
36
36
37
37
/**
38
- * {@code FactoryBean} to set up a <a href="http://hc.apache.org/httpcomponents-client">Apache
39
- * CloseableHttpClient</a>
38
+ * {@link FactoryBean} to set up a {@link CloseableHttpClient} using HttpComponents HttpClient 5.
40
39
*
40
+ * @see http://hc.apache.org/httpcomponents-client
41
41
* @author Lars Uffmann
42
42
* @since 4.0.5
43
43
*/
44
44
public class HttpComponents5ClientFactory implements FactoryBean <CloseableHttpClient > {
45
- private static final int DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS = (60 * 1000 );
46
45
47
- private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS ;
48
- private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000 );
46
+ private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration .ofSeconds (60 );
47
+
48
+ private static final Duration DEFAULT_READ_TIMEOUT = Duration .ofSeconds (60 );
49
+
50
+ private Duration connectionTimeout = DEFAULT_CONNECTION_TIMEOUT ;
49
51
50
- private int readTimeout = DEFAULT_READ_TIMEOUT_MILLISECONDS ;
52
+ private Duration readTimeout = DEFAULT_READ_TIMEOUT ;
51
53
52
54
private int maxTotalConnections = -1 ;
55
+
53
56
private AuthScope authScope = null ;
54
57
55
58
private Credentials credentials = null ;
@@ -86,24 +89,28 @@ public void setAuthScope(AuthScope authScope) {
86
89
/**
87
90
* Sets the timeout until a connection is established. A value of 0 means <em>never</em> timeout.
88
91
*
89
- * @param timeout the timeout value in milliseconds
92
+ * @param timeout the timeout value
90
93
*/
91
- public void setConnectionTimeout (int timeout ) {
92
- if (timeout < 0 ) {
94
+ public void setConnectionTimeout (Duration timeout ) {
95
+
96
+ if (timeout .isNegative ()) {
93
97
throw new IllegalArgumentException ("timeout must be a non-negative value" );
94
98
}
99
+
95
100
this .connectionTimeout = timeout ;
96
101
}
97
102
98
103
/**
99
104
* Set the socket read timeout for the underlying HttpClient. A value of 0 means <em>never</em> timeout.
100
105
*
101
- * @param timeout the timeout value in milliseconds
106
+ * @param timeout the timeout value
102
107
*/
103
- public void setReadTimeout (int timeout ) {
104
- if (timeout < 0 ) {
108
+ public void setReadTimeout (Duration timeout ) {
109
+
110
+ if (timeout .isNegative ()) {
105
111
throw new IllegalArgumentException ("timeout must be a non-negative value" );
106
112
}
113
+
107
114
this .readTimeout = timeout ;
108
115
}
109
116
@@ -114,9 +121,11 @@ public void setReadTimeout(int timeout) {
114
121
* @see PoolingHttpClientConnectionManager...
115
122
*/
116
123
public void setMaxTotalConnections (int maxTotalConnections ) {
124
+
117
125
if (maxTotalConnections <= 0 ) {
118
126
throw new IllegalArgumentException ("maxTotalConnections must be a positive value" );
119
127
}
128
+
120
129
this .maxTotalConnections = maxTotalConnections ;
121
130
}
122
131
@@ -142,14 +151,14 @@ public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost)
142
151
void applyMaxConnectionsPerHost (PoolingHttpClientConnectionManager connectionManager ) throws URISyntaxException {
143
152
144
153
for (Map .Entry <String , String > entry : maxConnectionsPerHost .entrySet ()) {
154
+
145
155
URI uri = new URI (entry .getKey ());
146
156
HttpHost host = new HttpHost (uri .getScheme (), uri .getHost (), getPort (uri ));
147
157
final HttpRoute route ;
148
158
149
159
if (uri .getScheme ().equals ("https" )) {
150
160
route = new HttpRoute (host , null , true );
151
- }
152
- else {
161
+ } else {
153
162
route = new HttpRoute (host );
154
163
}
155
164
int max = Integer .parseInt (entry .getValue ());
@@ -158,14 +167,17 @@ void applyMaxConnectionsPerHost(PoolingHttpClientConnectionManager connectionMan
158
167
}
159
168
160
169
static int getPort (URI uri ) {
170
+
161
171
if (uri .getPort () == -1 ) {
172
+
162
173
if ("https" .equalsIgnoreCase (uri .getScheme ())) {
163
174
return 443 ;
164
175
}
165
176
if ("http" .equalsIgnoreCase (uri .getScheme ())) {
166
177
return 80 ;
167
178
}
168
179
}
180
+
169
181
return uri .getPort ();
170
182
}
171
183
@@ -176,34 +188,38 @@ public boolean isSingleton() {
176
188
177
189
@ Override
178
190
public CloseableHttpClient getObject () throws Exception {
179
- PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder .create ();
191
+
192
+ PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder
193
+ .create ();
194
+
180
195
if (this .maxTotalConnections != -1 ) {
181
196
connectionManagerBuilder .setMaxConnTotal (this .maxTotalConnections );
182
197
}
183
198
184
- if (null != this . connectionManagerBuilderCustomizer ) {
199
+ if (this . connectionManagerBuilderCustomizer != null ) {
185
200
this .connectionManagerBuilderCustomizer .customize (connectionManagerBuilder );
186
201
}
187
202
188
203
this .connectionManager = connectionManagerBuilder .build ();
189
204
190
205
applyMaxConnectionsPerHost (connectionManager );
191
206
192
- RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ()
193
- .setConnectTimeout ( connectionTimeout , TimeUnit . MILLISECONDS )
194
- .setResponseTimeout (readTimeout , TimeUnit . MILLISECONDS );
207
+ RequestConfig .Builder requestConfigBuilder = RequestConfig .custom () //
208
+ .setConnectionRequestTimeout ( Timeout . of ( connectionTimeout )) //
209
+ .setResponseTimeout (Timeout . of ( readTimeout ) );
195
210
196
- HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ()
197
- .setDefaultRequestConfig (requestConfigBuilder .build ())
211
+ HttpClientBuilder httpClientBuilder = HttpClientBuilder .create () //
212
+ .setDefaultRequestConfig (requestConfigBuilder .build ()) //
198
213
.setConnectionManager (connectionManager );
199
214
200
- if (null != credentials && null != authScope ) {
215
+ if (credentials != null && authScope != null ) {
216
+
201
217
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider ();
202
218
basicCredentialsProvider .setCredentials (authScope , credentials );
203
219
httpClientBuilder .setDefaultCredentialsProvider (basicCredentialsProvider );
204
220
}
205
221
206
- if (null != this . clientBuilderCustomizer ) {
222
+ if (this . clientBuilderCustomizer != null ) {
207
223
clientBuilderCustomizer .customize (httpClientBuilder );
208
224
}
209
225
@@ -223,7 +239,8 @@ public void setClientBuilderCustomizer(HttpClientBuilderCustomizer clientBuilder
223
239
this .clientBuilderCustomizer = clientBuilderCustomizer ;
224
240
}
225
241
226
- public void setConnectionManagerBuilderCustomizer (PoolingHttpClientConnectionManagerBuilderCustomizer connectionManagerBuilderCustomizer ) {
242
+ public void setConnectionManagerBuilderCustomizer (
243
+ PoolingHttpClientConnectionManagerBuilderCustomizer connectionManagerBuilderCustomizer ) {
227
244
this .connectionManagerBuilderCustomizer = connectionManagerBuilderCustomizer ;
228
245
}
229
246
0 commit comments