1. 程式人生 >實用技巧 >其它 >springboot手動啟停kafka監聽

springboot手動啟停kafka監聽


在springboot專案中消費kafka訊息使用@KafkaListener監聽,參考程式碼如下:

@KafkaListener(id = "testListenerId" ,topics ={ "testTopicCode" }, groupId = "testGroupId")

本次主要是介紹怎麼手動啟停kafka監聽:

1、首先是要為監聽器設定ID,在@KafkaListener設定監聽器的ID為:testListenerId

2、在類中注入Kafka的監聽註冊器

@Autowired
private KafkaListenerEndpointRegistry kafkaRegistry;

3、暫停監聽

kafkaRegistry.getListenerContainer("testListenerId").pause();

4、恢復監聽

kafkaRegistry.getListenerContainer("testListenerId").resume();