package com.dz.modules.tencent.utils.speech;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
@Aspect
@Configuration
@Slf4j
public class TryAgainAspect {
@Around("@annotation(FailRetry)")
@Transactional(rollbackFor = Exception.class)
public Object retry(ProceedingJoinPoint joinPoint) throws Throwable {
int count = 0;
int num = 0;
do {
count++;
try {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
num = methodSignature.getMethod().getAnnotation(FailRetry.class).value();
Object proceed = joinPoint.proceed();
return joinPoint.proceed();
} catch (TryAgainException e) {
log.info("重试次数:{}", num);
if (count >= num) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
throw new TryAgainException("重试失败");
}
}
} while (true);
}
}
public class TryAgainException extends RuntimeException{
public TryAgainException(String e) {
super(e);
}
}
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FailRetry {
int value() default 15;
}
@Override
@Transactional(rollbackFor = Exception.class)
@FailRetry
public R<List<TencentSpeechResponseVO>> speechToText(Long id, Integer type) {
int i = configMapper.updateTencentBalance(tenantId, totalAmount, version);
if(i==0){
throw new TryAgainException("更新异常,版本号不一致");
}
}