11package sample ;
22
3+ import com .jakewharton .retrofit .Ok3Client ;
34import com .segment .analytics .Analytics ;
45import com .segment .analytics .messages .TrackMessage ;
56import java .util .LinkedHashMap ;
67import java .util .Map ;
78import java .util .UUID ;
9+ import java .util .concurrent .TimeUnit ;
810import java .util .concurrent .atomic .AtomicInteger ;
11+ import okhttp3 .OkHttpClient ;
12+ import retrofit .client .Client ;
913
1014public class Main {
1115 public static void main (String ... args ) throws Exception {
1216 final BlockingFlush blockingFlush = BlockingFlush .create ();
1317
1418 // https://segment.com/segment-engineering/sources/test-java/debugger
1519 final Analytics analytics =
16- Analytics .builder ("xemyw6oe3n" ) //
20+ Analytics .builder ("xemyw6oe3n" )
1721 .plugin (blockingFlush .plugin ())
1822 .plugin (new LoggingPlugin ())
23+ .client (createClient ())
1924 .build ();
2025
2126 final String userId = System .getProperty ("user.name" );
@@ -27,9 +32,9 @@ public static void main(String... args) throws Exception {
2732 Map <String , Object > properties = new LinkedHashMap <>();
2833 properties .put ("count" , count .incrementAndGet ());
2934 analytics .enqueue (
30- TrackMessage .builder ("Java Test" ) //
31- .properties (properties ) //
32- .anonymousId (anonymousId ) //
35+ TrackMessage .builder ("Java Test" )
36+ .properties (properties )
37+ .anonymousId (anonymousId )
3338 .userId (userId ));
3439 }
3540 }
@@ -38,4 +43,19 @@ public static void main(String... args) throws Exception {
3843 blockingFlush .block ();
3944 analytics .shutdown ();
4045 }
46+
47+ /**
48+ * By default, the analytics client uses an HTTP client with sane defaults. However you can
49+ * customize the client to your needs. For instance, this client is configured to automatically
50+ * gzip outgoing requests.
51+ */
52+ private static Client createClient () {
53+ return new Ok3Client (
54+ new OkHttpClient .Builder ()
55+ .connectTimeout (15 , TimeUnit .SECONDS )
56+ .readTimeout (15 , TimeUnit .SECONDS )
57+ .writeTimeout (15 , TimeUnit .SECONDS )
58+ .addInterceptor (new GzipRequestInterceptor ())
59+ .build ());
60+ }
4161}
0 commit comments