Comments (5)
Here is a retry function that I wrote to handle retries that has been adapted to work with Promises.
protected Promise<T> retry(Callable<CompletionStage<T>> callable) throws Exception {
int retries = 0;
boolean isRetry = false;
do {
try {
if (isRetry) {
logger.warn(String.format("Retrying previous request (%s).", retries));
}
return Promises.from(callable.call());
} catch (final Exception e) {
logger.error("An exception was thrown while processing request.", e);
isRetry = true;
if (retries >= maxRetries) {
throw e;
}
}
//
// Back off for two seconds to try and not saturate the server with retry attempts.
//
Thread.sleep(2000);
} while (retries++ < maxRetries);
throw new RuntimeException("The number of maximum retries has been exceeded for this request.");
}
from tascalate-concurrent.
Jeffrey,
If I understand your requirements correctly, then:
- You have
Callable<CompletionStage<T>> callable
that returns a newCompletionStage
whenever it's called. - You should use
net.tascalate.concurrent.Promises.retryFuture(callable, retryPolicy)
- In your case
retryPolicy
is:
RetryPolicy<T> retryPolicy = new RetryPolicy<>(maxRetries, DelayPolicy.fixedInterval(2000L));
Is it what you need?
from tascalate-concurrent.
It looks right. I don't know how I didn't see the retryFuture method before...
How are exceptions handled?
from tascalate-concurrent.
Jeffrey,
Unfortunately, RetryPolicy
class is not documented yet, but you can specify what exceptions are re-tryable and what should stop processing. Please check sources of the class -- but, in general, everything is configurable.
And, btw, in my example above you should use:
RetryPolicy<T> retryPolicy = new RetryPolicy<>(
maxRetries,
DelayPolicy.fixedInterval(2000L).withFirstRetryNoDelay()
);
To avoid waiting before the first execution
from tascalate-concurrent.
This worked. I apologize for not providing a follow up!
from tascalate-concurrent.
Related Issues (20)
- Issues with Callbacks HOT 11
- OnTimeout not working when computing intensive task HOT 5
- orTime and submit combination HOT 5
- Typo in DelayPolicy#withMinDelay HOT 1
- Feature enhancement on cancel HOT 2
- A FutureLocal.java would be nice(like ThreadLocal.java but more) HOT 1
- MultitargetException swallows Exceptions HOT 7
- Include class names in the documentation when highlighting a method
- Unexpected cancellation behavior HOT 6
- Promise.onTimeout does not cancel the async call in original CompletableTask.supplyAsync? HOT 8
- Promises#all(CompletionStage<? extends T>...) cancellation does not seem to work correctly HOT 5
- The cancel is not working on the async chain HOT 3
- Wait for Interrupted of CompletableTask.cancel HOT 4
- onTimeout behaviour confusing after 0.8.4 HOT 2
- Optimized Same Executor Runs HOT 1
- Mapped promise can't be interrupted HOT 1
- Exceptions in thenCompose() will not propagate HOT 3
- Promises.all - List type argument HOT 3
- whenCompleteAsync adds the failure exception as a suppressed exception to itself HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tascalate-concurrent.