user2695543
user2695543

Reputation: 111

How to set Spring Retry template to retry max attempts: infinite

I would like to modify DB connection creation with Spring Retry to try again if DB is down at application startup. I would not like to limit the number of retries. How should I configure the policy to do that.

My current code (I know in this state it limits to 100):

SimpleRetryPolicy policy = new SimpleRetryPolicy(100, Collections.singletonMap(Exception.class, true));

// Use the policy...
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(policy);
Connection conn = template.execute(new RetryCallback<Connection, Exception>() {
    public Connection doWithRetry(RetryContext context) throws Exception {
        return getConnectionFactory().createConnection();
    }
});

How should I modify this code?

Upvotes: 8

Views: 8670

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

Use AlwaysRetryPolicy instead of SimpleRetryPolicy.

But you might want to add a BackOffPolicy to wait between retries.

You can then interrupt the thread to shut everything down.

Upvotes: 6

Related Questions