好的,这实际上与冬天无关,众所周知,冬天已经到了 。 它是关于Spring Retry的,Spring是一个小的Spring框架库,它使我们可以将重试功能添加到应可重试的任何任务中。

这里有一个很好的教程 ,解释了如何设置简单的重试和恢复。 它很好地解释了如何添加spring-retry依赖项 ,使用@Retryable和@Recover批注以及将RetryTemplate与简单策略一起使用。 当我们实际上想根据异常的类型应用不同的重试行为时,我想讲的是一个稍微复杂的情况。 这是有道理的,因为我们可能知道有些异常是可恢复的,而有些则是不可恢复的,因此尝试从异常中恢复并没有太大的意义。 为此,有一个特定的重试策略实现,称为ExceptionClassifierRetryPolicy ,与Spring RetryTemplate一起使用 。

假设我们只能从IO异常中恢复并跳过所有其他异常 。 我们将创建三个类来扩展RetryCallback,并创建一个类来扩展RecoveryCallback以更好地显示内部发生的情况:

private class SuccessCallback implements RetryCallback<Boolean, RuntimeException> {@Overridepublic Boolean doWithRetry(RetryContext context) throws RuntimeException {System.out.println("Success callback: attempt " + context.getRetryCount());return true;}}private class ExceptionCallback implements RetryCallback<Boolean, 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 RetryCallback<Boolean, 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 RecoveryCallback<Boolean> {@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 won't be retried.IOException will be retried three times, counting the initial attempt.*/final ExceptionClassifierRetryPolicy exRetryPolicy = new ExceptionClassifierRetryPolicy();exRetryPolicy.setPolicyMap(new HashMap<Class<? 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

春天重试,因为冬天来了相关推荐

  1. 春天猫rtsy_春天重试,因为冬天来了

    春天猫rtsy 好的,这实际上与冬天无关,众所周知,冬天已经到了 . 它与Spring Retry有关,Spring Retry是一个小的Spring框架库,它使我们可以向应重试的任何任务添加重试功能 ...

  2. 2013中国手机的春天在哪里?

    转载,出自科幻星系 QQ:88328702 MSN:wangk1026@hotmail.com 2013的春天来了,树上的雪融化了,一切有生命的东西都忙着准备复苏,而那些终于熬过又一个寒冬的手机企业也 ...

  3. Science:Knight组发表尸体降解过程中的微生物组

    Metcalf, J. L., et al. (2016). "Microbial community assembly and metabolic function during mamm ...

  4. Sciences:Knight组发表尸体降解过程中的微生物组

    Metcalf, J. L., et al. (2016). "Microbial community assembly and metabolic function during mamm ...

  5. 16S+功能预测发Sciences:尸体降解过程中的微生物组

    Metcalf, J. L., et al. (2016). "Microbial community assembly and metabolic function during mamm ...

  6. 16S+功能预测也能发Sciences:尸体降解过程中的微生物组

    Metcalf, J. L., et al. (2016). "Microbial community assembly and metabolic function during mamm ...

  7. java中for循环的简化_Java中for语句的简化写法

    Java当中的for语句是可以进行简化的,但是简化有一定的条件: 简化后的写法:  for(数据类型  表示符 : 表达式){....} 根据"表达式"类型,总共就有两种类型的表达 ...

  8. python序列元素的编号称为_Python序列

    序列 是一块用于存放多个值的连续内存空间,并且按照一定顺序排列,没一个值(称为元素)都分配一个数字,称为索引或位置. 通过该索引可以去除相应的值 在Python中,序列结构主要有列表 元组 集合 字典 ...

  9. 学妹问我Java枚举类与注解,我直接用这个搞定她!

    很多人问我学妹长什么样,不多说 上图吧! 学妹问我Java枚举类与注解,我直接一篇文章搞定! 一.枚举类 ① 自定义枚举类 ② enum关键字定义枚举类 ③ enum 枚举类的方法 ④ enum 枚举 ...

最新文章

  1. android 仿京东toolbar,仿京东商城系列2------自定义toolbar
  2. Java常用类之【日期相关类】
  3. 『原创』+『参考』使用C#在PPC的Today界面上的任务栏加入应用程序图标
  4. 中间件是什么?在.NET Core中的工作原理又是怎样的呢?
  5. 冒泡排序详解--python
  6. 机器学习之朴素贝叶斯算法原理
  7. 为什么哈希表的容量一定要是 2的整数次幂?
  8. Unix整理笔记——在指定时间运行程序——里程碑M12
  9. C语言如何调用REFPROP软件,如何用C++6.0调用refprop物性查询软件
  10. android 7修改机型,Android 7.0支持机型大全
  11. 中国各地区工业COD排放量面板数据(1998-2017年)
  12. 【181214】VC++动画翻页效果的电话簿程序源代码
  13. SofaRpc源码学习篇-netty以及跟dubbo骚操作,负载均衡
  14. 耿丹CS16-2班第三次作业汇总
  15. yolov3中的route和shortcut层
  16. ERP打印入库单(四十)
  17. 关于餐厅选址,这10种店铺千万别选!
  18. Android第一行代码-Fragment
  19. Git仓库完整迁移 含历史记录
  20. Mac打开网页速度奇慢/无法打开内网网页解决方案

热门文章

  1. MySQL find_in_set()函数
  2. JavaScript学习总结(八)——JavaScript数组
  3. React中解决样式丢失问题
  4. java正则表达式验证密码_最新密码验证正则表达式
  5. java虚拟机的内存模型_JVM(Java虚拟机)内存模型(转载/整理)
  6. DevExperience(1801)
  7. ReviewForJob——二叉堆优先队列的实现(三种堆节点类型——int + struct HeapNode + struct HeapNode*)
  8. java日志——修改日志管理器配置+日志本地化
  9. 快速排序算法思想及实现
  10. java中无法推断类型参数_Java 10中的本地类型推断,或者如果它像鸭子一样嘎嘎叫...