I've been using '@KafkaListener' at method level to create consumers. Now, I'm trying to create my own custom annotation by extending '@KafkaListener' and limit the no of attributes (for example, because of some reasons, I don't want to expose attributes like 'errorHandler' 'containerGroup' etc ). Now my question is, to implement this, is there any option to extend the existing '@KafkaListener' ? Please suggest.
Asked
Active
Viewed 667 times
1 Answers
3
Yes, it's quite easy.
@SpringBootApplication
public class So61684460Application {
public static void main(String[] args) {
SpringApplication.run(So61684460Application.class, args);
}
@MyListener(topics = "so61684460", id = "so61684460")
public void listen(String in) {
System.out.println(in);
}
@Bean
public NewTopic topic() {
return TopicBuilder.name("so61684460").partitions(3).replicas(1).build();
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@KafkaListener(concurrency = "${my.concurrency}")
@interface MyListener {
@AliasFor(annotation = KafkaListener.class, attribute = "id")
String id();
@AliasFor(annotation = KafkaListener.class, attribute = "topics")
String[] topics() default "";
}
As you can see, as well as restricting the visibility of some attributes, you can make them required (id
above), change the default value, or set hard-coded or parameterized values in the invisible attributes (concurrency
above).
This is described in the documentation.

Gary Russell
- 166,535
- 14
- 146
- 179
-
awesome, this is what we need. Thanks Gary – Raj May 08 '20 at 20:36