Skip to content

Commit d670e8d

Browse files
committed
read me - usage examples
1 parent ec878f6 commit d670e8d

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,102 @@ Pull requests and bug reports are welcome
2727
[1]: https://travis-ci.org/EnigmaBridge/retry.java.svg
2828
[2]: https://travis-ci.org/EnigmaBridge/retry.java
2929

30+
## Examples
31+
32+
#### Synchronous Backoff
33+
```
34+
// Initialize retry strategy to Backoff
35+
final EBRetryStrategy retryStrategy = new EBRetryStrategyBackoff.Builder()
36+
.setMaxElapsedTimeMillis(1000*60*5) // Limit on total time
37+
.build();
38+
39+
// New retry mechanism instance, for each job.
40+
final EBRetry<ResultObject, Throwable> ebRetry = new EBRetry<ResultObject, Throwable>(retryStrategy.copy());
41+
42+
// Define retry job
43+
ebRetry.setJob(new EBRetryJobSimpleSafeThrErr<ResultObject>() {
44+
@Override
45+
public void runAsyncNoException(EBCallback<ResultObject, Throwable> callback) throws Throwable {
46+
try {
47+
// Do the actuall job here. May be called multiple times.
48+
final ResultObject result = our_task_to_retry();
49+
50+
// Signalize success to the retry object so it knows we can quit trying.
51+
callback.onSuccess(result);
52+
53+
} catch(IOException exception) {
54+
// You mau log it if you want
55+
//LOG.debug("Job failed");
56+
57+
// Call fail callback so retry mechanism knows this attempt was
58+
// not successful and it should continue trying or give up
59+
// if number of attempts or total ellapsed time exceeded threshold.
60+
//
61+
// Fail accepts job error object. We usually use Throwable as an job error
62+
// as it usually corresponds with the failure (e.g., IOException on network error).
63+
// We wrap throwable to the job error and pass it to the fail callback.
64+
callback.onFail(new EBRetryJobErrorThr(exception), false);
65+
}
66+
}
67+
});
68+
69+
// Start the job synchronously.
70+
try {
71+
return ebRetry.runSync();
72+
73+
} catch (EBRetryFailedException e) {
74+
throw new IOException(e);
75+
76+
} catch (EBRetryException e){
77+
throw new IOException("Fatal request error", e);
78+
}
79+
```
80+
81+
### Asynchronous backoff
82+
Retry mechanism supports asynchronous jobs.
83+
84+
```
85+
// Initialize retry strategy to Backoff
86+
final EBRetryStrategy retryStrategy = new EBRetryStrategyBackoff.Builder()
87+
.setMaxElapsedTimeMillis(1000*60*5) // Limit on total time
88+
.build();
89+
90+
// New retry mechanism instance, for each job.
91+
final EBRetry<ResultObject, Throwable> ebRetry = new EBRetry<ResultObject, Throwable>(retryStrategy.copy());
92+
93+
// Define retry job
94+
ebRetry.setJob(new EBRetryJobSimpleSafeThrErr<ResultObject>() {
95+
@Override
96+
public void runAsyncNoException(EBCallback<ResultObject, Throwable> callback) throws Throwable {
97+
try {
98+
// Do the actuall job here. May be called multiple times.
99+
final ResultObject result = our_task_to_retry();
100+
101+
// Signalize success to the retry object so it knows we can quit trying.
102+
callback.onSuccess(result);
103+
104+
} catch(IOException exception) {
105+
callback.onFail(new EBRetryJobErrorThr(exception), false);
106+
}
107+
}
108+
});
109+
110+
// Define async listener. Will be called on success / fail.
111+
// Fail = backoff strategy expired (threshold exceeded)
112+
ebRetry.addListener(new EBRetryListener<EBRawResponse, Throwable>() {
113+
@Override
114+
public void onSuccess(EBRawResponse ebRawResponse, EBRetry<EBRawResponse, Throwable> retry) {
115+
// hooray, success
116+
}
117+
118+
@Override
119+
public void onFail(EBRetryJobError<Throwable> error, EBRetry<EBRawResponse, Throwable> retry) {
120+
// well, we tried
121+
}
122+
});
123+
124+
// Run asynchronously.
125+
// Keep future if you want to detect if job is still running
126+
// Or to cancel it or to skip current backoff waiting interval (runNow())
127+
final EBFuture<EBRawResponse, Throwable> future = ebRetry.runAsync();
128+
```

0 commit comments

Comments
 (0)