spring-core源码走读

2021-02-13 15:17

阅读:489

标签:uid   替换字符串   uuid   comment   用户   value   eric   parameter   success   

Spring-core 5.0.8

asm

  • ASM is an all purpose Java bytecode manipulation and analysis framework.

  • ASM是一个万能的java字节码操纵和分析框架

  • asm官网

  • ASM和访问者模式

  • ASM库的介绍和使用

cglib

  • Byte Code Generation Library is high level API to generate and transform JAVA byte code

  • 高层级的生成和转换Java字节码的字节码生成库

  • cglib github地址

objenesis

  • Objenesis is a small Java library that serves one purpose:To instantiate a new object of a particular class.

  • Objenesis是一个只为特殊类初始化对象小型Java库

  • objenesis官网

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) {


评论


亲,登录后才可以留言!