返回到文章

采纳

编辑于

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

kafka

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; //返回出去的数据