33import com .fasterxml .jackson .core .JsonProcessingException ;
44import com .fasterxml .jackson .databind .DeserializationFeature ;
55import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import com .securenative .config .SecureNativeOptions ;
67import com .securenative .exceptions .SecureNativeHttpException ;
7- import com .securenative .exceptions .SecureNativeInvalidUriException ;
88import com .securenative .exceptions .SecureNativeParseException ;
99import com .securenative .exceptions .SecureNativeSDKException ;
1010import com .securenative .http .HttpClient ;
1111import com .securenative .http .HttpResponse ;
1212import com .securenative .models .Event ;
1313import com .securenative .models .RequestOptions ;
14- import com .securenative .config .SecureNativeOptions ;
1514
1615import java .io .IOException ;
17- import java .util .concurrent .*;
16+ import java .util .concurrent .ConcurrentLinkedQueue ;
17+ import java .util .concurrent .Executors ;
18+ import java .util .concurrent .ScheduledExecutorService ;
19+ import java .util .concurrent .TimeUnit ;
1820
1921
2022public class SecureNativeEventManager implements EventManager {
2123 private static final Logger logger = Logger .getLogger (SecureNativeEventManager .class );
22- private final int [] coefficients = new int [] { 1 , 1 , 2 , 3 , 5 , 8 , 13 };
24+ private final int [] coefficients = new int []{ 1 , 1 , 2 , 3 , 5 , 8 , 13 };
2325 private int attempt = 0 ;
2426 private Boolean sendEnabled = false ;
2527 private ScheduledExecutorService scheduler ;
@@ -54,7 +56,7 @@ public <T> T sendSync(Class<T> clazz, Event event, String url) throws IOExceptio
5456 String responseBody = response .getBody ();
5557 try {
5658 return mapper .readValue (responseBody , clazz );
57- }catch (Exception ex ){
59+ } catch (Exception ex ) {
5860 logger .error ("Failed to parse response body" , ex .getMessage ());
5961 throw new SecureNativeParseException (ex .getMessage ());
6062 }
@@ -77,10 +79,10 @@ public void sendAsync(Event event, String url, Boolean retry) {
7779
7880 private void sendEvents () throws InterruptedException {
7981 if (!this .events .isEmpty () && this .sendEnabled ) {
80- RequestOptions requestOptions = events .peek ();
82+ RequestOptions requestOptions = events .peek ();
8183 try {
8284 String body = requestOptions .getBody ();
83- HttpResponse resp = this .httpClient .post (requestOptions .getUrl (), body );
85+ HttpResponse resp = this .httpClient .post (requestOptions .getUrl (), body );
8486 if (resp .getStatusCode () == 401 ) {
8587 requestOptions .setRetry (false );
8688 }
@@ -93,15 +95,15 @@ private void sendEvents() throws InterruptedException {
9395 } catch (Exception ex ) {
9496 logger .error ("Failed to send event" , ex .getMessage ());
9597 if (requestOptions .getRetry ()) {
96- if (attempt ++ == coefficients .length ){
98+ if (attempt ++ == coefficients .length ) {
9799 attempt = 0 ;
98100 }
99101 int backoff = coefficients [attempt ] * this .options .getInterval ();
100102 logger .debug ("BackOff automatic sending by" , backoff );
101103 this .sendEnabled = false ;
102104 Thread .sleep (backoff );
103105 this .sendEnabled = true ;
104- }else {
106+ } else {
105107 // remove the event from queue, retry: false
106108 events .remove (requestOptions );
107109 }
@@ -119,7 +121,10 @@ public void startEventsPersist() {
119121 this .sendEnabled = true ;
120122 this .scheduler = Executors .newSingleThreadScheduledExecutor ();
121123 this .scheduler .scheduleWithFixedDelay (() -> {
122- try { sendEvents (); } catch (InterruptedException ignored ) { }
124+ try {
125+ sendEvents ();
126+ } catch (InterruptedException ignored ) {
127+ }
123128 }, 0 , this .options .getInterval (), TimeUnit .MILLISECONDS );
124129 }
125130
@@ -131,7 +136,8 @@ public void stopEventsPersist() {
131136 this .scheduler .shutdown ();
132137 // drain event queue
133138 this .scheduler .awaitTermination (this .options .getTimeout (), TimeUnit .MILLISECONDS );
134- } catch (InterruptedException ignored ) { }
139+ } catch (InterruptedException ignored ) {
140+ }
135141
136142 logger .debug ("Stopped event persistence" );
137143 }
0 commit comments