failsafe: How to reset failsafe?

Hi 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?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 34 (2 by maintainers)

Most upvoted comments

Could be done differently:

int totalMessagesToBeRead = 100;

RetryPolicy<Message> retryPolicy = new RetryPolicy<>()
        .withDelay(Duration.ofMillis(retryIntervalMillis))
        .withMaxRetries(retryLimit)
        .onRetry(e -> disconnect())
        .handleIf(failure -> isRetryRequired(failure));

try {
    consumeMessages();
} catch (FailsafeException e) {
    throw e.getCause();
} finally {
    closeSession();
    closeConnection();
}

private void consumeMessages() {
    range(0, totalMessagesToBeRead).forEach(i -> 
        processMessage(getNextMessage()));
}

private Message getNextMessage() {
    return Failsafe
            .with(retryPolicy)
            .get(() -> {
                connectIfNecessary();
                return consumer.receive();
            ]);
}

private void connectIfNecessary() {
    if (connection == null) connect();
}

private void disconnect() {
    connection = null;
}