19
19
import java .io .IOException ;
20
20
import java .net .URI ;
21
21
import java .net .http .HttpClient ;
22
+ import java .time .Duration ;
22
23
import java .util .concurrent .Executor ;
23
24
24
25
import org .springframework .core .task .SimpleAsyncTaskExecutor ;
25
26
import org .springframework .http .HttpMethod ;
27
+ import org .springframework .lang .Nullable ;
26
28
import org .springframework .util .Assert ;
27
29
28
30
@@ -40,6 +42,9 @@ public class JdkClientHttpRequestFactory implements ClientHttpRequestFactory {
40
42
41
43
private final Executor executor ;
42
44
45
+ @ Nullable
46
+ private Duration readTimeout ;
47
+
43
48
44
49
/**
45
50
* Create a new instance of the {@code JdkClientHttpRequestFactory}
@@ -74,9 +79,20 @@ public JdkClientHttpRequestFactory(HttpClient httpClient, Executor executor) {
74
79
}
75
80
76
81
82
+ /**
83
+ * Set the underlying {@code HttpClient}'s read timeout (in milliseconds).
84
+ * A timeout value of 0 specifies an infinite timeout.
85
+ * <p>Default is the system's default timeout.
86
+ * @see java.net.http.HttpRequest.Builder#timeout
87
+ */
88
+ public void setReadTimeout (int readTimeout ) {
89
+ this .readTimeout = Duration .ofMillis (readTimeout );
90
+ }
91
+
92
+
77
93
@ Override
78
94
public ClientHttpRequest createRequest (URI uri , HttpMethod httpMethod ) throws IOException {
79
- return new JdkClientHttpRequest (this .httpClient , uri , httpMethod , this .executor );
95
+ return new JdkClientHttpRequest (this .httpClient , uri , httpMethod , this .executor , this . readTimeout );
80
96
}
81
97
82
98
}
0 commit comments