建立网站是很多企业开始进行网络营销的第一步,石家庄网络关键词排名,织梦网站名称,免费的黄台app下载春天猫rtsy好的#xff0c;这实际上与冬天无关#xff0c;众所周知#xff0c;冬天已经到了 。 它与Spring Retry有关#xff0c;Spring Retry是一个小的Spring框架库#xff0c;它使我们可以向应重试的任何任务添加重试功能。 这里有一个很好的教程 #xff0c;解释了如… 春天猫rtsy 好的这实际上与冬天无关众所周知冬天已经到了 。 它与Spring Retry有关Spring Retry是一个小的Spring框架库它使我们可以向应重试的任何任务添加重试功能。 这里有一个很好的教程 解释了如何设置简单的重试和恢复。 它很好地解释了如何添加spring-retry依赖项 使用Retryable和Recover批注以及将RetryTemplate与简单策略一起使用。 当我们实际上想根据异常的类型应用不同的重试行为时我想讲的是一个稍微复杂的情况。 这是有道理的因为我们可能知道某些异常是可恢复的而某些异常是不可恢复的因此尝试从异常中恢复并没有太大的意义。 为此有一个特定的重试策略实现称为ExceptionClassifierRetryPolicy 与Spring RetryTemplate一起使用 。 假设我们只能从IO异常中恢复并跳过所有其他异常 。 我们将创建三个类来扩展RetryCallback并创建一个类来扩展RecoveryCallback以更好地显示内部发生的情况 private class SuccessCallback implements RetryCallbackBoolean, RuntimeException {Overridepublic Boolean doWithRetry(RetryContext context) throws RuntimeException {System.out.println(Success callback: attempt context.getRetryCount());return true;}}private class ExceptionCallback implements RetryCallbackBoolean, Exception {Overridepublic Boolean doWithRetry(RetryContext context) throws Exception {System.out.println(Exception callback: attempt context.getRetryCount());throw new Exception(Test Exception);}}private class SpecificExceptionCallback implements RetryCallbackBoolean, IOException {Overridepublic Boolean doWithRetry(RetryContext context) throws IOException {System.out.println(IO Exception callback: attempt context.getRetryCount());throw new IOException(Test IO Exception);}}private class LoggingRecoveryCallback implements RecoveryCallbackBoolean {Overridepublic Boolean recover(RetryContext context) throws Exception {System.out.println(Attempts exhausted. Total: context.getRetryCount());System.out.println(Last exception: Optional.ofNullable(context.getLastThrowable()).orElse(new Throwable(No exception thrown)).getMessage());System.out.println(\n);return false;}} 然后我们设置RetryTemplate 。 我们将使用SimpeRetryPolicy和IOException的固定尝试次数以及一个NeverRetryPolicy 它仅允许对其他所有事物进行初始尝试。 *We want to retry on IOException only.Other Exceptions wont be retried.IOException will be retried three times, counting the initial attempt.*/final ExceptionClassifierRetryPolicy exRetryPolicy new ExceptionClassifierRetryPolicy();exRetryPolicy.setPolicyMap(new HashMapClass? extends Throwable, RetryPolicy() {{put(IOException.class, new SimpleRetryPolicy(3));put(Exception.class, new NeverRetryPolicy());}});retryTemplate.setRetryPolicy(exRetryPolicy); 现在我们需要使用这些回调来演示它们如何工作。 首先成功执行这很简单 // we do not catch anything hereSystem.out.println(\n*** Executing successfull callback...);retryTemplate.execute(new SuccessCallback(), new LoggingRecoveryCallback()); 其输出如下 *** Executing successfull callback...
Success callback: attempt 0 然后是异常 // we catch Exception to allow the program to continueSystem.out.println(\n*** Executing Exception callback...);try {retryTemplate.execute(new ExceptionCallback(), new LoggingRecoveryCallback());} catch (Exception e) {System.out.println(Suppressed Exception);}*** Executing Exception callback...
Exception callback: attempt 0
Attempts exhausted. Total: 1
Last exception: Test Exception 最后是我们的IOException // we catch IOException to allow the program to continueSystem.out.println(\n*** Executing IO Exception callback...);try {retryTemplate.execute(new SpecificExceptionCallback(), new LoggingRecoveryCallback());} catch (IOException e) {System.out.println(Suppressed IO Exception);}*** Executing IO Exception callback...
IO Exception callback: attempt 0
IO Exception callback: attempt 1
IO Exception callback: attempt 2
Attempts exhausted. Total: 3
Last exception: Test IO Exception 如我们所见只有IOException发起了三个尝试。 请注意尝试次数从0开始编号因为执行回调时尝试次数并未耗尽因此上一次尝试次数为2而不是3。 但是在RecoveryCallback上所有尝试均已用尽因此上下文保留3次尝试。 我们还可以看到尝试成功后未调用RecoveryCallback 。 也就是说仅当执行以异常结束时才调用它。 RetryTemplate是同步的因此所有执行都在我们的主线程中进行。 这就是为什么我在调用周围添加了try / catch块的原因以使程序可以毫无问题地运行所有三个示例。 否则重试策略将在上次失败尝试后将异常抛出并停止执行。 还有一个非常有趣的CompositeRetryPolicy 它允许添加几个策略并委托来依次调用它们。 它还可以允许创建相当灵活的重试策略但这本身就是另一个主题。 我认为spring-retry是一个非常有用的库它可以使常见的可重试任务更可预测可测试且更易于实现。 翻译自: https://www.javacodegeeks.com/2017/07/spring-retry-winter-coming.html春天猫rtsy