How to reset failsafe?
See original GitHub issueHi sir, Could you help me with my requirement? My requirement is to carry out a JDBC operation with retries. I am using the following approach:
RetryPolicy retryPolicy = (RetryPolicy) new RetryPolicy()
.withDelay(Duration.ofMillis(retryIntervalMillis))
.withMaxRetries(retryLimit)
.onFailedAttempt(e -> {
ExecutionAttemptedEvent event = (ExecutionAttemptedEvent) e;
LOG.warn("Error encountered while establishing connection or doing the " +
"read/write operation {}", event.getLastFailure().getMessage());
})
.onRetry(e -> {
ExecutionAttemptedEvent event = (ExecutionAttemptedEvent) e;
Throwable failure = event.getLastFailure();
// log the retry as it seems to be a network/connection failure
LOG.warn("Connection error encountered {}", failure.getMessage(),
failure);
LOG.warn("Retrying {}th time to proceed again with a new connection",
event.getAttemptCount());
})
.handleIf(failure -> isRetryRequired((Throwable) failure));
isRetryRequired() checks the exception’s message and decides whether to do retry ot not. I am not showing its body here.
Next, this is how failsafe is used:
try {
Failsafe
.with(retryPolicy)
.run(() -> {
getJdbcConnection();
createStatement();
executeQueries();
});
} catch (SQLException e1) {
// todo
} finally {
closeConnection();
}
void executeQueries() {
for (String query : queryList) {
// execute the query
}
}
My question is, if any of the method fails (getJdbcConnection or createStatement or executeQuery) then I want to retry and thats how I have written the code. However, suppose I had configured three retries and I had successfully obtained the connection in 2nd retry. Once the connection is successful, I want to reset the Failsafe so that it can do three retries again if the connection fails next time. How is this possible? How can I reset the Failsafe? Or what approach do you suggest?
Issue Analytics
- State:
- Created 4 years ago
- Comments:34
Could be done differently:
👍