You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@RetryableTopic is pretty handfull to configure retry strategies on @KafkaListener annotated methods, but it lacks a very useful capability, the meta-annotation.
Just as we can create meta-annotations to make it easier to configure multiple @KafkaListeners that share the same properties, reducing boilerplate (and also improving readability), it would be great to be able to do the same thing with @RetryableTopics.
This issue proposes to add this feature with a simple changes to @RetryableTopic annotation target.
Context
To justify this change, let's suppose that we are implementing some retry strategies for multiple topics with different backoffs. It's reasonable to assume that to reduce boilerplate I want to create generic retry strategies that can be shared by multiple listeners, but the only way to do this currently is to create a Bean like:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@KafkaListener(...)
public @interface MyTopicListener {}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@KafkaListener(...)
public @interface MyOtherTopicListener {}
And finally:
classMyConsumerClass {
@MyTopicListener@WithFixedBackoffRetryvoidconsume(Messagemessage) throwsMyException {
/** consume the message... */
}
@MyOtherTopicListener@WithExponentialBackoffRetryvoidconsume(OtherMessageotherMessage) throwMyOtherException {
/** consume the other message... */
}
}
It is more transparent for the developer to use Kafka and not have to mix kafka related configuration and business logic on the same piece of code.
Implementation Proposal
The proposal is to change the @Target meta-annotation of the @RetryableTopic annotation, adding the ElementType.ANNOTATION_TYPE. This will enable the call of RetryTopicConfigurationProvider.findRetryConfigurationFor() to find the meta-annotation on Method object using the AnnotationUtils.findAnnotation(). The AnnotationUtils.findAnnotation() already searches the full type hierarchy using the MergedAnnotators API.
There is no significant change on codebase, just the addition of a target to an annotation. The new test cases where added to RetryTopicConfigurationProviderTests on a forked repository and everything seems to work fine.
Future Improvements
Another good improvement would be use MergedAnnotations directly on RetryTopicConfigurationProvider.findRetryConfigurationFor() to get a synthetic Annotation that is merged with attributes annotated with @AliasFor. It could be useful to add some level of parametrization. For example, let the business area developer to choose what exceptions will trigger the retry strategy directly on the "high-level" annotation:
Expected Behavior
@RetryableTopic
is pretty handfull to configure retry strategies on@KafkaListener
annotated methods, but it lacks a very useful capability, the meta-annotation.Just as we can create meta-annotations to make it easier to configure multiple
@KafkaListeners
that share the same properties, reducing boilerplate (and also improving readability), it would be great to be able to do the same thing with@RetryableTopics
.This issue proposes to add this feature with a simple changes to
@RetryableTopic
annotation target.Context
To justify this change, let's suppose that we are implementing some retry strategies for multiple topics with different backoffs. It's reasonable to assume that to reduce boilerplate I want to create generic retry strategies that can be shared by multiple listeners, but the only way to do this currently is to create a Bean like:
With the use of meta-annotations we could do something like:
And for topic listener meta-annotations:
And finally:
It is more transparent for the developer to use Kafka and not have to mix kafka related configuration and business logic on the same piece of code.
Implementation Proposal
The proposal is to change the
@Target
meta-annotation of the@RetryableTopic
annotation, adding theElementType.ANNOTATION_TYPE
. This will enable the call ofRetryTopicConfigurationProvider.findRetryConfigurationFor()
to find the meta-annotation onMethod
object using theAnnotationUtils.findAnnotation()
. TheAnnotationUtils.findAnnotation()
already searches the full type hierarchy using theMergedAnnotators
API.We doesn't have to change current
findRetryConfigurationFor()
implementation:Impact
There is no significant change on codebase, just the addition of a target to an annotation. The new test cases where added to
RetryTopicConfigurationProviderTests
on a forked repository and everything seems to work fine.Future Improvements
Another good improvement would be use
MergedAnnotations
directly onRetryTopicConfigurationProvider.findRetryConfigurationFor()
to get a syntheticAnnotation
that is merged with attributes annotated with@AliasFor
. It could be useful to add some level of parametrization. For example, let the business area developer to choose what exceptions will trigger the retry strategy directly on the "high-level" annotation:Then we could do:
The text was updated successfully, but these errors were encountered: