Advertisement
Guest User

RetryingTransactionAspect.java

a guest
Apr 7th, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. @Component
  2. @Aspect
  3. public class RetryingTransactionAspect implements Ordered {
  4.  
  5.     private final Logger logger = LoggerFactory.getLogger(RetryingTransactionAspect.class);
  6.  
  7.     @Inject
  8.     @Named("transactionManager")
  9.     private PlatformTransactionManager transactionManager;
  10.  
  11.     @Pointcut("execution(public * * (..))")
  12.     private void anyPublicMethod() {
  13.     }
  14.  
  15.     @Pointcut("execution(@RetryingTransaction * * (..))")
  16.     private void anyRetryingTransactionAnnotatedMethod() {
  17.     }
  18.  
  19.     @Around("anyPublicMethod() && anyRetryingTransactionAnnotatedMethod()")
  20.     private Object retryOperation(ProceedingJoinPoint joinPoint) throws Throwable {
  21.         Signature signature = joinPoint.getSignature();
  22.         if (!(signature instanceof MethodSignature)) {
  23.             logger.error("unsupported join point signature {}", signature);
  24.             return doNotApply(joinPoint);
  25.         }
  26.  
  27.         MethodSignature methodSignature = (MethodSignature) signature;
  28.         Method targetMethod = methodSignature.getMethod();
  29.         RetryingTransaction annotation = targetMethod.getAnnotation(RetryingTransaction.class);
  30.         if (annotation == null) {
  31.             logger.error("advised method {} is missing the required annotation of type {}",
  32.                          targetMethod,
  33.                          RetryingTransaction.class);
  34.             return doNotApply(joinPoint);
  35.         }
  36.  
  37.         int maxAttempts = annotation.repeatCount();
  38.         int attemptCount = 0;
  39.         List<Class<? extends Throwable>> exceptions = Arrays.asList(annotation.retryFor());
  40.         Throwable failure;
  41.         TransactionStatus currentTransactionStatus = null;
  42.         do {
  43.             attemptCount++;
  44.             logger.debug("try {} of {} retries for {}", attemptCount, maxAttempts, joinPoint);
  45.             try {
  46.                 DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
  47.                 transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
  48.                 currentTransactionStatus = transactionManager.getTransaction(transactionDefinition);
  49.  
  50.                 Object returnValue = joinPoint.proceed();
  51.  
  52.                 transactionManager.commit(currentTransactionStatus);
  53.  
  54.                 return returnValue;
  55.             } catch (Throwable t) {
  56.                 if (!exceptions.contains(t.getClass())) {
  57.                     throw t;
  58.                 }
  59.                 if (currentTransactionStatus != null) {
  60.                     transactionManager.rollback(currentTransactionStatus);
  61.                 }
  62.                 failure = t;
  63.             }
  64.         } while (attemptCount < maxAttempts);
  65.         logger.error("max retry attempts reached");
  66.         throw failure;
  67.     }
  68.  
  69.     public Object doNotApply(ProceedingJoinPoint joinPoint) throws Throwable {
  70.         logger.info("advice will not apply!");
  71.         return joinPoint.proceed();
  72.     }
  73.  
  74.     public int getOrder() {
  75.         return 1;
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement