Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[스프링AOP실전] 재시도AOP 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

[스프링AOP실전] 재시도AOP

Seung__ 2022. 1. 9. 13:38

@Retry 애노테이션이 있으면 예외가 발생했을 때 다시 시도해서 문제를 복구한다.

 

1. @Retry


  • Retry방식을 사용할 때 항상 재시도 횟수가 정해져 있어야 함.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Retry {
    int value() default 3;

    //Retry는 항상 재시도 횟수가 있어야함..
}

 

 

2. RetryAspect


@Slf4j
@Aspect
public class RetryAspect {
    /*
    @Around("@annotation(hello.aop.exam.annotation.Retry)")
    public Object doRetry(ProceedingJoinPoint joinPoint, Retry retry){
    }*/

    @Around("@annotation(retry)")
    public Object doRetry(ProceedingJoinPoint joinPoint, Retry retry) throws Throwable {
        log.info("[retry] {} retry={}", joinPoint.getSignature(), retry);

        int maxRetry = retry.value();
        Exception exceptionHolder = null;

        for (int retryCount=1; retryCount<=maxRetry; retryCount++){
            try {
                log.info("[retry] try count={}/{}", retryCount, maxRetry);
                return joinPoint.proceed();
            }catch (Exception e){
                exceptionHolder = e;
            }
        }

        throw exceptionHolder;
    }
}

 

  • @annotation(retry) , Retry retry 를 사용해서 어드바이스에 애노테이션을 파라미터로 전달한다.
  • retry.value() 를 통해서 애노테이션에 지정한 값을 가져올 수 있다.
  • 예외가 발생해서 결과가 정상 반환되지 않으면 retry.value() 만큼 재시도한다.

 

 

3.  Repository에 적용


@Repository
public class ExamRepository {

    private static int seq = 0;

    /**
     * 5번에 한 번 실패하는 요청
     */
    @Trace
    @Retry(value = 4) //value 변경 가능
    public String save(String itemId){
        seq++;
        if (seq%5 == 0){
            throw new IllegalStateException("예외 발생");
        }

        return "ok";
    }
}
  • value값 변경 가능

 

 

 

4. Test


@Slf4j
//@Import(TraceAspect.class)
@Import({TraceAspect.class, RetryAspect.class})
@SpringBootTest
class ExamTest {

    @Autowired
    ExamService examService;

    @Test
    void test(){
        for (int i=0; i<5; i++){
            log.info("client request i={}", i);
            examService.request("data"+i);
        }
    }
}
client request i=0
[trace] void hello.aop.exam.ExamService.request(String), args=[data0]
[trace] String hello.aop.exam.ExamRepository.save(String), args=[data0]
[retry] String hello.aop.exam.ExamRepository.save(String) retry=@hello.aop.exam.annotation.Retry(value=4)
[retry] try count=1/4
client request i=1
[trace] void hello.aop.exam.ExamService.request(String), args=[data1]
[trace] String hello.aop.exam.ExamRepository.save(String), args=[data1]
[retry] String hello.aop.exam.ExamRepository.save(String) retry=@hello.aop.exam.annotation.Retry(value=4)
[retry] try count=1/4
client request i=2
[trace] void hello.aop.exam.ExamService.request(String), args=[data2]
[trace] String hello.aop.exam.ExamRepository.save(String), args=[data2]
[retry] String hello.aop.exam.ExamRepository.save(String) retry=@hello.aop.exam.annotation.Retry(value=4)
[retry] try count=1/4
client request i=3
[trace] void hello.aop.exam.ExamService.request(String), args=[data3]
[trace] String hello.aop.exam.ExamRepository.save(String), args=[data3]
[retry] String hello.aop.exam.ExamRepository.save(String) retry=@hello.aop.exam.annotation.Retry(value=4)
[retry] try count=1/4
client request i=4
[trace] void hello.aop.exam.ExamService.request(String), args=[data4]
[trace] String hello.aop.exam.ExamRepository.save(String), args=[data4]
[retry] String hello.aop.exam.ExamRepository.save(String) retry=@hello.aop.exam.annotation.Retry(value=4)
[retry] try count=1/4
[retry] try count=2/4

 

 

 

5. GitHub: 220109 @Retry


https://github.com/bsh6463/SpringAOP

 

GitHub - bsh6463/SpringAOP

Contribute to bsh6463/SpringAOP development by creating an account on GitHub.

github.com

 

Comments