12
12
import java .util .Collections ;
13
13
import java .util .List ;
14
14
import java .util .Map ;
15
+ import java .util .concurrent .TimeUnit ;
15
16
import java .util .concurrent .atomic .AtomicInteger ;
16
17
import javax .annotation .Nonnull ;
17
18
import javax .annotation .Nullable ;
31
32
import org .apache .http .protocol .HTTP ;
32
33
import org .apache .http .protocol .HttpContext ;
33
34
import org .mapfish .print .config .Configuration ;
35
+ import org .mapfish .print .servlet .job .impl .ThreadPoolJobManager ;
34
36
import org .slf4j .Logger ;
35
37
import org .slf4j .LoggerFactory ;
36
38
import org .springframework .http .HttpHeaders ;
@@ -52,30 +54,53 @@ public class MfClientHttpRequestFactoryImpl extends HttpComponentsClientHttpRequ
52
54
* @param maxConnTotal Maximum total connections.
53
55
* @param maxConnPerRoute Maximum connections per route.
54
56
*/
55
- public MfClientHttpRequestFactoryImpl (final int maxConnTotal , final int maxConnPerRoute ) {
56
- super (createHttpClient (maxConnTotal , maxConnPerRoute ));
57
+ public MfClientHttpRequestFactoryImpl (
58
+ final int maxConnTotal ,
59
+ final int maxConnPerRoute ,
60
+ final ThreadPoolJobManager threadPoolJobManager ) {
61
+ super (createHttpClient (maxConnTotal , maxConnPerRoute , threadPoolJobManager ));
57
62
}
58
63
59
64
@ Nullable
60
65
static Configuration getCurrentConfiguration () {
61
66
return CURRENT_CONFIGURATION .get ();
62
67
}
63
68
64
- private static int getIntProperty (final String name ) {
69
+ /**
70
+ * Return the number of milliseconds until the timeout Use the Automatic cancellation timeout if
71
+ * not defined.
72
+ *
73
+ * @param name timeout idemtifier
74
+ * @return the number of milliseconds until the timeout
75
+ */
76
+ private static int getTimeoutValue (
77
+ final String name , final ThreadPoolJobManager threadPoolJobManager ) {
65
78
final String value = System .getProperty (name );
66
79
if (value == null ) {
67
- return -1 ;
80
+ long millis = TimeUnit .SECONDS .toMillis (threadPoolJobManager .getTimeout ());
81
+ if (millis > Integer .MAX_VALUE ) {
82
+ LOGGER .warn (
83
+ "The value of {} is too large. The timeout will be set to the maximum value of {}" ,
84
+ name ,
85
+ Integer .MAX_VALUE );
86
+ return Integer .MAX_VALUE ;
87
+ } else {
88
+ return Integer .parseInt (Long .toString (millis ));
89
+ }
68
90
}
69
91
return Integer .parseInt (value );
70
92
}
71
93
72
94
private static CloseableHttpClient createHttpClient (
73
- final int maxConnTotal , final int maxConnPerRoute ) {
95
+ final int maxConnTotal ,
96
+ final int maxConnPerRoute ,
97
+ final ThreadPoolJobManager threadPoolJobManager ) {
74
98
final RequestConfig requestConfig =
75
99
RequestConfig .custom ()
76
- .setConnectionRequestTimeout (getIntProperty ("http.connectionRequestTimeout" ))
77
- .setConnectTimeout (getIntProperty ("http.connectTimeout" ))
78
- .setSocketTimeout (getIntProperty ("http.socketTimeout" ))
100
+ .setConnectionRequestTimeout (
101
+ getTimeoutValue ("http.connectionRequestTimeout" , threadPoolJobManager ))
102
+ .setConnectTimeout (getTimeoutValue ("http.connectTimeout" , threadPoolJobManager ))
103
+ .setSocketTimeout (getTimeoutValue ("http.socketTimeout" , threadPoolJobManager ))
79
104
.build ();
80
105
81
106
final HttpClientBuilder httpClientBuilder =
@@ -89,11 +114,19 @@ private static CloseableHttpClient createHttpClient(
89
114
.setMaxConnTotal (maxConnTotal )
90
115
.setMaxConnPerRoute (maxConnPerRoute )
91
116
.setUserAgent (UserAgentCreator .getUserAgent ());
92
- return httpClientBuilder .build ();
117
+ CloseableHttpClient closeableHttpClient = httpClientBuilder .build ();
118
+ LOGGER .debug (
119
+ "Created CloseableHttpClient using connectionRequestTimeout: {} connectTimeout: {}"
120
+ + " socketTimeout: {}" ,
121
+ getTimeoutValue ("http.connectionRequestTimeout" , threadPoolJobManager ),
122
+ getTimeoutValue ("http.connectTimeout" , threadPoolJobManager ),
123
+ getTimeoutValue ("http.socketTimeout" , threadPoolJobManager ));
124
+ return closeableHttpClient ;
93
125
}
94
126
95
127
// allow extension only for testing
96
128
@ Override
129
+ @ Nonnull
97
130
public ConfigurableRequest createRequest (
98
131
@ Nonnull final URI uri , @ Nonnull final HttpMethod httpMethod ) {
99
132
HttpRequestBase httpRequest = (HttpRequestBase ) createHttpUriRequest (httpMethod , uri );
@@ -161,20 +194,24 @@ public HttpMethod getMethod() {
161
194
return HttpMethod .valueOf (this .request .getMethod ());
162
195
}
163
196
197
+ @ Nonnull
164
198
@ Override
165
199
public String getMethodValue () {
166
200
return this .request .getMethod ();
167
201
}
168
202
203
+ @ Nonnull
169
204
public URI getURI () {
170
205
return this .request .getURI ();
171
206
}
172
207
208
+ @ Nonnull
173
209
@ Override
174
210
protected OutputStream getBodyInternal (@ Nonnull final HttpHeaders headers ) {
175
211
return this .outputStream ;
176
212
}
177
213
214
+ @ Nonnull
178
215
@ Override
179
216
protected Response executeInternal (@ Nonnull final HttpHeaders headers ) throws IOException {
180
217
CURRENT_CONFIGURATION .set (this .configuration );
@@ -207,7 +244,7 @@ protected Response executeInternal(@Nonnull final HttpHeaders headers) throws IO
207
244
}
208
245
}
209
246
210
- static class Response extends AbstractClientHttpResponse {
247
+ public static class Response extends AbstractClientHttpResponse {
211
248
private static final Logger LOGGER = LoggerFactory .getLogger (Response .class );
212
249
private static final AtomicInteger ID_COUNTER = new AtomicInteger ();
213
250
private final HttpResponse response ;
@@ -224,6 +261,7 @@ public int getRawStatusCode() {
224
261
return this .response .getStatusLine ().getStatusCode ();
225
262
}
226
263
264
+ @ Nonnull
227
265
@ Override
228
266
public String getStatusText () {
229
267
return this .response .getStatusLine ().getReasonPhrase ();
@@ -247,6 +285,7 @@ public void close() {
247
285
LOGGER .trace ("Closed Http Response object: {}" , this .id );
248
286
}
249
287
288
+ @ Nonnull
250
289
@ Override
251
290
public synchronized InputStream getBody () throws IOException {
252
291
if (this .inputStream == null ) {
@@ -262,6 +301,7 @@ public synchronized InputStream getBody() throws IOException {
262
301
return this .inputStream ;
263
302
}
264
303
304
+ @ Nonnull
265
305
@ Override
266
306
public HttpHeaders getHeaders () {
267
307
final HttpHeaders translatedHeaders = new HttpHeaders ();
0 commit comments