@KafkaListener 作為 Meta Annotation

從 2.2 版本開始,您現在可以使用 @KafkaListener 作為 meta annotation。以下範例示範如何操作

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@KafkaListener
public @interface MyThreeConsumersListener {

    @AliasFor(annotation = KafkaListener.class, attribute = "id")
    String id();

    @AliasFor(annotation = KafkaListener.class, attribute = "topics")
    String[] topics();

    @AliasFor(annotation = KafkaListener.class, attribute = "concurrency")
    String concurrency() default "3";

}

您必須至少別名 topics、topicPattern 或 topicPartitions 其中之一 (而且,通常是 id 或 groupId,除非您在 consumer factory 組態中指定了 group.id)。以下範例示範如何操作

@MyThreeConsumersListener(id = "my.group", topics = "my.topic")
public void listen1(String in) {
    ...
}