代码拉取完成,页面将自动刷新
/**
* 连接异常标记
*
* @author ithuameng
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConnectionRetry {
}
/**
* 连接异常重试切面类
*
* @author ithuameng
*/
@Component
@Aspect
public class ConnectionAspect {
private static final int MAX_RETRIES = 3;
@Around("@annotation(ConnectionRetry)")
public Object tryCount(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
int numAttempts = 0;
boolean isDone;
do {
isDone = false;
numAttempts++;
try {
result = joinPoint.proceed();
isDone = true;
} catch (SocketException e) {
System.out.println("连接异常,准备进行第" + numAttempts + "次重试!");
}
} while (numAttempts < MAX_RETRIES && !isDone);
if (!isDone) {
throw new BlsException(CommonError.BIZ_ERR, "连接超时!");
}
return result;
}
}
/**
* 乐观锁标记
*
* @author ithuameng
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Locker {
}
/**
* 并发重试标记
*
* @author ithuameng
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConcurrentRetry {
}
/**
* 乐观锁并发重试切面类
*
* @author ithuameng
*/
@Component
@Aspect
public class ConcurrentLockerAspect {
private static final int MAX_RETRIES = 2;
@Around("@annotation(ConcurrentRetry)")
public Object tryCount(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
ConcurrentException concurrentException = null;
int numAttempts = 0;
boolean isDone;
do {
isDone = false;
numAttempts++;
try {
result = joinPoint.proceed();
isDone = true;
} catch (ConcurrentException exception) {
concurrentException = exception;
System.out.println(numAttempts + " attempt...");
}
} while (numAttempts < MAX_RETRIES && !isDone);
if (!isDone) {
System.out.println("attempt " + MAX_RETRIES + " times but not work!");
throw concurrentException;
}
return result;
}
@AfterReturning(value = "@annotation(Locker)", returning = "returnValue")
public void doAfterReturning(int returnValue) {
if (returnValue == 0) {
throw new ConcurrentException();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。