@@ -839,6 +839,8 @@ public final class ChronosLogHandler {
839839
840840 final AtomicInteger sequenceNumber = new AtomicInteger ();
841841
842+ final static int MAX_RETRIES = 1 ;
843+
842844
843845 public ChronosLogHandler ( final ChronosJob job ) {
844846 this .executor = Executors .newSingleThreadExecutor ();
@@ -855,22 +857,36 @@ public void publish( String message ) {
855857 }
856858
857859 pendingMessages .add ( executor .submit ( () -> {
858- try {
859- parameters .clear ();
860- parameters .put ( "recordSequenceNumber" , sequenceNumber .incrementAndGet () );
861- parameters .put ( "log" , message );
862-
863- final JSONObject jsonResponse = Unirest .post ( getUrl ( address , port , ChronosRestApi .JOB , query ) ).fields ( parameters ).asJson ().getBody ().getObject ();
864- final JSONObject status = jsonResponse .getJSONObject ( ChronosRestApi .STATUS_OBJECT_KEY );
865-
866- if ( status .getInt ( ChronosRestApi .STATUS_CODE_KEY ) != ChronosRestApi .STATUS_CODE__SUCCESS ) {
867- log .warn ( "Service returned: {}: {}" ,
868- status .getInt ( ChronosRestApi .STATUS_CODE_KEY ),
869- status .getString ( ChronosRestApi .STATUS_MESSAGE_KEY ) );
860+ int attempt = 0 ;
861+ while ( attempt < MAX_RETRIES ) {
862+ try {
863+ parameters .clear ();
864+ parameters .put ( "recordSequenceNumber" , sequenceNumber .incrementAndGet () );
865+ parameters .put ( "log" , message );
866+
867+ JSONObject jsonResponse = Unirest .post ( getUrl ( address , port , ChronosRestApi .JOB , query ) )
868+ .fields ( parameters ).asJson ().getBody ().getObject ();
869+ JSONObject status = jsonResponse .getJSONObject ( ChronosRestApi .STATUS_OBJECT_KEY );
870+
871+ if ( status .getInt ( ChronosRestApi .STATUS_CODE_KEY ) == ChronosRestApi .STATUS_CODE__SUCCESS ) {
872+ return ; // Success, exit retry loop
873+ } else {
874+ log .warn ( "Service returned: {}: {}" ,
875+ status .getInt ( ChronosRestApi .STATUS_CODE_KEY ),
876+ status .getString ( ChronosRestApi .STATUS_MESSAGE_KEY ) );
877+ }
878+ } catch ( UnirestException ex ) {
879+ log .debug ( "Exception while publishing log records. Attempt {}: {}" , attempt + 1 , MAX_RETRIES + 1 , ex );
880+ }
881+ attempt ++;
882+ try {
883+ TimeUnit .MILLISECONDS .sleep ( 500 );
884+ } catch ( InterruptedException e ) {
885+ Thread .currentThread ().interrupt ();
886+ return ;
870887 }
871- } catch ( UnirestException ex ) {
872- log .warn ( "Exception while publishing log records." , ex );
873888 }
889+ log .warn ( "Failed to publish log after {} attempts" , MAX_RETRIES + 1 );
874890 } ) );
875891 }
876892
0 commit comments