是的,Spring Boot Kafka 支持手动提交消息。在 Spring Boot 中,你可以通过设置 enable.auto.commit
属性为 false
来禁用自动提交,然后使用 KafkaTemplate
或 Consumer
的 commitSync()
或 commitAsync()
方法来手动提交消息。
以下是一个使用 KafkaTemplate
手动提交消息的示例:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; @Service public class KafkaProducer { @Autowired private KafkaTemplatekafkaTemplate; public void sendMessage(String topic, String message) { kafkaTemplate.send(topic, message); // 手动提交消息 kafkaTemplate.flush(); } }
在这个示例中,我们首先使用 kafkaTemplate.send()
方法发送消息,然后调用 kafkaTemplate.flush()
方法手动提交消息。请注意,flush()
方法会等待所有消息都被发送并确认后才返回。