写一个kafka的工具类,返回所需要的信息,这样return会对kafka有影响吗?

..... 发表于: 2019-08-20   最后更新时间: 2019-08-20 19:30:29   1,614 游览
List<String> buffer = new ArrayList<>();
        Properties consumerProp = getConsumerProp();
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerProp);
        try{
            consumer.subscribe(topicList);//topicList这个是传输过来的
            while(true){
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
                if(records.isEmpty()){
                    break;
                }
                for (ConsumerRecord<String, String> record : records){
                   buffer.add(record.value());
                }
                consumer.commitAsync();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                consumer.commitSync();
            }finally {
                consumer.close();
            }
        }
        return buffer; //返回出去的数据
发表于 2019-08-20
添加评论

你这个程序问题一大堆。。你仔细读一读消费者类那篇文章。
https://www.orchome.com/451

你的答案

查看kafka相关的其他问题或提一个您自己的问题