spring retry注解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>test</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies> <!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry; @SpringBootApplication
@EnableRetry
public class Application { public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController { @Autowired
private RemoteService remoteService; @RequestMapping("/show")
public String show() {
try {
remoteService.call();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Exception Occur" + e.getMessage());
}
return "Hello World";
}
}
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service; @Service
public class RemoteService { private final int MAXATTEMPTS = 2;
private final long DELAY = 1000L; @Retryable(value = {NullPointerException.class, ArithmeticException.class},
maxAttempts = MAXATTEMPTS, backoff = @Backoff(delay = DELAY, multiplier = 1))
public void call() throws Exception {
System.out.println("do something...");
int a = 5 / 0;
String.valueOf(null);
// throw new NullPointerException("RPC调用异常");
} @Recover
public void recover(Exception e) {
e.printStackTrace();
}
}
spring retry注解的更多相关文章
- Spring retry基本使用
Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...
- Spring Retry
最近组内准备将项目中原有的重试功能抽取出来重构为一个重试平台,由于对重试的功能要求比较高,采用了不少中间件和框架(jimdb,jproxy, Elastic-Job ,JMQ,Hbase, Disru ...
- Spring retry实践
在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...
- Spring异常重试框架Spring Retry
Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...
- 异常重试框架Spring Retry实践
前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...
- Spring框架中一个有用的小组件:Spring Retry
1.概述 Spring Retry 是Spring框架中的一个组件, 它提供了自动重新调用失败操作的能力.这在错误可能是暂时发生的(如瞬时网络故障)的情况下很有帮助. 在本文中,我们将看到使用Spri ...
- Spring Retry 在SpringBoot 中的应用
Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...
- Spring Retry 重试
重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
随机推荐
- 【队列】最大值减去最小值小于等于num的子数组数量
摘自<程序员代码面试指南> 题目: 给定数组 arr 和整数 num, 共返回有多少个⼦数组满⾜如下情况:max(arr[i...j]) - min(arr[i...j]) <= n ...
- Linux IO实时监控iostat命令详解(转)
简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...
- select元素选择时间以及jQuery对select的属性操作
<select class="input04" id="1" name="in_class1" onchange="setc ...
- 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版
目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...
- 二、java 与 scala相互调用
介绍:scala 是简化的java,运行于jvm的脚步语言.Java和scala通过各自编译器编译过都是jvm能解析class文件.本文介绍java和scala如何互调 scala的源代码文件是以. ...
- oracle 实现 自增主键功能
转自:https://blog.csdn.net/zxh2075/article/details/78488141 之前有一项工作是将mysql的数据库实现转移到oracle,遇到了自增主键实现的问题 ...
- <正则吃饺子> :关于gson使用的一点总结
一.场景 在群里看到的信息:在使用 gson时候,报了个错 :java.lang.IllegalArgumentException: declares multiple JSON fields n ...
- Web Pages(单页面模型)
.NET 是一套框架,用来个HTML.JS.CSS和服务器端脚本构建网页和网站. 可以有三种开发模式:Web Pages(单页面模型).MVC(模型视图控制器).Web Forms(事件驱动模型) W ...
- java 设置Cookie值
原文地址:http://blog.csdn.net/lansetiankong12/article/details/52622654 /** * 读取所有cookie * 注意二.从客户端读取Cook ...
- python变量、类型、运算、输出
1.变量.类型.运算.输出等 # -*- coding: utf-8 -*- a=2 b=3 c=a+b print u'结果是=%i'%c #加u显示中文 str=unicode(s,"u ...