7
7
import com .segment .analytics .http .SegmentService ;
8
8
import com .segment .analytics .internal .AnalyticsClient ;
9
9
import com .segment .analytics .internal .AnalyticsVersion ;
10
+ import com .segment .analytics .internal .Config ;
11
+ import com .segment .analytics .internal .Config .FileConfig ;
12
+ import com .segment .analytics .internal .Config .HttpConfig ;
10
13
import com .segment .analytics .messages .Message ;
11
14
import com .segment .analytics .messages .MessageBuilder ;
15
+ import java .io .Closeable ;
16
+ import java .io .IOException ;
12
17
import java .util .ArrayList ;
13
18
import java .util .Arrays ;
14
19
import java .util .Collections ;
15
20
import java .util .Date ;
16
21
import java .util .List ;
17
22
import java .util .concurrent .ExecutorService ;
18
23
import java .util .concurrent .ThreadFactory ;
19
- import java .util .concurrent .TimeUnit ;
20
24
import okhttp3 .ConnectionSpec ;
21
25
import okhttp3 .HttpUrl ;
22
26
import okhttp3 .OkHttpClient ;
40
44
*
41
45
* @see <a href="https://Segment/">Segment</a>
42
46
*/
43
- public class Analytics {
47
+ public class Analytics implements Closeable {
44
48
private final AnalyticsClient client ;
45
49
private final List <MessageTransformer > messageTransformers ;
46
50
private final List <MessageInterceptor > messageInterceptors ;
@@ -90,9 +94,9 @@ public boolean offer(MessageBuilder builder) {
90
94
return client .offer (message );
91
95
}
92
96
93
- /** Stops this instance from processing further requests. */
94
- public void shutdown () {
95
- client .shutdown ();
97
+ /** Stops this instance from processing further requests. */
98
+ public void close () {
99
+ client .close ();
96
100
}
97
101
98
102
/**
@@ -125,7 +129,6 @@ public static class Builder {
125
129
private static final String DEFAULT_ENDPOINT = "https://api.segment.io" ;
126
130
private static final String DEFAULT_PATH = "/v1/import/" ;
127
131
private static final String DEFAULT_USER_AGENT = "analytics-java/" + AnalyticsVersion .get ();
128
- private static final int MESSAGE_QUEUE_MAX_BYTE_SIZE = 1024 * 500 ;
129
132
130
133
private final String writeKey ;
131
134
private OkHttpClient client ;
@@ -137,12 +140,10 @@ public static class Builder {
137
140
private List <MessageInterceptor > messageInterceptors ;
138
141
private ExecutorService networkExecutor ;
139
142
private ThreadFactory threadFactory ;
140
- private int flushQueueSize ;
141
- private int maximumFlushAttempts ;
142
- private long flushIntervalInMillis ;
143
- private int queueCapacity ;
144
143
private boolean forceTlsV1 = false ;
145
144
private GsonBuilder gsonBuilder ;
145
+ private HttpConfig httpConfig ;
146
+ private FileConfig fileConfig ;
146
147
147
148
Builder (String writeKey ) {
148
149
if (writeKey == null || writeKey .trim ().length () == 0 ) {
@@ -234,15 +235,6 @@ public Builder messageInterceptor(MessageInterceptor interceptor) {
234
235
return this ;
235
236
}
236
237
237
- /** Set queue capacity */
238
- public Builder queueCapacity (int capacity ) {
239
- if (capacity <= 0 ) {
240
- throw new IllegalArgumentException ("capacity should be positive." );
241
- }
242
- this .queueCapacity = capacity ;
243
- return this ;
244
- }
245
-
246
238
public Builder gsonBuilder (GsonBuilder gsonBuilder ) {
247
239
if (gsonBuilder == null ) {
248
240
throw new NullPointerException ("Null gsonBuilder" );
@@ -256,36 +248,6 @@ public Builder gsonBuilder(GsonBuilder gsonBuilder) {
256
248
return this ;
257
249
}
258
250
259
- /** Set the queueSize at which flushes should be triggered. */
260
- @ Beta
261
- public Builder flushQueueSize (int flushQueueSize ) {
262
- if (flushQueueSize < 1 ) {
263
- throw new IllegalArgumentException ("flushQueueSize must not be less than 1." );
264
- }
265
- this .flushQueueSize = flushQueueSize ;
266
- return this ;
267
- }
268
-
269
- /** Set the interval at which the queue should be flushed. */
270
- @ Beta
271
- public Builder flushInterval (long flushInterval , TimeUnit unit ) {
272
- long flushIntervalInMillis = unit .toMillis (flushInterval );
273
- if (flushIntervalInMillis < 1000 ) {
274
- throw new IllegalArgumentException ("flushInterval must not be less than 1 second." );
275
- }
276
- this .flushIntervalInMillis = flushIntervalInMillis ;
277
- return this ;
278
- }
279
-
280
- /** Set how many retries should happen before getting exhausted */
281
- public Builder retries (int maximumRetries ) {
282
- if (maximumRetries < 1 ) {
283
- throw new IllegalArgumentException ("retries must be at least 1" );
284
- }
285
- this .maximumFlushAttempts = maximumRetries ;
286
- return this ;
287
- }
288
-
289
251
/** Set the {@link ExecutorService} on which all HTTP requests will be made. */
290
252
public Builder networkExecutor (ExecutorService networkExecutor ) {
291
253
if (networkExecutor == null ) {
@@ -320,9 +282,22 @@ public Builder forceTlsVersion1() {
320
282
forceTlsV1 = true ;
321
283
return this ;
322
284
}
285
+
286
+ public Builder httpConfig (HttpConfig httpConfig ) {
287
+ this .httpConfig = httpConfig ;
288
+ return this ;
289
+ }
290
+ public Builder fileConfig (FileConfig fileConfig ) {
291
+ this .fileConfig = fileConfig ;
292
+ return this ;
293
+ }
323
294
324
- /** Create a {@link Analytics} client. */
325
- public Analytics build () {
295
+ /**
296
+ * Create a {@link Analytics} client.
297
+ *
298
+ * @throws IOException if cannot create the configured filePath directory
299
+ */
300
+ public Analytics build () throws IOException {
326
301
if (gsonBuilder == null ) {
327
302
gsonBuilder = new GsonBuilder ();
328
303
}
@@ -341,25 +316,9 @@ public Analytics build() {
341
316
}
342
317
}
343
318
344
- if (client == null ) {
345
- client = Platform .get ().defaultClient ();
346
- }
347
-
348
319
if (log == null ) {
349
320
log = Log .NONE ;
350
321
}
351
- if (flushIntervalInMillis == 0 ) {
352
- flushIntervalInMillis = Platform .get ().defaultFlushIntervalInMillis ();
353
- }
354
- if (queueCapacity == 0 ) {
355
- queueCapacity = Integer .MAX_VALUE ;
356
- }
357
- if (flushQueueSize == 0 ) {
358
- flushQueueSize = Platform .get ().defaultFlushQueueSize ();
359
- }
360
- if (maximumFlushAttempts == 0 ) {
361
- maximumFlushAttempts = 3 ;
362
- }
363
322
if (messageTransformers == null ) {
364
323
messageTransformers = Collections .emptyList ();
365
324
} else {
@@ -371,10 +330,19 @@ public Analytics build() {
371
330
messageInterceptors = Collections .unmodifiableList (messageInterceptors );
372
331
}
373
332
if (networkExecutor == null ) {
374
- networkExecutor = Platform . get () .defaultNetworkExecutor ();
333
+ networkExecutor = Config .defaultNetworkExecutor ();
375
334
}
376
335
if (threadFactory == null ) {
377
- threadFactory = Platform .get ().defaultThreadFactory ();
336
+ threadFactory = Config .defaultThreadFactory ();
337
+ }
338
+ if (client == null ) {
339
+ client = Config .defaultClient ();
340
+ }
341
+ if (httpConfig == null ) {
342
+ httpConfig = HttpConfig .builder ().build ();
343
+ }
344
+ if (fileConfig == null ) {
345
+ fileConfig = FileConfig .builder ().build ();
378
346
}
379
347
380
348
HttpLoggingInterceptor interceptor =
@@ -415,18 +383,7 @@ public void log(String message) {
415
383
416
384
SegmentService segmentService = restAdapter .create (SegmentService .class );
417
385
418
- AnalyticsClient analyticsClient = AnalyticsClient .create (
419
- endpoint ,
420
- segmentService ,
421
- queueCapacity ,
422
- flushQueueSize ,
423
- flushIntervalInMillis ,
424
- log ,
425
- threadFactory ,
426
- networkExecutor ,
427
- writeKey ,
428
- gson );
429
-
386
+ AnalyticsClient analyticsClient = new AnalyticsClient (endpoint , segmentService , log , threadFactory , networkExecutor , writeKey , gson , httpConfig , fileConfig );
430
387
return new Analytics (analyticsClient , messageTransformers , messageInterceptors , log );
431
388
}
432
389
}
0 commit comments