77import com .segment .analytics .http .SegmentService ;
88import com .segment .analytics .internal .AnalyticsClient ;
99import 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 ;
1013import com .segment .analytics .messages .Message ;
1114import com .segment .analytics .messages .MessageBuilder ;
15+ import java .io .Closeable ;
16+ import java .io .IOException ;
1217import java .util .ArrayList ;
1318import java .util .Arrays ;
1419import java .util .Collections ;
1520import java .util .Date ;
1621import java .util .List ;
1722import java .util .concurrent .ExecutorService ;
1823import java .util .concurrent .ThreadFactory ;
19- import java .util .concurrent .TimeUnit ;
2024import okhttp3 .ConnectionSpec ;
2125import okhttp3 .HttpUrl ;
2226import okhttp3 .OkHttpClient ;
4044 *
4145 * @see <a href="https://Segment/">Segment</a>
4246 */
43- public class Analytics {
47+ public class Analytics implements Closeable {
4448 private final AnalyticsClient client ;
4549 private final List <MessageTransformer > messageTransformers ;
4650 private final List <MessageInterceptor > messageInterceptors ;
@@ -90,9 +94,9 @@ public boolean offer(MessageBuilder builder) {
9094 return client .offer (message );
9195 }
9296
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 ();
96100 }
97101
98102 /**
@@ -125,7 +129,6 @@ public static class Builder {
125129 private static final String DEFAULT_ENDPOINT = "https://api.segment.io" ;
126130 private static final String DEFAULT_PATH = "/v1/import/" ;
127131 private static final String DEFAULT_USER_AGENT = "analytics-java/" + AnalyticsVersion .get ();
128- private static final int MESSAGE_QUEUE_MAX_BYTE_SIZE = 1024 * 500 ;
129132
130133 private final String writeKey ;
131134 private OkHttpClient client ;
@@ -137,12 +140,10 @@ public static class Builder {
137140 private List <MessageInterceptor > messageInterceptors ;
138141 private ExecutorService networkExecutor ;
139142 private ThreadFactory threadFactory ;
140- private int flushQueueSize ;
141- private int maximumFlushAttempts ;
142- private long flushIntervalInMillis ;
143- private int queueCapacity ;
144143 private boolean forceTlsV1 = false ;
145144 private GsonBuilder gsonBuilder ;
145+ private HttpConfig httpConfig ;
146+ private FileConfig fileConfig ;
146147
147148 Builder (String writeKey ) {
148149 if (writeKey == null || writeKey .trim ().length () == 0 ) {
@@ -234,15 +235,6 @@ public Builder messageInterceptor(MessageInterceptor interceptor) {
234235 return this ;
235236 }
236237
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-
246238 public Builder gsonBuilder (GsonBuilder gsonBuilder ) {
247239 if (gsonBuilder == null ) {
248240 throw new NullPointerException ("Null gsonBuilder" );
@@ -256,36 +248,6 @@ public Builder gsonBuilder(GsonBuilder gsonBuilder) {
256248 return this ;
257249 }
258250
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-
289251 /** Set the {@link ExecutorService} on which all HTTP requests will be made. */
290252 public Builder networkExecutor (ExecutorService networkExecutor ) {
291253 if (networkExecutor == null ) {
@@ -320,9 +282,22 @@ public Builder forceTlsVersion1() {
320282 forceTlsV1 = true ;
321283 return this ;
322284 }
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+ }
323294
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 {
326301 if (gsonBuilder == null ) {
327302 gsonBuilder = new GsonBuilder ();
328303 }
@@ -341,25 +316,9 @@ public Analytics build() {
341316 }
342317 }
343318
344- if (client == null ) {
345- client = Platform .get ().defaultClient ();
346- }
347-
348319 if (log == null ) {
349320 log = Log .NONE ;
350321 }
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- }
363322 if (messageTransformers == null ) {
364323 messageTransformers = Collections .emptyList ();
365324 } else {
@@ -371,10 +330,19 @@ public Analytics build() {
371330 messageInterceptors = Collections .unmodifiableList (messageInterceptors );
372331 }
373332 if (networkExecutor == null ) {
374- networkExecutor = Platform . get () .defaultNetworkExecutor ();
333+ networkExecutor = Config .defaultNetworkExecutor ();
375334 }
376335 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 ();
378346 }
379347
380348 HttpLoggingInterceptor interceptor =
@@ -415,18 +383,7 @@ public void log(String message) {
415383
416384 SegmentService segmentService = restAdapter .create (SegmentService .class );
417385
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 );
430387 return new Analytics (analyticsClient , messageTransformers , messageInterceptors , log );
431388 }
432389 }
0 commit comments