温馨提示×

springboot kafka能进行消息手动提交吗

小樊
108
2024-12-18 23:05:29
栏目: 大数据
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

是的,Spring Boot Kafka 支持手动提交消息。在 Spring Boot 中,你可以通过设置 enable.auto.commit 属性为 false 来禁用自动提交,然后使用 KafkaTemplateConsumercommitSync()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 KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String topic, String message) {
        kafkaTemplate.send(topic, message);
        // 手动提交消息
        kafkaTemplate.flush();
    }
}

在这个示例中,我们首先使用 kafkaTemplate.send() 方法发送消息,然后调用 kafkaTemplate.flush() 方法手动提交消息。请注意,flush() 方法会等待所有消息都被发送并确认后才返回。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:springboot kafka能进行消息回溯吗

0