32
32
import java .util .concurrent .TimeUnit ;
33
33
34
34
/**
35
- * HttpProjectConfigManager is an implementation of a ProjectConfigManager
35
+ * HttpProjectConfigManager is an implementation of a {@link PollingProjectConfigManager}
36
36
* backed by a datafile. Currently this is loosely tied to Apache HttpClient
37
37
* implementation which is the client of choice in this package.
38
- *
39
- * Note that this implementation is blocking and stateless. This is best used in
40
- * conjunction with the {@link PollingProjectConfigManager} to provide caching
41
- * and asynchronous fetching.
42
38
*/
43
39
public class HttpProjectConfigManager extends PollingProjectConfigManager {
44
40
@@ -48,8 +44,8 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
48
44
private final URI uri ;
49
45
private String datafileLastModified ;
50
46
51
- private HttpProjectConfigManager (long period , TimeUnit timeUnit , OptimizelyHttpClient httpClient , String url ) {
52
- super (period , timeUnit );
47
+ private HttpProjectConfigManager (long period , TimeUnit timeUnit , OptimizelyHttpClient httpClient , String url , long blockingTimeoutPeriod , TimeUnit blockingTimeoutUnit ) {
48
+ super (period , timeUnit , blockingTimeoutPeriod , blockingTimeoutUnit );
53
49
this .httpClient = httpClient ;
54
50
this .uri = URI .create (url );
55
51
}
@@ -128,9 +124,13 @@ public static class Builder {
128
124
private String url ;
129
125
private String format = "https://cdn.optimizely.com/datafiles/%s.json" ;
130
126
private OptimizelyHttpClient httpClient ;
127
+
131
128
private long period = 5 ;
132
129
private TimeUnit timeUnit = TimeUnit .MINUTES ;
133
130
131
+ private long blockingTimeoutPeriod = 10 ;
132
+ private TimeUnit blockingTimeoutUnit = TimeUnit .SECONDS ;
133
+
134
134
public Builder withDatafile (String datafile ) {
135
135
this .datafile = datafile ;
136
136
return this ;
@@ -156,6 +156,23 @@ public Builder withOptimizelyHttpClient(OptimizelyHttpClient httpClient) {
156
156
return this ;
157
157
}
158
158
159
+ /**
160
+ * Configure time to block before Completing the future. This timeout is used on the first call
161
+ * to {@link PollingProjectConfigManager#getConfig()}. If the timeout is exceeded then the
162
+ * PollingProjectConfigManager will begin returning null immediately until the call to Poll
163
+ * succeeds.
164
+ */
165
+ public Builder withBlockingTimeout (long period , TimeUnit timeUnit ) {
166
+ if (timeUnit == null ) {
167
+ throw new NullPointerException ("Must provide valid timeUnit" );
168
+ }
169
+
170
+ this .blockingTimeoutPeriod = period ;
171
+ this .blockingTimeoutUnit = timeUnit ;
172
+
173
+ return this ;
174
+ }
175
+
159
176
public Builder withPollingInterval (long period , TimeUnit timeUnit ) {
160
177
if (timeUnit == null ) {
161
178
throw new NullPointerException ("Must provide valid timeUnit" );
@@ -186,17 +203,15 @@ public HttpProjectConfigManager build(boolean defer) {
186
203
httpClient = HttpClientUtils .getDefaultHttpClient ();
187
204
}
188
205
189
- if (url != null ) {
190
- return new HttpProjectConfigManager (period , timeUnit , httpClient , url );
191
- }
206
+ if (url == null ) {
207
+ if (sdkKey == null ) {
208
+ throw new NullPointerException ("sdkKey cannot be null" );
209
+ }
192
210
193
- if (sdkKey == null ) {
194
- throw new NullPointerException ("sdkKey cannot be null" );
211
+ url = String .format (format , sdkKey );
195
212
}
196
213
197
- url = String .format (format , sdkKey );
198
-
199
- HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager (period , timeUnit , httpClient , url );
214
+ HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager (period , timeUnit , httpClient , url , blockingTimeoutPeriod , blockingTimeoutUnit );
200
215
201
216
if (datafile != null ) {
202
217
try {
0 commit comments