码迷,mamicode.com
首页 > 其他好文 > 详细

消费消息+手动提交+同步异步

时间:2020-07-21 01:09:32      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:trap   pen   close   client   conf   imp   同步异步   port   system   

依赖

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
        </dependency>

代码

package com.perfect.kafka;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

public class KafkaComsumerTest {

    @Test
    public void cunsumertest(){
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");

        //关闭自动提交
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,false);
        //props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,1000);

        //latest,earliest
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");

        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");


        props.put(ConsumerConfig.GROUP_ID_CONFIG,"bigdata1");

        KafkaConsumer<String,String> c = new KafkaConsumer<String, String>(props);

        c.subscribe(Collections.singletonList("test2"));

        while(true){

            for(int i=0;i<10000;i++){
                ConsumerRecords records = c.poll(100);

                records.forEach(System.out::println);
            }
            //同步提交
            c.commitSync();
            //异步提交
            //c.commitAsync();

        }

       // c.close();
    }
}

 

消费消息+手动提交+同步异步

标签:trap   pen   close   client   conf   imp   同步异步   port   system   

原文地址:https://www.cnblogs.com/abuduri/p/13347849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!