|
这个版本仍在开发中,目前尚未被认为是稳定的。要使用最新稳定版本,请使用 Spring for Apache Kafka 4.0.4! |
@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";
}
You must 为至少一个 topics、topicPattern 或 topicPartitions 别名(通常还需要为 id 或 groupId 别名,除非在消费者工厂配置中指定了 group.id)进行别名设置。
以下示例展示了如何实现:
@MyThreeConsumersListener(id = "my.group", topics = "my.topic")
public void listen1(String in) {
...
}