1. 抽象类:
具体业务实现doProcess()即可。可继承CallBack类,亦可new Callback类。再不明白可查看下方demo
package com.nami.retry;
/**
* 描述:
*
* @Author: lbc
* @Date: 2024-08-04 15:19
* @email: 594599620@qq.com
* @Description: keep coding
*/
public abstract class Callback {
/**
* do
*/
public abstract RetryResult doProcess();
}
2. 执行结果:
package com.nami.retry;
/**
* 描述:
*
* @Author: lbc
* @Date: 2024-08-04 15:19
* @email: 594599620@qq.com
* @Description: keep coding
*/
public class RetryResult {
private boolean isRetry;
private Object obj;
public RetryResult(boolean isRetry, Object obj) {
this.isRetry = isRetry;
this.obj = obj;
}
public RetryResult(boolean isRetry) {
this.isRetry = isRetry;
}
public boolean isRetry() {
return isRetry;
}
public void setRetry(boolean retry) {
isRetry = retry;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public static RetryResult ofResult(boolean retry, Object obj) {
return new RetryResult(retry, obj);
}
public static RetryResult ofResult(boolean isRetry) {
return new RetryResult(isRetry);
}
}
3. 执行器:
package com.nami.retry;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
/**
* 描述:
*
* @Author: lbc
* @Date: 2024-08-04 15:23
* @email: 594599620@qq.com
* @Description: keep coding
*/
@Slf4j
public class RetryExecutor {
public static Object execute(int retryCount, Callback callback) {
for (int curRetryCount = 0; curRetryCount < retryCount; curRetryCount++) {
RetryResult retryResult = callback.doProcess();
if (retryResult.isRetry()) {
continue;
}
return retryResult.getObj();
}
return null;
}
/**
* 改变for写法,变高级点
*
* @param retryCount
* @param callback
* @return
*/
public static Object execute2(int retryCount, Callback callback) {
int curRetryCount = 0;
for (;;) {
if (curRetryCount >= retryCount) {
break;
}
curRetryCount++;
RetryResult retryResult = callback.doProcess();
if (retryResult.isRetry()) {
continue;
}
return retryResult.getObj();
}
return null;
}
/**
* execute
*
* @param retryCount 重试次数
* @param millsSeconds 线程休眠时间,单位 ms
* @param callback 执行方法
* @return
*/
public static Object execute(int retryCount, long millsSeconds, Callback callback) {
if (retryCount <= 0) {
throw new IllegalArgumentException("执行次数必须大于0!");
}
RetryResult retryResult = null;
int executeCount = 0;
while (true) {
if (executeCount >= retryCount) {
break;
}
executeCount++;
// 进入实现类方法,执行具体业务逻辑
retryResult = callback.doProcess();
if (retryResult.isRetry()) {
// 如果需要重试,并且需要间隔时间,那么执行线程等待
if (millsSeconds > 0) {
try {
TimeUnit.MILLISECONDS.sleep(millsSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
continue;
}
return retryResult.getObj();
}
if (retryResult != null && retryResult.getObj() != null) {
return retryResult.getObj();
}
return null;
}
public static void main(String[] args) {
Object result = execute(3, 3000, new Callback() {
@Override
public RetryResult doProcess() {
log.info("[retry-do] hello, world!");
// String result = HttpUtil.get("https://www.baidu.com");
// log.info("[retry-do] result: {}", result);
return RetryResult.ofResult(false, "结果");
// return RetryResult.ofResult(true);
}
}
);
log.info("[retry-do] result={}", result);
}
}
注:笔者水平有限,如有错误,欢迎指正!
4. 如果需要自己的策略,可在此基础,进行魔改!