spring core aop : log trace - spring aop (@Trace, @Retry)

This commit is contained in:
haerong22
2021-11-28 00:20:31 +09:00
parent f691f3b2bd
commit 03b2085dac
7 changed files with 80 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
package com.example.aop.exam;
import com.example.aop.exam.annotation.Retry;
import com.example.aop.exam.annotation.Trace;
import org.springframework.stereotype.Repository;
@Repository
@@ -10,6 +12,8 @@ public class ExamRepository {
/**
* 5번에 1번 실패하는 요청
*/
@Trace
@Retry
public String save(String itemId) {
seq++;
if (seq % 5 == 0) {

View File

@@ -1,5 +1,6 @@
package com.example.aop.exam;
import com.example.aop.exam.annotation.Trace;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -9,6 +10,7 @@ public class ExamService {
private final ExamRepository examRepository;
@Trace
public void request(String itemId) {
examRepository.save(itemId);
}

View File

@@ -0,0 +1,12 @@
package com.example.aop.exam.annotation;
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 Retry {
int value() default 3;
}

View File

@@ -0,0 +1,11 @@
package com.example.aop.exam.annotation;
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 Trace {
}

View File

@@ -0,0 +1,30 @@
package com.example.aop.exam.aop;
import com.example.aop.exam.annotation.Retry;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Slf4j
@Aspect
public class RetryAspect {
@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;
}
}

View File

@@ -0,0 +1,17 @@
package com.example.aop.exam.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Slf4j
@Aspect
public class TraceAspect {
@Before("@annotation(com.example.aop.exam.annotation.Trace)")
public void doTrace(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
log.info("[trace] {} args={}", joinPoint.getSignature(), args);
}
}

View File

@@ -1,11 +1,15 @@
package com.example.aop.exam;
import com.example.aop.exam.aop.RetryAspect;
import com.example.aop.exam.aop.TraceAspect;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
@Slf4j
@Import({TraceAspect.class, RetryAspect.class})
@SpringBootTest
public class ExamTest {