[英]In Spring Boot How do you configure some listeners to use automatic Acknowledgment and others to use manual acknowledgment
I have a simple Spring Boot app with multiple methods annotated with @KafkaListener.我有一个简单的 Spring Boot 应用程序,其中有多种方法,并用 @KafkaListener 注释。 I would like some of them to use automatic acknowledgment while others use manual acknowledgment.
我希望他们中的一些人使用自动确认,而其他人使用手动确认。 Is it possible to achieve this using Spring Boot configuration with out manually setting up container factories?
是否可以使用 Spring 引导配置来实现这一点,而无需手动设置容器工厂? According to the documentation Using a @KafkaListener requires code like
根据文档使用 @KafkaListener 需要类似的代码
@KafkaListener(id = "cat", topics = "myTopic",
containerFactory = "kafkaManualAckListenerContainerFactory")
public void listen(String data, Acknowledgment ack) {
...
ack.acknowledge();
}
Does this mean I need to create two container factory's, one for automatic acknowledgement and one for manual acknowledgement and configure all listeners to reference the correct factory?这是否意味着我需要创建两个容器工厂,一个用于自动确认,一个用于手动确认并配置所有侦听器以引用正确的工厂?
Does this mean I need to create two container factory's, one for automatic acknowledgement and one for manual acknowledgement and configure all listeners to reference the correct factory?
这是否意味着我需要创建两个容器工厂,一个用于自动确认,一个用于手动确认并配置所有侦听器以引用正确的工厂?
The assumption is correct.假设是正确的。 You just provide one
ListenerContainerFactory
bean with one set of ContainerProperties
and another one with different set.您只需为一个
ListenerContainerFactory
bean 提供一组ContainerProperties
和另一个具有不同集的 bean。 See this JavaDocs for more info in the AbstractKafkaListenerContainerFactory
:有关
AbstractKafkaListenerContainerFactory
的更多信息,请参阅此 JavaDocs:
/**
* Obtain the properties template for this factory - set properties as needed
* and they will be copied to a final properties instance for the endpoint.
* @return the properties.
*/
public ContainerProperties getContainerProperties()
You may consider to rely on the conventional bean name for that containerFactory
property for one of the ack mode and set of listeners:您可以考虑依赖该
containerFactory
属性的常规 bean 名称来获取其中一个 ack 模式和一组侦听器:
/**
* The bean name of the {@link org.springframework.kafka.config.KafkaListenerContainerFactory}
* to use to create the message listener container responsible to serve this endpoint.
* <p>
* If not specified, the default container factory is used, if any. If a SpEL
* expression is provided ({@code #{...}}), the expression can either evaluate to a
* container factory instance or a bean name.
* @return the container factory bean name.
*/
String containerFactory() default "";
Where default one is KafkaListenerAnnotationBeanPostProcessor.DEFAULT_KAFKA_LISTENER_CONTAINER_FACTORY_BEAN_NAME
- kafkaListenerContainerFactory
.默认值是
KafkaListenerAnnotationBeanPostProcessor.DEFAULT_KAFKA_LISTENER_CONTAINER_FACTORY_BEAN_NAME
- kafkaListenerContainerFactory
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.