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()); | |
} | |
} | |
} |
Thanks for this solution
I'm using Feign Client with Java Annotations, like:
@FeignClient(value="myClient", url="URL", configuration="MyConfig.class")
. I've added the@Bean Retryer
toMyConfig
class and annotated the called method with@FeignRetryer
interface.When I debug this code,
e.request().requestTemplate().methodMetadata().method().getAnnotation(FeignRetryable.class)
doesn't bring the annotation that I've written to the client method.Is this related with Spring AOP? Any solutions that you can think of? EDIT: My bad, found out that a diffrent method was called. Spring AOP works normal.
happy it worked with you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this solution
I'm using Feign Client with Java Annotations, like:
@FeignClient(value="myClient", url="URL", configuration="MyConfig.class")
.I've added the
@Bean Retryer
toMyConfig
class and annotated the called method with@FeignRetryer
interface.When I debug this code,
e.request().requestTemplate().methodMetadata().method().getAnnotation(FeignRetryable.class)
doesn't bring the annotation that I've written to the client method.Is this related with Spring AOP? Any solutions that you can think of?
EDIT: My bad, found out that a diffrent method was called. Spring AOP works normal.