huangfox

冰冻三尺,非一日之寒!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1、代码实现

kafkaListener

需要指定id,例如这里是:full-part-id。

@KafkaListener(topics = "part-full-topic", id = "full-part-id", containerGroup = "full-part-group")
public void listenFullPart(ConsumerRecord<String, String> record) {
    Optional<String> recordOptional = Optional.fromNullable(record.value());
    if (recordOptional.isPresent()) {
        List<PartStockInfoVo> partStockInfoVos = JSONObject.parseArray(recordOptional.get(), PartStockInfoVo.class);
        esPartInfoClient.updateFullIndex(partStockInfoVos);
    }
}

消费开关

@RestController
public class KafkaManageController {

    @Autowired
    private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;

    @RequestMapping("/stop")
    public void stop() {
        MessageListenerContainer listenerContainer = kafkaListenerEndpointRegistry.getListenerContainer("full-part-id");
        listenerContainer.stop();
    }

    @RequestMapping("/start")
    public void start() {
        MessageListenerContainer listenerContainer = kafkaListenerEndpointRegistry.getListenerContainer("full-part-id");
        listenerContainer.start();
    }
}

 

 

参考:

1、How can i stop consumers from consuming?

 

posted on 2018-10-16 15:48  huangfox  阅读(6193)  评论(2编辑  收藏  举报