@KafkaListener 作为元注解
从 2.2 版本开始,您现在可以将 @KafkaListener 用作元注解。以下示例展示了如何实现这一点
@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,除非您已在消费者工厂配置中指定了 group.id)。以下示例展示了如何实现这一点
@MyThreeConsumersListener(id = "my.group", topics = "my.topic")
public void listen1(String in) {
...
}