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(!

相关文章:

  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2021-05-11
  • 2021-08-04
  • 2021-10-14
  • 2021-06-20
猜你喜欢
  • 2019-02-26
  • 2021-10-03
  • 2022-02-13
  • 2022-12-23
  • 2021-12-16
  • 2022-01-07
相关资源
相似解决方案