-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from synadia-io/start-retrier-0-1-0
Start Retrier 0.1.0
- Loading branch information
Showing
6 changed files
with
128 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
retrier/src/examples/java/io/synadia/examples/Placeholder.java
This file was deleted.
Oops, something went wrong.
113 changes: 113 additions & 0 deletions
113
retrier/src/examples/java/io/synadia/examples/RetrierExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) 2024 Synadia Communications Inc. All Rights Reserved. | ||
// See LICENSE and NOTICE file for details. | ||
|
||
package io.synadia.examples; | ||
|
||
import io.synadia.jnats.extension.Retrier; | ||
import io.synadia.jnats.extension.RetryAction; | ||
import io.synadia.jnats.extension.RetryConfig; | ||
import io.synadia.jnats.extension.RetryObserver; | ||
|
||
public class RetrierExample { | ||
|
||
private static String RECOVERABLE = "recoverable"; | ||
private static String UNRECOVERABLE = "unrecoverable"; | ||
|
||
public static void main(String[] args) { | ||
RetryConfig retryConfig = RetryConfig.builder() | ||
.attempts(5) | ||
.deadline(5000) | ||
.backoffPolicy(new long[]{100, 200, 300, 400, 500}) | ||
.build(); | ||
|
||
System.out.println("In this example, the action fails a few times, but eventually succeeds"); | ||
ExampleActionProvider actionProvider = new ExampleActionProvider(3); | ||
ExampleRetryObserver observer = new ExampleRetryObserver(); | ||
try { | ||
Retrier.execute(retryConfig, actionProvider, observer); | ||
} | ||
catch (Exception e) { | ||
System.out.println("This was setup to eventually complete, should not get here"); | ||
System.exit(-1); | ||
} | ||
System.out.println(" Expecting 4 action executions: " + actionProvider.executions); | ||
System.out.println(" Expecting 0 failuresLeft: " + actionProvider.failuresLeft); | ||
System.out.println(" Expecting to observe 3 exceptions: " + observer.exceptions); | ||
|
||
// ========================================================================================== | ||
|
||
System.out.println("\nIn this example, the action fails more times than attempts allowed"); | ||
actionProvider = new ExampleActionProvider(6); | ||
observer = new ExampleRetryObserver(); | ||
try { | ||
Retrier.execute(retryConfig, actionProvider, observer); | ||
} | ||
catch (Exception e) { | ||
System.out.println("This was expected, even though it is \"recoverable\" -> " + e); | ||
} | ||
System.out.println(" Expecting 6 action executions: " + actionProvider.executions); | ||
System.out.println(" Expecting 1 failuresLeft: " + actionProvider.failuresLeft); | ||
System.out.println(" Expecting to observe 5 exceptions: " + observer.exceptions); | ||
|
||
// ========================================================================================== | ||
|
||
System.out.println("\nIn this example, there is an unrecoverable exception, so the observer cancelled the retry."); | ||
ExampleUnrecoverableActionProvider unProvider = new ExampleUnrecoverableActionProvider(3); | ||
observer = new ExampleRetryObserver(); | ||
try { | ||
Retrier.execute(retryConfig, unProvider, observer); | ||
} | ||
catch (Exception e) { | ||
System.out.println("This was expected, it is \"urecoverable\" -> " + e); | ||
} | ||
System.out.println(" Expecting 4 action executions: " + unProvider.executions); | ||
System.out.println(" Expecting 0 failuresLeft: " + unProvider.failuresLeft); | ||
System.out.println(" Expecting to observe 4 exceptions: " + observer.exceptions); | ||
} | ||
|
||
static class ExampleActionProvider implements RetryAction<Boolean> { | ||
public int failuresLeft; | ||
public int executions; | ||
|
||
public ExampleActionProvider(int failuresBeforeWorking) { | ||
this.failuresLeft = failuresBeforeWorking + 1; | ||
} | ||
|
||
@Override | ||
public Boolean execute() throws Exception { | ||
executions++; | ||
if (--failuresLeft > 0) { | ||
throw new Exception(RECOVERABLE); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
static class ExampleUnrecoverableActionProvider implements RetryAction<Boolean> { | ||
public int failuresLeft; | ||
public int executions; | ||
|
||
public ExampleUnrecoverableActionProvider(int failuresBeforeUnrecoverable) { | ||
this.failuresLeft = failuresBeforeUnrecoverable + 1; | ||
} | ||
|
||
@Override | ||
public Boolean execute() throws Exception { | ||
executions++; | ||
if (--failuresLeft > 0) { | ||
throw new Exception(RECOVERABLE); | ||
} | ||
throw new Exception(UNRECOVERABLE); | ||
} | ||
} | ||
|
||
static class ExampleRetryObserver implements RetryObserver { | ||
public int exceptions = 0; | ||
|
||
@Override | ||
public boolean shouldRetry(Exception e) { | ||
exceptions++; | ||
return !e.getMessage().contains(UNRECOVERABLE); | ||
} | ||
} | ||
} |