一、环境树立
- 加入SpringRetry依靠,SpringRetry运用AOP结束,所以也需求加入AOP包
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</depejavaapi中文在线看ndency>
<dependency>http 302;
<groupId>org.spr业务性作业是什么意思ingframework</groupId>
<artifactId>spring-aspects</artifactId>
</de业务文书pendency>
- 官方文档
- www.baeldung.cjava初学om/spring-retr…
二、RetryTemplate
2.1 RetryTemplate
- RetryTemplate封装HTTP了Retry底子操作
- org.springframework.retry.support.RetryTemplate
- RetryTemplate中可以指定监听、回退战略、java难学吗重试战略等
- 只需求正常new RetryTemplate()即可运用
2.2 RetryListener
- RetryListener指定了当实施过程中呈现过错时的回javaapi中文在线看调
- org.springframewohttp://www.baidu.comrk.retry.RetryListener
package org.springframework.retry;
public interface RetryListener {
<T, E extends Throwable> boolean open(Retrapp下载yContext context, RetryC业务员allback<T, E> callback);
<T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable);
<T, E extends Thro接口crc过错计数wable> void onError(RetryContext context, RetryCall接口文档back<T, E> callback, Throwable throwable);
}
- 装备之后在RetryTemplate中指定
2.3HTTP 回退战略
2.3.1 FixedBackOffPolicy
- 当出业务文书现过错时推迟多少时刻继续调用
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(1000L);
retryTem接口测验面试题plate.setBackOf接口crc过错计数fPolicy(fixedBackOffPolicy);
- 装备之后在RetryTempl业务性作业ate中指定
2.3.2 Expone业务性作业是什么意思ntihttp代理alBackOffPolicy
- 当呈现过错时第一次按照指定推迟时刻推迟后按照指数进行推迟
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
exponejava言语ntialBackOffPolicy.setInitialInterval(1000L);
expoappearnentialBackOffPolicy.set接口crc过错计数Multiplier(2);
retryTemplat业务所e.setBackOffPolicy(exponentialBackOffPolicy);
- 装备之后在RetryTemplate中指定
2.4 重试战略
- 重试战略首要指定呈现过错时重试次数
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(5);
retryTemplate.setRhttpwatchetryPolicy(retryPolicy);
- 装备之后在RetryTemplate中指定
2.5 RetryCallback
- RetryCallback为retryTemplate.execute时实施的回调
- public final <Tappreciate, E exapproachtends Throwable> T execute(RetryCallback<T, E&g业务的四个特性t; retryC业务所是干什么的allback) throws E
2.6 中心运用
- 可以运用RetryTempl业务所是干什么的ate结束简单运用
- 装备retryTemplate
- 指定回退战略为ExponentialBhttp://www.baidu.comackOffPolicy
- 指定重试战略为SimpleRetryPolicy
- 指定监听器RetryListener
import com.codecoord.util.PrintUtil;
import org.springframework.context.annotation.Bean;
import or业务性作业是什么意思g.springframework.context.annotation.Cohttps和http的差异nfiguration;
import org.springframework.retry.RetryCallback;
impor业务t org.springframework.retry.RetryContext;
impojava初学rt org.spri业务ngframework.retry.RetryListener;
im业务所port org.springframework.retry.backoff.Exponapp是什么意思entialBapplicationackOffPolicy;
import org.springfra接口的界说mework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
@Configuration
public class RetryTemplateConfig {
@Bean
public RetryTemplate retryTemplate() {application
RetryTemplaappreciatete retryTemplate =httpclient new RetryTemplate();
ExponentialBackOffPolicyappearance exponentialappearanceBahttp://www.baidu.comckOffPolicy = new Exponentia接口和抽象类的差异lBackOffPolicy();
exponentialBackOffPolicy.setInitialInterval(1000L);
eappreciatexponentialBackOffPolicy.setMultiplier(2);
retryjavascriptTemplate.setBackOffPolicy(exponentialBackOffPolicy);
SimpleRet业务所ryPolicy retryPolicy = new SimpleRetryPolicy();
retrjava言语yPolicy.setMaxAttempts(5);
retryTemplate.setRetryPolicy(retryPolicy);
RetryListener[] listeners = {
new RetryListener() {
@Override
public接口文档 <T, E extends T接口文档hrowable> boolean open(RetryContext contextjava模拟器, RetryCallbacjava言语k<T, E&javaapi中文在线看gt; callback) {
PrintUtil.接口是什么print("open");
return true;
}
@Override
public <T, E extends Throwable> void close(RetryContext context, Ret接口是什么ryCallback<T, E> callback,
Throwable throwable) {
PrintUtil.print("close");
}
@Override
public <T, E extend接口s Throwable> voi接口的界说d onErrojavaapi中文在线看r(R接口卡etryCoappstorentext context, RappointmentetryCallback<T, E> callback,
Throwable throwable) {
PrintUtil.print("onError");
}
}
};
retryTemplate.setLi业务性作业是什么意思steners(listeners);
return retrhttps和http的差异yTemplate;
}
}
- 在controller中注入RetryTemplate运用,也可以是在servijava面试题ce中
@RestChttp://www.baidu.comontroller
public class SpringRethttp 404ryController {
@Resource
priva接口的界说te RetryTemplate retryTempjava怎么读late;
private static int count = 0;
@RequestMapping("/retry")
public Object retry() {
try {
count = 0;
retryTemplate.execute((RetryCallback<Voiapp是什么意思d, RuntimeExcepHTTPtion>) co接口是什么ntext -> {
++count;
throw new RuntimeException("抛出异常");
});
} catch (RuntimeExcappeareption e)接口文档 {
System.outhttpclient.println("Exception");
}
return "retry = " + count;
}
}
- 拜访retry接口,然后调查日志输出
18:27:20.648 - http-nio-8888-exec-1 - open
18:27:20.649 - http-nio-888app是什么意思8-exec-1 - retryT接口的效果em接口测验plathttpwatche.execute实施
18:27:javaee20.649 - http-nio-8888-exec-1 - onError
18:27:21.658 - http-接口nio-8888-ex业务的四个特性ec-1 - retryTemplate.execute实施
18:27:21.658 - http-nio-8888-exec-1 - onError
18:27:2java面试题3.670 - http-nio-888接口和抽象类的差异8-exec-1 - retryT接口测验面试题emplate.execute实施
18:27:23.670 - http-nio-8888-exec-1 - onError
18:27:27.679 - http-nio-8888业务性作业是什么意思-exec-1 - retryTemplathttps和http的差异e.execute实施
18:27:27.679 - http-nio-8888-exec-1 - onError
18:27:35.681 - http-nio-8888java模拟器-exec-1 - retryTemplate.execute实施
18:27:35.681 - http-nio-8888-e接口是什么xec-1 - onError
18:27:35业务员.681 - http-nio-8888-exec-1 - close
三、EnableRetry
- @EnableRetrjava作业培训班y打开重试,在类上指定的时分办法将默许实施,重试三次
- 界说serv业务员ice,打开@EnableRetry注解和指定@Retryable,重试可以参看后面接口的界说一节
import org.springframework.retry.annotation.Retryable;
public interface RetryService {
@Retryable
void retryServiceCall();
}
import org.springframework.retry.annotation.EnableRetry;
impo业务员rt org.springframework.st接口和抽象类的差异ereotype.Service;
@EnableRetry
@Service
public clahttps和http的差异ss RetryServiceImpl implements RetrySehttp://www.baidu.comrvice {
@Override
public void retryServiceCall() {
PrintUtil.print("办法调用..");
throw new RuntimeException("手艺异常");
}
}
- controller中注入service
@Requesjava模拟器tMapping("/retr业务文书yAnnotation")
public Objeapplicationc接口测验t retryAnnotation()javascript {
retryService.retryServiceCall();
return "retryAnnotation";
}
- 将会默许重试
18:46:48httpclient.721 - http-nio-8888-exec-1 - 办法调用..
18:46:49.724 - http-nappointmentio-8javaapi中文在线看888-exec-1 - 办法调用..
18:46:50.730 - http-nio-8888-exec-1 - 办法调用..
java.lan业务g.Runtimhttp 500eException: 手艺HTTP异常接口crc过错计数
四、Retryable
- 用于http://www.baidu.com需求重试的办法上的注解
- 有以下几个特javascript色
- Retryaappreciateble注解参数
- value:指定产生的异常进行重试
- include:和value一样,默许空,当exclude也为空时,全部异常都重试
- exclude:指定异常不重试,默许空,当include也为空时,全部异常都重试
- maxAttemps:重试次数,默许3
- backoffapp是什么意思:重试补偿机制,默许没接口crc过错计数有
- @Backoff 注解 重试补偿java模拟器战略
- 不设置参数时,默许运用FixedBackOffPolicy(指定等候时刻)接口文档,重试等候1000ms
- 设置delay,运用FixedBackOffPolicy(指定等候设置delay和maxDealy时,重试等候在这两个值之间均态散布)
- 设置delay、maxDealy、multiplier,运用 ExponentialBackOffPolicyapproach(指数级重试间隔的结束),multiplier即指定推迟倍数,比方delay=5000L,multiplhttp协议ier=2,则第一次重试为5秒,第2次为10秒,第三次为20秒
@Target({ ElementType.METHOD, Elhttp 404ementType.TYPE })
@Retentjavascription(RetentionPolicy.RUNTIME)
@Documenthttp 500ed
public @interfajavaapi中文在线看ce Retryable {
Strappreciateing interceptor() default "";
Class<? extends Th接口测验面试题rowable>[业务性作业是什么意思] value() default {};
Class<? extends Throwabjavaeele>[] include() defauhttps和http的差异lt {};
Class<? extends Throwable>[] exclude() default {};
String label() default ""接口的效果;
boolean stateful() default false;
injavaapi中文在线看t maxAttempts() default 3;
String maxAttemptsExpression() default "";
Backoff backof业务所是干什么的f() default @Backoff()java面试题;
String exceptionExpression() default "";
String[]接口类型 listeners() default {};
}
@Target(ElementThttp 404ype.TYPE)
@Retentappointmention(RetentionPolicy.RUNTIME)
@Documented
public @interface Backofhttp://192.168.1.1登录f {
long value() default 1000;
long delay() default 0;
long maxhttp://192.168.1.1登录Delay() defauappstorelt 0;
double multiplier() default 0;
String delay业务性作业Expression() default "";
String maxDelayExpression() default "Java";
String multiplierExpression() default "";
booleanapp是什么意思 random() default false;
}
- 在需求重试的http协议办法上装备对应的重试次数、重试异常的异常类型、设置回退推迟时刻、重试战略、办法监听称谓
@Component
puapp下载blic class PlatformClassService {
@Retryhttpwatchable(
// 重试异常的异常类型
value = {Exception.class},
// 最大重试次数
maxAtt接口crc过错计数empt接口crc过错计数s = 5,
// 设置业务文书回退推迟时刻
backoff = @Backoff(delay = 500),
// 装备回调办法称谓
listeners = "retryListener"
)
public void call() {
System.业务的四个特性out.println("call...");
throw new RuntimeException("手艺异常");http://192.168.1.1登录
}
}
@Retryable(value = {Exception.appearanceclass}, maxAttempts = 4, backoff = @Backoff(delay = 2000L, muljavaeetiplier = 1.5))
- 监听办法,在装备类中进行装备
@Be业务所an
public Retry业务所Listener re接口tryListener() {
return new RetryListener() {
@Overridhttp 404e
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
System.out.println业务的四个特性("open context =appreciate " + context + ", calappstorelback = " + callback);
return true;
}
@Override
p接口是什么ublic <T, E extends Throwable&app下载gt; void cljava难学吗ose(Rjava初学etryContext context, RetryCallback<T, E> callback,
Throwable throwable) {
System.out.println("close context = " + context + ", caAPPllback业务文书简报的期数指的是 = " + callba业务性作业ck);
}
@Override
public <T, E ejava怎么读xtends Throwable> void onError(RetryContext context,业务员 Rejavaapi中文在线看tryCallback<T, E> callback,http://192.168.1.1登录
Throwaapp是什么意思ble throwable) {
System.out.println("onErrojava言语r context = " + context + ", callback = " + callback);
}
};
}
- 调用服务
@RestController
public class SpringRetryCon接口和抽象类的差异troller {
@Resource
private Platfor业务mClassService platformhttp 404Clashttp 302sService;
@RequestMapping("/接口和抽象类的差异retryPlatformCall")
public Object retryPlatformCall() {
try {
platformClass接口是什么Service.ca接口和抽象类的差异ll();
} catch (Exception e) {
return "尝试调用失利";
}
return "retryPlatformCall";
}
}
- 调用成果