import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RetryOnFailure {
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class RetryAspect {
public static final int MAX_RETRY_TIMES = 5;//重试次数
//切点
@Pointcut("execution(* com.aaad.aaa..*.dao..*(..))")
public void retryOnFailure(){}
@Around("retryOnFailure()")
public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
int numAttempts = 0;
OptimisticLockingFailureException lockFailureException;
do {
numAttempts++;
try {
return pjp.proceed();
}catch(OptimisticLockingFailureException ex) {
lockFailureException = ex;
}
}while(numAttempts <= MAX_RETRY_TIMES);
System.out.println("lyx*************"+numAttempts);
throw lockFailureException;
}
}