반응형

설명

  • spring-retry를 활용하여 실패한 동작을 자동으로 다시 호출하는 기능을 구현
  • 일시적인 네트워크 결함과 같은 오류 처리에 유용하다.

dependencies

dependencies {
    ...
    implementation 'org.springframework.retry:spring-retry:1.3.1'
    implementation 'org.springframework:spring-aspects:5.2.15.RELEASE'    
}

Enable Spring Retry

@EnableRetry // 추가
@SpringBootApplication
public class SpringRetryableApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringRetryableApplication.class, args);
    }
}

Service

@Slf4j
@Service
public class TestService {
    private static int retryCount = 0;

    @Retryable(value = {IllegalStateException.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000))
    public void test() {
        log.info("retryCount : {}", retryCount++);
        throw new IllegalStateException("for test");
    }

    @Recover
    public void recover() {
        log.info("recover");
    }
}

Test

@SpringBootTest
public class TestServiceTest {
    @Autowired
    private TestService testService;

    @Test
    public void test() {
        testService.test();
    }
}

Result

retryCount : 0
retryCount : 1
retryCount : 2
recover

참고

반응형

'Development > Spring' 카테고리의 다른 글

[Spring] Model Mapping  (0) 2021.06.13
[Spring] Spring Security  (0) 2021.05.29
[Spring] DataSource  (0) 2021.05.19
[Spring] Testcontainers  (0) 2021.04.15
[Spring] Server Sent Event(SSE)  (0) 2021.03.30

+ Recent posts