question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to reset failsafe?

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:34

github_iconTop GitHub Comments

1reaction
whiskeysierracommented, Nov 22, 2019

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;
}
0reactions
jhaltermancommented, Nov 25, 2019

👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Reset Engine Failsafe Mode [Symptoms and Fixes]
The other thing you can try out is to shut off the engine and give it at least 5 minutes before restarting. Use...
Read more >
Vehicle Engine Failsafe Mode Explained and How to Reset it
And yes, hard codes are responsible for failsafe more or limp mode. So if you're sure your vehicle is in limp mode, it...
Read more >
How do i reset engine failsafe mode - JustAnswer
Disconnect the battery cables and clamp them together for 10 mins. Remove both battery leads from the battery then use a nut and...
Read more >
How to Reset Engine Failsafe Mode - Gametechia
1. Park your vehicle on level ground and turn off the engine.2. Locate the reset button on the failsafe device. It is usually...
Read more >
How to reset engine failsafe mode - ZGR.net
How do you reset limp mode? · Bring your car to a complete stop. · Shift your automatic transmission into PARK. · Turn...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found