spring-retry简单demo(附完整代码)
2018-08-12 16:42
525 查看
重试
最近项目要用到重试。开始想自己写,后来想用RetryTemplate,最后想到既然项目用了springboot,还是直接集成spring-retry把。
代码
Application
package com.danni; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.retry.annotation.EnableRetry; @SpringBootApplication @EnableRetry public class Application { public static void main(String[] args) throws Exception { @SuppressWarnings("resource") ApplicationContext annotationContext = new AnnotationConfigApplicationContext("com.danni"); RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class); remoteService.call(); } }
RemoteService
package com.danni; import org.springframework.remoting.RemoteAccessException; 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 { @Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1)) public void call() throws Exception { System.out.println("do something..."); throw new RemoteAccessException("RPC调用异常"); } @Recover public void recover(RemoteAccessException e) { System.out.println(e.getMessage()); } }
pom.xml
<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>spring-retry-demo</groupId> <artifactId>spring-retry-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Retry --> <!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry --> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <!-- <version>1.1.2.RELEASE</version> --> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <!-- <version>1.8.6</version> --> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
运行
...省略 16:33:26.561 [main] INFO org.springframework.jmx.export.annotation.AnnotationMBeanExporter - Registering beans for JMX exposure on startup 16:33:26.561 [main] DEBUG org.springframework.jmx.export.annotation.AnnotationMBeanExporter - Autodetecting user-defined JMX MBeans 16:33:26.561 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@170da4c] 16:33:26.561 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor' 16:33:26.561 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor' 16:33:26.561 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source 16:33:26.561 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'remoteService' 16:33:26.577 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=0 do something... 16:33:26.592 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000 16:33:31.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=1 16:33:31.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=1 do something... 16:33:31.592 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000 16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=2 16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=2 do something... 16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=3 16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry failed last attempt: count=3 RPC调用异常
代码下载
https://gitee.com/danni3/spring-retry-demo
参考
https://blog.csdn.net/u014513883/article/details/52371198
https://github.com/spring-projects/spring-retry
https://www.cnblogs.com/EasonJim/p/7684649.html
相关文章推荐
- java web项目简单查询demo前后台完整代码
- 最简单的PBO(异步Read-Back)Demo 完整代码示例
- 分享一份完整的spring data jpa demo代码
- 分享一份完整的spring data jpa demo代码
- spring boot + kafka 简单Demo
- spring 集成apache qpid 简单demo
- Quartz定时任务简单demo及与Spring整合
- springMVC+Hibernate简单的Demo
- 最简单的cxf restful webservice Demo(包括与spring集成)
- 使用Spring缓存的简单Demo
- 简单实用方便的ajax评论完整代码
- [置顶] docker构建spring boot 简单demo
- 简单的spring使用反射实现属性赋值demo
- 【GIFDecoder】GIFDecoder的排错以及修改另附完整代码和demo
- SpringBoot+Websocket实现一个简单的网页聊天功能代码
- Spring完整的约束配置代码
- springmvc+freemarker的简单demo
- 简单代码 模拟 spring的IOC机制
- Java笔记9:Spring简单Demo
- spring-boot demo生成代码地址