Last active
May 4, 2024 00:08
-
-
Save mhewedy/5a1b68f0a19edf6b4060e74cce3d0015 to your computer and use it in GitHub Desktop.
Annotation for feign retry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Enable retries on selected Feign methods. | |
* <p> | |
* <ol> | |
* <li>In the feign config class add: | |
* <pre> | |
* {@code | |
* @Bean | |
* public Retryer feignRetryer() { | |
* return new AnnotationRetryer(new Retryer.Default()); | |
* } | |
* } | |
* </pre> | |
* </li> | |
* <li>Annotate feign method with @FeignRetryable: | |
* <pre> | |
* {@code | |
* | |
* @FeignRetryable | |
* @PostMapping("/api/url") | |
* ResponseDto callTheApi(@RequestBody RequestDto request); | |
* } | |
* </pre> | |
* </li> | |
* </ol> | |
* Now only the annotated methods will be retryable according to the delegate Retryer passed to {@link AnnotationRetryer}, | |
* other methods will not be retryable. | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface FeignRetryable { | |
class AnnotationRetryer implements Retryer { | |
private final Retryer delegate; | |
public AnnotationRetryer(Retryer delegate) { | |
this.delegate = delegate; | |
} | |
@Override | |
public void continueOrPropagate(RetryableException e) { | |
boolean hasAnnotation = | |
e.request().requestTemplate().methodMetadata().method().getAnnotation(FeignRetryable.class) != null; | |
if (hasAnnotation) { | |
delegate.continueOrPropagate(e); | |
} else { | |
throw e; | |
} | |
} | |
@Override | |
public Retryer clone() { | |
return new AnnotationRetryer(delegate.clone()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
happy it worked with you