如何设置Spring Retry模板重试最大尝试次数:无限

use*_*543 4 java spring multithreading jdbc spring-retry

我想用Spring Retry修改数据库连接的创建,以便在应用程序启动时数据库关闭时再试一次。我不想限制重试次数。我应该如何配置策略来做到这一点。

我当前的代码(我知道在这种状态下它限制为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();
    }
});
Run Code Online (Sandbox Code Playgroud)

我应该如何修改此代码?

Gar*_*ell 5

使用AlwaysRetryPolicy代替SimpleRetryPolicy

但是您可能想要添加一个BackOffPolicy在重试之间等待的时间。

然后,您可以中断线程以关闭所有程序。

  • 不要在评论中提出新问题;问一个新问题。你不能;你必须连接一个拦截器并使用“interceptor”属性。将“maxAttempts”设置为“Integer.MAX_VALUE”将实现类似的效果。 (2认同)