我需要调用一个 url,并且调用可能会返回错误的 http 代码,例如 404、500 等。我想在收到这些错误时实现重试功能:每小时都会进行一次新的调用,最多十次。
我使用 async-http-client 库来执行异步 POST 调用。
你有什么想法吗?
预先感谢您的帮助。
最佳答案
值得考虑 Spring Retry功能。
API 的构造与您想要重试的内容无关,并且关注重试策略、退避、限制重试次数等。
如果您使用 Java 7/8,另一种可能性是 AsyncRetryExecutor 。例如
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
RetryExecutor executor = new AsyncRetryExecutor(scheduler).
retryOn(SocketException.class).
withExponentialBackoff(500, 2). //500ms times 2 after each retry
withMaxDelay(10_000). //10 seconds
withUniformJitter(). //add between +/- 100 ms randomly
withMaxRetries(20);
关于java - 如何使用 AsyncHttpClient 对错误的 http 代码实现重试功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195172/