feat: 1.3.0
1. 优化手动重试模块
This commit is contained in:
parent
c2aa5ba79a
commit
0e6d43681a
@ -9,29 +9,8 @@ package com.aizuda.easy.retry.client.core;
|
|||||||
public interface RetryOperations {
|
public interface RetryOperations {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成重试任务并上报服务端,且存在嵌套重试情况下不上报服务端
|
* 执行重试
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void generateAsyncTask();
|
void executeRetry();
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成重试任务并上报服务端
|
|
||||||
*
|
|
||||||
* @param forceReport true: 表示存在嵌套重试情况下强制上报服务端 false: 表示存在嵌套重试情况下不上报服务端
|
|
||||||
*/
|
|
||||||
void generateAsyncTask(boolean forceReport);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成重试任务并上报服务端,且存在嵌套重试情况下不上报服务端
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
Boolean generateSyncTask();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成重试任务并上报服务端
|
|
||||||
*
|
|
||||||
* @param forceReport true: 表示存在嵌套重试情况下强制上报服务端 false: 表示存在嵌套重试情况下不上报服务端
|
|
||||||
*/
|
|
||||||
Boolean generateSyncTask(boolean forceReport);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import java.lang.annotation.Inherited;
|
|||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在使用手动生成重试任务时,通过ExecutorMethodRegister配置注册重试场景
|
* 在使用手动生成重试任务时,通过ExecutorMethodRegister配置注册重试场景
|
||||||
@ -64,4 +65,32 @@ public @interface ExecutorMethodRegister {
|
|||||||
*/
|
*/
|
||||||
String bizNo() default "";
|
String bizNo() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异步上报数据到服务端
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
boolean async() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否强制上报数据到服务端
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
boolean forceReport() default false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步(async:false)上报数据需要配置超时时间
|
||||||
|
*
|
||||||
|
* @return 超时时间
|
||||||
|
*/
|
||||||
|
long timeout() default 60 * 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 超时时间单位
|
||||||
|
*
|
||||||
|
* @return TimeUnit
|
||||||
|
*/
|
||||||
|
TimeUnit unit() default TimeUnit.MILLISECONDS;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import com.aizuda.easy.retry.client.core.callback.SimpleRetryCompleteCallback;
|
|||||||
import com.aizuda.easy.retry.client.core.retryer.RetryType;
|
import com.aizuda.easy.retry.client.core.retryer.RetryType;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重试接入入口
|
* 重试接入入口
|
||||||
@ -101,5 +102,26 @@ public @interface Retryable {
|
|||||||
*/
|
*/
|
||||||
boolean isThrowException() default true;
|
boolean isThrowException() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异步上报数据到服务端
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
boolean async() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步(async:false)上报数据需要配置超时时间
|
||||||
|
*
|
||||||
|
* @return 超时时间
|
||||||
|
*/
|
||||||
|
long timeout() default 60 * 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 超时时间单位
|
||||||
|
*
|
||||||
|
* @return TimeUnit
|
||||||
|
*/
|
||||||
|
TimeUnit unit() default TimeUnit.MILLISECONDS;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.aizuda.easy.retry.client.core.executor;
|
package com.aizuda.easy.retry.client.core.executor;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import com.aizuda.easy.retry.client.core.RetryExecutorParameter;
|
import com.aizuda.easy.retry.client.core.RetryExecutorParameter;
|
||||||
import com.aizuda.easy.retry.client.core.cache.RetryerInfoCache;
|
import com.aizuda.easy.retry.client.core.cache.RetryerInfoCache;
|
||||||
|
import com.aizuda.easy.retry.client.core.exception.EasyRetryClientException;
|
||||||
import com.github.rholder.retry.*;
|
import com.github.rholder.retry.*;
|
||||||
import com.aizuda.easy.retry.common.core.log.LogUtils;
|
import com.aizuda.easy.retry.common.core.log.LogUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -10,14 +12,18 @@ import java.util.concurrent.Callable;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Guava 重试执行器
|
||||||
|
*
|
||||||
* @author: www.byteblogs.com
|
* @author: www.byteblogs.com
|
||||||
* @date : 2022-03-03 18:07
|
* @date : 2022-03-03 18:07
|
||||||
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GuavaRetryExecutor extends AbstractRetryExecutor<WaitStrategy, StopStrategy> {
|
public class GuavaRetryExecutor extends AbstractRetryExecutor<WaitStrategy, StopStrategy> {
|
||||||
|
|
||||||
public GuavaRetryExecutor(String sceneName, String executorClassName) {
|
public GuavaRetryExecutor(String sceneName, String executorClassName) {
|
||||||
retryerInfo = RetryerInfoCache.get(sceneName, executorClassName);
|
retryerInfo = RetryerInfoCache.get(sceneName, executorClassName);
|
||||||
|
Assert.notNull(retryerInfo, () -> new EasyRetryClientException("retryerInfo is null sceneName:[{}] executorClassName:[{}]", sceneName, executorClassName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public GuavaRetryExecutor() {
|
public GuavaRetryExecutor() {
|
||||||
|
@ -23,6 +23,7 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 扫描手动注入重试方法
|
* 扫描手动注入重试方法
|
||||||
@ -61,6 +62,10 @@ public class ExecutorMethodScanner implements Scanner, ApplicationContextAware {
|
|||||||
Class<? extends IdempotentIdGenerate> idempotentIdGenerate = retryable.idempotentId();
|
Class<? extends IdempotentIdGenerate> idempotentIdGenerate = retryable.idempotentId();
|
||||||
Method executorMethodName = executorNotProxy.getMethod("doExecute", Object.class);
|
Method executorMethodName = executorNotProxy.getMethod("doExecute", Object.class);
|
||||||
Class<? extends RetryCompleteCallback> retryCompleteCallback = retryable.retryCompleteCallback();
|
Class<? extends RetryCompleteCallback> retryCompleteCallback = retryable.retryCompleteCallback();
|
||||||
|
boolean async = retryable.async();
|
||||||
|
long timeout = retryable.timeout();
|
||||||
|
TimeUnit unit = retryable.unit();
|
||||||
|
boolean forceReport = retryable.forceReport();
|
||||||
|
|
||||||
return new RetryerInfo(retryable.scene(),
|
return new RetryerInfo(retryable.scene(),
|
||||||
executorClassName,
|
executorClassName,
|
||||||
@ -69,13 +74,17 @@ public class ExecutorMethodScanner implements Scanner, ApplicationContextAware {
|
|||||||
executor,
|
executor,
|
||||||
executorMethodName,
|
executorMethodName,
|
||||||
RetryType.ONLY_REMOTE,
|
RetryType.ONLY_REMOTE,
|
||||||
0,
|
1,
|
||||||
0,
|
1,
|
||||||
idempotentIdGenerate,
|
idempotentIdGenerate,
|
||||||
StringUtils.EMPTY,
|
StringUtils.EMPTY,
|
||||||
(Class<? extends ExecutorMethod>) executor.getClass(),
|
(Class<? extends ExecutorMethod>) executor.getClass(),
|
||||||
true,
|
Boolean.TRUE,
|
||||||
retryCompleteCallback
|
retryCompleteCallback,
|
||||||
|
async,
|
||||||
|
forceReport,
|
||||||
|
timeout,
|
||||||
|
unit
|
||||||
);
|
);
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
LogUtils.error(log, "{}重试信息加载报错:{}", executor.getClass().getName(), e);
|
LogUtils.error(log, "{}重试信息加载报错:{}", executor.getClass().getName(), e);
|
||||||
|
@ -18,7 +18,12 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: www.byteblogs.com
|
* @author: www.byteblogs.com
|
||||||
@ -45,7 +50,8 @@ public class RetryableScanner implements Scanner, ApplicationContextAware {
|
|||||||
Map<Method, Retryable> annotatedMethods = null;
|
Map<Method, Retryable> annotatedMethods = null;
|
||||||
try {
|
try {
|
||||||
annotatedMethods = MethodIntrospector.selectMethods(bean.getClass(),
|
annotatedMethods = MethodIntrospector.selectMethods(bean.getClass(),
|
||||||
(MethodIntrospector.MetadataLookup<Retryable>) method -> AnnotatedElementUtils.findMergedAnnotation(method, Retryable.class));
|
(MethodIntrospector.MetadataLookup<Retryable>) method -> AnnotatedElementUtils
|
||||||
|
.findMergedAnnotation(method, Retryable.class));
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
LogUtils.error(log, "{}重试信息加载报错:{}", beanDefinitionName, ex);
|
LogUtils.error(log, "{}重试信息加载报错:{}", beanDefinitionName, ex);
|
||||||
}
|
}
|
||||||
@ -80,6 +86,9 @@ public class RetryableScanner implements Scanner, ApplicationContextAware {
|
|||||||
Class<? extends ExecutorMethod> retryMethod = retryable.retryMethod();
|
Class<? extends ExecutorMethod> retryMethod = retryable.retryMethod();
|
||||||
boolean throwException = retryable.isThrowException();
|
boolean throwException = retryable.isThrowException();
|
||||||
Class<? extends RetryCompleteCallback> retryCompleteCallback = retryable.retryCompleteCallback();
|
Class<? extends RetryCompleteCallback> retryCompleteCallback = retryable.retryCompleteCallback();
|
||||||
|
boolean async = retryable.async();
|
||||||
|
long timeout = retryable.timeout();
|
||||||
|
TimeUnit unit = retryable.unit();
|
||||||
|
|
||||||
return new RetryerInfo(retryable.scene(),
|
return new RetryerInfo(retryable.scene(),
|
||||||
executorClassName,
|
executorClassName,
|
||||||
@ -94,7 +103,11 @@ public class RetryableScanner implements Scanner, ApplicationContextAware {
|
|||||||
bizNo,
|
bizNo,
|
||||||
retryMethod,
|
retryMethod,
|
||||||
throwException,
|
throwException,
|
||||||
retryCompleteCallback
|
retryCompleteCallback,
|
||||||
|
async,
|
||||||
|
Boolean.FALSE, // 基于注解的上报不允许强制上报
|
||||||
|
timeout,
|
||||||
|
unit
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package com.aizuda.easy.retry.client.core.retryer;
|
package com.aizuda.easy.retry.client.core.retryer;
|
||||||
|
|
||||||
import com.aizuda.easy.retry.client.core.RetryOperations;
|
import com.aizuda.easy.retry.client.core.RetryOperations;
|
||||||
import com.aizuda.easy.retry.client.core.report.ReportHandler;
|
|
||||||
import com.aizuda.easy.retry.client.core.strategy.ExecutorMethod;
|
import com.aizuda.easy.retry.client.core.strategy.ExecutorMethod;
|
||||||
|
import com.aizuda.easy.retry.client.core.strategy.RetryStrategy;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手动生成重试任务模板类
|
* 手动生成重试任务模板类
|
||||||
@ -18,36 +16,11 @@ public class EasyRetryTemplate implements RetryOperations {
|
|||||||
private Class<? extends ExecutorMethod> executorMethodClass;
|
private Class<? extends ExecutorMethod> executorMethodClass;
|
||||||
private String scene;
|
private String scene;
|
||||||
private Object[] params;
|
private Object[] params;
|
||||||
private ReportHandler reportHandler;
|
private RetryStrategy retryStrategy;
|
||||||
private long timeout = 60 * 1000;
|
|
||||||
private TimeUnit unit = TimeUnit.MILLISECONDS;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateAsyncTask() {
|
public void executeRetry() {
|
||||||
generateAsyncTask(Boolean.FALSE);
|
retryStrategy.openRetry(scene, executorMethodClass.getName(), params);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void generateAsyncTask(boolean forceReport) {
|
|
||||||
if (forceReport) {
|
|
||||||
reportHandler.asyncReportWithForce(scene, executorMethodClass.getName(), params);
|
|
||||||
} else {
|
|
||||||
reportHandler.asyncReport(scene, executorMethodClass.getName(), params);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean generateSyncTask() {
|
|
||||||
return generateSyncTask(Boolean.FALSE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean generateSyncTask(final boolean forceReport) {
|
|
||||||
if (forceReport) {
|
|
||||||
return reportHandler.syncReportWithForce(scene, executorMethodClass.getName(), params, timeout, unit);
|
|
||||||
} else {
|
|
||||||
return reportHandler.syncReport(scene, executorMethodClass.getName(), params, timeout, unit);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setExecutorMethodClass(
|
protected void setExecutorMethodClass(
|
||||||
@ -63,15 +36,7 @@ public class EasyRetryTemplate implements RetryOperations {
|
|||||||
this.params = params;
|
this.params = params;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setReportHandler(final ReportHandler reportHandler) {
|
protected void setRetryStrategy(final RetryStrategy retryStrategy) {
|
||||||
this.reportHandler = reportHandler;
|
this.retryStrategy = retryStrategy;
|
||||||
}
|
|
||||||
|
|
||||||
protected void setTimeout(final long timeout) {
|
|
||||||
this.timeout = timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setUnit(final TimeUnit unit) {
|
|
||||||
this.unit = unit;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package com.aizuda.easy.retry.client.core.retryer;
|
package com.aizuda.easy.retry.client.core.retryer;
|
||||||
|
|
||||||
import com.aizuda.easy.retry.client.core.report.ReportHandler;
|
|
||||||
import com.aizuda.easy.retry.client.core.strategy.ExecutorMethod;
|
import com.aizuda.easy.retry.client.core.strategy.ExecutorMethod;
|
||||||
|
import com.aizuda.easy.retry.client.core.strategy.LocalRetryStrategies;
|
||||||
|
import com.aizuda.easy.retry.client.core.strategy.RetryStrategy;
|
||||||
import com.aizuda.easy.retry.common.core.context.SpringContext;
|
import com.aizuda.easy.retry.common.core.context.SpringContext;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建重试模板对象
|
* 构建重试模板对象
|
||||||
*
|
*
|
||||||
@ -17,8 +16,6 @@ public class RetryTaskTemplateBuilder {
|
|||||||
private Class<? extends ExecutorMethod> executorMethodClass;
|
private Class<? extends ExecutorMethod> executorMethodClass;
|
||||||
private String scene;
|
private String scene;
|
||||||
private Object[] params;
|
private Object[] params;
|
||||||
private long timeout = 60*1000;
|
|
||||||
private TimeUnit unit = TimeUnit.MILLISECONDS;
|
|
||||||
|
|
||||||
public static RetryTaskTemplateBuilder newBuilder() {
|
public static RetryTaskTemplateBuilder newBuilder() {
|
||||||
return new RetryTaskTemplateBuilder();
|
return new RetryTaskTemplateBuilder();
|
||||||
@ -39,25 +36,13 @@ public class RetryTaskTemplateBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RetryTaskTemplateBuilder timeout(long timeout) {
|
|
||||||
this.timeout = timeout;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RetryTaskTemplateBuilder unit(TimeUnit unit) {
|
|
||||||
this.unit = unit;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EasyRetryTemplate build() {
|
public EasyRetryTemplate build() {
|
||||||
EasyRetryTemplate easyRetryTemplate = new EasyRetryTemplate();
|
EasyRetryTemplate easyRetryTemplate = new EasyRetryTemplate();
|
||||||
easyRetryTemplate.setParams(params);
|
easyRetryTemplate.setParams(params);
|
||||||
easyRetryTemplate.setExecutorMethodClass(executorMethodClass);
|
easyRetryTemplate.setExecutorMethodClass(executorMethodClass);
|
||||||
easyRetryTemplate.setScene(scene);
|
easyRetryTemplate.setScene(scene);
|
||||||
easyRetryTemplate.setTimeout(timeout);
|
RetryStrategy retryStrategy = SpringContext.getBeanByType(LocalRetryStrategies.class);
|
||||||
easyRetryTemplate.setUnit(unit);
|
easyRetryTemplate.setRetryStrategy(retryStrategy);
|
||||||
ReportHandler reportHandler = SpringContext.CONTEXT.getBean(ReportHandler.class);
|
|
||||||
easyRetryTemplate.setReportHandler(reportHandler);
|
|
||||||
return easyRetryTemplate;
|
return easyRetryTemplate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,14 @@ import lombok.Data;
|
|||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 定义重试场景的信息
|
||||||
|
*
|
||||||
* @author: www.byteblogs.com
|
* @author: www.byteblogs.com
|
||||||
* @date : 2022-03-03 15:06
|
* @date : 2022-03-03 15:06
|
||||||
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@ -31,4 +35,10 @@ public class RetryerInfo {
|
|||||||
private final Class<? extends ExecutorMethod> executorMethod;
|
private final Class<? extends ExecutorMethod> executorMethod;
|
||||||
private final boolean isThrowException;
|
private final boolean isThrowException;
|
||||||
private final Class<? extends RetryCompleteCallback> retryCompleteCallback;
|
private final Class<? extends RetryCompleteCallback> retryCompleteCallback;
|
||||||
|
private final boolean async;
|
||||||
|
private final boolean forceReport;
|
||||||
|
private final long timeout;
|
||||||
|
private final TimeUnit unit;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,6 @@ public class LocalRetryStrategies extends AbstractRetryStrategies {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ReportHandler reportHandler;
|
private ReportHandler reportHandler;
|
||||||
@Autowired(required = false)
|
|
||||||
private PlatformTransactionManager platformTransactionManager;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supports(int stage, RetryType retryType) {
|
public boolean supports(int stage, RetryType retryType) {
|
||||||
@ -112,30 +110,26 @@ public class LocalRetryStrategies extends AbstractRetryStrategies {
|
|||||||
// 如果是仅仅本地重试或本地_远程模式则先支持重试
|
// 如果是仅仅本地重试或本地_远程模式则先支持重试
|
||||||
case ONLY_LOCAL:
|
case ONLY_LOCAL:
|
||||||
case LOCAL_REMOTE:
|
case LOCAL_REMOTE:
|
||||||
return () -> {
|
return () -> retryExecutor.execute(params);
|
||||||
if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
|
||||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
|
||||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
|
|
||||||
TransactionStatus transaction = platformTransactionManager.getTransaction(def);
|
|
||||||
Object execute;
|
|
||||||
try {
|
|
||||||
execute = retryExecutor.execute(params);
|
|
||||||
platformTransactionManager.commit(transaction);
|
|
||||||
} catch (Exception e) {
|
|
||||||
platformTransactionManager.rollback(transaction);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
return execute;
|
|
||||||
} else {
|
|
||||||
return retryExecutor.execute(params);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
case ONLY_REMOTE:
|
case ONLY_REMOTE:
|
||||||
// 仅仅是远程重试则直接上报
|
// 仅仅是远程重试则直接上报
|
||||||
log.debug("上报 scene:[{}]", retryerInfo.getScene());
|
log.debug("上报 scene:[{}]", retryerInfo.getScene());
|
||||||
return () -> {
|
return () -> {
|
||||||
|
|
||||||
|
if (retryerInfo.isAsync()) {
|
||||||
|
if (retryerInfo.isForceReport()) {
|
||||||
|
reportHandler.asyncReportWithForce(retryerInfo.getScene(), retryerInfo.getExecutorClassName(), params);
|
||||||
|
} else {
|
||||||
reportHandler.asyncReport(retryerInfo.getScene(), retryerInfo.getExecutorClassName(), params);
|
reportHandler.asyncReport(retryerInfo.getScene(), retryerInfo.getExecutorClassName(), params);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (retryerInfo.isForceReport()) {
|
||||||
|
reportHandler.syncReport(retryerInfo.getScene(), retryerInfo.getExecutorClassName(), params, retryerInfo.getTimeout(), retryerInfo.getUnit());
|
||||||
|
} else {
|
||||||
|
reportHandler.syncReport(retryerInfo.getScene(), retryerInfo.getExecutorClassName(), params, retryerInfo.getTimeout(), retryerInfo.getUnit());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RetrySiteSnapshot.setStage(RetrySiteSnapshot.EnumStage.REMOTE.getStage());
|
RetrySiteSnapshot.setStage(RetrySiteSnapshot.EnumStage.REMOTE.getStage());
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
@ -3,26 +3,24 @@ package com.example.demo;
|
|||||||
import com.aizuda.easy.retry.client.core.annotation.ExecutorMethodRegister;
|
import com.aizuda.easy.retry.client.core.annotation.ExecutorMethodRegister;
|
||||||
import com.aizuda.easy.retry.client.core.strategy.ExecutorMethod;
|
import com.aizuda.easy.retry.client.core.strategy.ExecutorMethod;
|
||||||
import com.aizuda.easy.retry.common.core.util.JsonUtil;
|
import com.aizuda.easy.retry.common.core.util.JsonUtil;
|
||||||
import com.example.model.Cat;
|
|
||||||
import com.example.model.Zoo;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 测试手动重试并同步上报任务
|
||||||
|
*
|
||||||
* @author: www.byteblogs.com
|
* @author: www.byteblogs.com
|
||||||
* @date : 2022-03-07 14:07
|
* @date : 2022-03-07 14:07
|
||||||
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
@ExecutorMethodRegister(scene = CustomCreateTask.SCENE)
|
@ExecutorMethodRegister(scene = CustomAsyncCreateTask.SCENE, async = true)
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CustomCreateTask implements ExecutorMethod {
|
public class CustomAsyncCreateTask implements ExecutorMethod {
|
||||||
|
|
||||||
public static final String SCENE = "customCreateTask";
|
public static final String SCENE = "customAsyncCreateTask";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object doExecute(Object obj) {
|
public Object doExecute(Object obj) {
|
||||||
log.info("测试自定义重试方法 MyExecutorMethod params:[{}]", JsonUtil.toJsonString(obj));
|
log.info(SCENE + " params:[{}]", JsonUtil.toJsonString(obj));
|
||||||
return "测试成功";
|
return "测试成功";
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import com.aizuda.easy.retry.client.core.annotation.ExecutorMethodRegister;
|
||||||
|
import com.aizuda.easy.retry.client.core.strategy.ExecutorMethod;
|
||||||
|
import com.aizuda.easy.retry.common.core.util.JsonUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试手动重试并同步上报任务
|
||||||
|
*
|
||||||
|
* @author: www.byteblogs.com
|
||||||
|
* @date : 2022-03-07 14:07
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
@ExecutorMethodRegister(scene = CustomSyncCreateTask.SCENE, async = false, timeout = 10000, unit = TimeUnit.MILLISECONDS)
|
||||||
|
@Slf4j
|
||||||
|
public class CustomSyncCreateTask implements ExecutorMethod {
|
||||||
|
|
||||||
|
public static final String SCENE = "customSyncCreateTask";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object doExecute(Object obj) {
|
||||||
|
log.info(SCENE + " params:[{}]", JsonUtil.toJsonString(obj));
|
||||||
|
return "测试成功";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
package com.example;
|
package com.example;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Assert;
|
|
||||||
import com.aizuda.easy.retry.client.core.retryer.EasyRetryTemplate;
|
import com.aizuda.easy.retry.client.core.retryer.EasyRetryTemplate;
|
||||||
import com.aizuda.easy.retry.client.core.retryer.RetryTaskTemplateBuilder;
|
import com.aizuda.easy.retry.client.core.retryer.RetryTaskTemplateBuilder;
|
||||||
import com.example.demo.CustomCreateTask;
|
import com.example.demo.CustomAsyncCreateTask;
|
||||||
|
import com.example.demo.CustomSyncCreateTask;
|
||||||
import com.example.model.Cat;
|
import com.example.model.Cat;
|
||||||
import com.example.model.Zoo;
|
import com.example.model.Zoo;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -11,10 +11,9 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: shuguang.zhang
|
* @author: www.byteblogs.com
|
||||||
* @date : 2023-05-10 13:47
|
* @date : 2023-05-10 13:47
|
||||||
*/
|
*/
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ -29,31 +28,28 @@ public class EasyRetryTemplateTest {
|
|||||||
Zoo zoo = new Zoo();
|
Zoo zoo = new Zoo();
|
||||||
zoo.setNow(LocalDateTime.now());
|
zoo.setNow(LocalDateTime.now());
|
||||||
EasyRetryTemplate retryTemplate = RetryTaskTemplateBuilder.newBuilder()
|
EasyRetryTemplate retryTemplate = RetryTaskTemplateBuilder.newBuilder()
|
||||||
.withExecutorMethod(CustomCreateTask.class)
|
.withExecutorMethod(CustomAsyncCreateTask.class)
|
||||||
.withParam(zoo)
|
.withParam(zoo)
|
||||||
.withScene(CustomCreateTask.SCENE)
|
.withScene(CustomAsyncCreateTask.SCENE)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
retryTemplate.generateAsyncTask(true);
|
retryTemplate.executeRetry();
|
||||||
|
|
||||||
Thread.sleep(90000);
|
Thread.sleep(90000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void generateSyncTask() throws InterruptedException {
|
public void generateSyncTask() {
|
||||||
|
|
||||||
Cat cat = new Cat();
|
|
||||||
cat.setName("zsd");
|
|
||||||
Zoo zoo = new Zoo();
|
Zoo zoo = new Zoo();
|
||||||
zoo.setNow(LocalDateTime.now());
|
zoo.setNow(LocalDateTime.now());
|
||||||
EasyRetryTemplate retryTemplate = RetryTaskTemplateBuilder.newBuilder()
|
EasyRetryTemplate retryTemplate = RetryTaskTemplateBuilder.newBuilder()
|
||||||
.withExecutorMethod(CustomCreateTask.class)
|
.withExecutorMethod(CustomSyncCreateTask.class)
|
||||||
.withParam(zoo)
|
.withParam(zoo)
|
||||||
.withScene(CustomCreateTask.SCENE)
|
.withScene(CustomSyncCreateTask.SCENE)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Boolean aBoolean = retryTemplate.generateSyncTask(true);
|
retryTemplate.executeRetry();
|
||||||
Assert.isTrue(aBoolean);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user