spring-core源码走读
Posted 龘驫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-core源码走读相关的知识,希望对你有一定的参考价值。
asm
-
ASM is an all purpose Java bytecode manipulation and analysis framework.
-
ASM是一个万能的java字节码操纵和分析框架
cglib
-
Byte Code Generation Library is high level API to generate and transform JAVA byte code
-
高层级的生成和转换Java字节码的字节码生成库
objenesis
-
Objenesis is a small Java library that serves one purpose:To instantiate a new object of a particular class.
-
Objenesis是一个只为特殊类初始化对象小型Java库
lang
-
定义八个注解
-
NonNull.class
-
NonNullApi.class
-
NonNullFields.class
-
Nullable.class
-
UsesJava7.class
-
UsesJava8.class
-
UsesSunHttpServer.class
-
UsesSunMisc.class
-
util
backoff
重试算法
-
定义退避算法
-
BackOff.class
-
BackOffExecution.class
-
ExponentialBackOff.class 指数重试间隔实现
-
FixedBackOff.class 固定重试间隔实现
-
-
用于封装重试逻辑
使用说明
BackOffExecution exec = backOff.start();
?
// In the operation recovery/retry loop:
long waitInterval = exec.nextBackOff();
if (waitInterval == BackOffExecution.STOP) {
// do not retry operation
}
else {
// sleep, e.g. Thread.sleep(waitInterval)
// retry operation
}
}
测试代码
package com.zby.util.backoff;
?
import java.util.Date;
?
import org.springframework.util.backoff.BackOff;
import org.springframework.util.backoff.BackOffExecution;
import org.springframework.util.backoff.ExponentialBackOff;
import org.springframework.util.backoff.FixedBackOff;
?
public class BackoffDemo {
?
public static void main(String[] args) throws Exception {
//BackOff backOff = new FixedBackOff(1000, 10);
BackOff backOff = new ExponentialBackOff(500, 2);
BackOffExecution backOffExecution = backOff.start();
int successTime=7;
for(int i=0;;i++) {
System.out.println("重试:"+backOffExecution);
long waitInterval = backOffExecution.nextBackOff();
if (waitInterval == BackOffExecution.STOP) {
System.out.println("执行失败:" + backOffExecution);
break;
} else {
Thread.sleep(waitInterval);
System.out.println("准备执行操作,当前时间:"+new Date());
boolean success = operation(successTime==i);
if(success) {
break;
}
}
}
?
}
?
public static boolean operation(boolean success) {
System.out.println("执行结果:"+success);
return success;
}
?
}
?
-
可以封装一下把公共代码封装起来,提供重试机制,如:
package com.zby.util.backoff;
?
import java.util.Date;
?
import org.springframework.util.backoff.BackOff;
import org.springframework.util.backoff.BackOffExecution;
import org.springframework.util.backoff.FixedBackOff;
?
public abstract class FixedBackOffRunnable implements Runnable{
private BackOffExecution backOffExecution;
public FixedBackOffRunnable(long interval, long maxAttempts) {
BackOff backOff = new FixedBackOff(interval, maxAttempts);
backOffExecution = backOff.start();
}
public void run() {
while (true) {
System.out.println("重试:" + backOffExecution);
long waitInterval = backOffExecution.nextBackOff();
if (waitInterval == BackOffExecution.STOP) {
System.out.println("执行失败:" + backOffExecution);
break;
} else {
try {
Thread.sleep(waitInterval);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("准备执行操作,当前时间:" + new Date());
boolean retry=false;
try {
doRun();
} catch (RetryableExeception e) {
retry=true;
}
if(!retry) {