Acknowledgement.acknowledge()在spring-kafka @KafkaListener中抛出异常

Ara*_* Kv 4 apache-kafka spring-boot spring-kafka

当我将enable.auto.commit设置为false并尝试使用基于注释的spring-kafka @KafkaListener手动提交偏移量时,我得到一个org.springframework.kafka.listener.ListenerExecutionFailedException:无法使用传入消息调用侦听器方法

我有一个非常简单的代码如下:

@KafkaListener(id = "someid", topics = "${demo.topic}", containerFactory = "someContainerFactory")
public void listenFooGroup(String message, Acknowledgement ack) {
    System.out.println("Received Messasge in group 'foo': " + message);

    // TODO: Do something with the message
}
Run Code Online (Sandbox Code Playgroud)

当我从制作人发送消息时,我得到以下异常:

org.springframework.kafka.listener.ListenerExecutionFailedException:无法使用传入消息调用侦听器方法.

端点处理程序详细信息

方法[public void com.****.*****.*******.KafkaMessageListener.listenFooGroup(java.lang.String,org.springframework.kafka.support.Acknowledgment)]

Bean [com.****.*****.*******.KafkaMessageListener@5856dbe4]; 嵌套异常是org.springframework.messaging.converter.MessageConversionException:无法处理消息; 嵌套异常是org.springframework.messaging.converter.MessageConversionException:无法从[java.lang.String]转换为[org.springframework.kafka.support.Acknowledgment]以获取GenericMessage [payload = test,headers = {kafka_offset = 57,kafka_receivedMessageKey = null,kafka_receivedPartitionId = 0,kafka_receivedTopic = demotopic}],failedMessage = GenericMessage [payload = test,headers = {kafka_offset = 57,kafka_receivedMessageKey = null,kafka_receivedPartitionId = 0,kafka_receivedTopic = demotopic}]

请帮忙.TIA.

Gar*_*ell 10

您必须将容器工厂的containerPropertiesackMode设置为MANUALMANUAL_IMMEDIATE获取Acknowledgment对象.

对于其他ack模式,容器负责提交偏移量.

factory.getContainerProperties().setAckMode(AckMode.MANUAL_IMMEDIATE)
Run Code Online (Sandbox Code Playgroud)

或者....ackMode如果使用Spring Boot,则设置属性

  • 下一个版本将抛出[更有意义的例外](https://github.com/spring-projects/spring-kafka/pull/356).`new IllegalStateException("No Acknowledgement可用作参数,侦听器容器必须有一个MANUAL Ackmode来填充确认.",`.感谢你指出这一点. (2认同)
  • 不要在对非常旧的答案的评论中提出新问题。`spring.kafka.listener.ack-mode` - https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/html/appendix-application-properties.html#common-application-特性 (2认同)