测试类以及测试代码、复制即可

package com.cms.util;

import javax.swing.plaf.synth.SynthSpinnerUI;

import org.springframework.retry.RecoveryCallback;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate; public class TestUtils {
public static void main(String[] args) throws Exception {
new TestUtils().sendMsg("", null, null);
}
public void sendMsg(String msg,String phone,String[] arr) throws Exception{ RetryTemplate template = new RetryTemplate();
// 策略
SimpleRetryPolicy policy = new SimpleRetryPolicy();
policy.setMaxAttempts(20);
template.setRetryPolicy(policy); ExponentialBackOffPolicy exponen = new ExponentialBackOffPolicy();
exponen.setInitialInterval(1000);
exponen.setMultiplier(2);
template.setBackOffPolicy(exponen);
String result = template.execute(
new RetryCallback<String, Exception>() {
@Override
public String doWithRetry(RetryContext arg0) throws Exception {
try {
if(1==1){
System.out.println(123);
throw new Exception();
}
} catch (Exception e) {
// TODO Auto-generated catch block
throw new Exception();
}
return "";
}
}
,
new RecoveryCallback<String>() {
@Override
public String recover(RetryContext context) {
return "recovery callback";
}
}
);
}
}

以下是maven依赖

           <dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>

spring的重试策略、发生异常会自动重新调用的更多相关文章

  1. 在linux下python爬虫进程发生异常时自动重启直至正常结束的方法

    之前在做爬虫的时候遇到一种情况,当网络情况不太好的时候,爬虫爬到的链接在urlopen时会因为无法正常连接而报URLError或者timeout的错误导致陈序报错而终止:但是这些错误在重新运行陈序后能 ...

  2. CASE:DB shutdown/open 过程中发生异常导致JOB不能自动执行

    CASE:DB shutdown/open 过程中发生异常导致JOB不能自动执行 现象: 一个DB中的所有JOB在3月25日之后就不再自动运行,查询DBA_JOBS,发现LAST_DATE定格在3月2 ...

  3. String.Net “System.TypeInitializationException”类型的未经处理的异常在 Spring.NetDemo.exe 中发生

    今天编写String.Net时,遇到“System.TypeInitializationException”类型的未经处理的异常在 Spring.NetDemo.exe 中发生 原因配置文件的顺序写错 ...

  4. 关于HttpClient重试策略的研究

    一.背景 由于工作上的业务本人经常与第三方系统交互,所以经常会使用HttpClient与第三方进行通信.对于交易类的接口,订单状态是至关重要的. 这就牵扯到一系列问题: HttpClient是否有默认 ...

  5. java web项目发生异常依然能运行

    由于JavaWeb应用业务逻辑的复杂性,容易发生一些意想不到的错误和异常,给系统的调试带来不必要的麻烦,不友好的提示信息使编程者对错误和异常无从下手.特别是当发生异常时,Java异常栈输出的信息只能给 ...

  6. 自己动手实践 spring retry 重试框架

    前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...

  7. Polly一种.NET弹性和瞬态故障处理库(重试策略、断路器、超时、隔板隔离、缓存、回退、策略包装)

    下载地址:https://github.com/App-vNext/Polly 该库实现了七种恢复策略. 重试策略(Retry) 重试策略针对的前置条件是短暂的故障延迟且在短暂的延迟之后能够自我纠正. ...

  8. Azure Storage Client Library 重试策略建议

    有关如何配置 Azure Storage Library 重试策略的信息,可参阅 Gaurav Mantri 撰写的一篇不错的文章<SCL 2.0 – 实施重试策略>.但很难找到关于使用何 ...

  9. Spring装配Bean之组件扫描和自动装配

    Spring从两个角度来实现自动化装配: 组件扫描:Spring会自动发现应用上下文中所创建的bean. 自动装配:Spring自动满足bean之间的依赖. 案例:音响系统的组件.首先为CD创建Com ...

随机推荐

  1. spark的存储系统--BlockManager源码分析

    spark的存储系统--BlockManager源码分析 根据之前的一系列分析,我们对spark作业从创建到调度分发,到执行,最后结果回传driver的过程有了一个大概的了解.但是在分析源码的过程中也 ...

  2. Beego 学习比较8:SQL语句

    SQL语句 1>     常用的SQL语句 1->新增  insert into 表名(字段A,字段B,…) Values(字段A值,字段B值,…) 2->更新  update 表名 ...

  3. CSS3 2D 变换

    CSS2D transform 表示2D变换,目前获得了各主流浏览器的支持,是CSS3中具有颠覆性的特征之一,可以实现元素的位移.旋转.倾斜.缩放,甚至支持矩阵方式,可以取代大量之前只能靠Flash才 ...

  4. Apache加固之目录、文件限制

    如果你用类似phpstudy集成平台的话,所有你想要修改的配置基本上在phpstudy上就可以直接设置操作.但如果你的服务器是通过一步步安装的(Apache+PHP+Mysql)的话,那么就要对各功能 ...

  5. spring boot 的request.getServletContext().getRealPath路径获取问题

    默认情况下springboot中request.getServletContext().getRealPath 返回的是一个临时文件夹的地址 通过查看源代码 位置在 org.springframewo ...

  6. SAP Cloud Platform integration上创建一个最简单的iFlow

    登录SAP CPI控制台,点击这个铅笔图标进入工作区域: 选择一个已经存在的content package: 在这个content package里创建一个新的iFlow: 默认生成的iFlow模型如 ...

  7. MySQL 触发器学习-markdown->html 格式测试

    <!doctype html>触发器 figure:first-child { margin-top: -20px; } #write ol, #write ul { position: ...

  8. shell脚本条件判断if中-a到-z的意思

    [ -a FILE ]  如果 FILE 存在则为真.  [ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真.  [ -c FILE ]  如果 FILE 存在且是一个字特殊文件则 ...

  9. 网络服务-SAMBA

    1. Samba 概述 SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内不同操作系统的计算机之间提供文件及打印机等资源的共享 ...

  10. Java基础--static关键字

    不管是平时阅读源代码,还是笔试.面试中,static关键字还是经常被问道,这篇文章主要来重新复习一下该关键字. 一.static用途 static方便在没有创建对象的时候调用方法或者变量. stati ...