消费消息+手动提交+同步异步
Posted abuduri
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了消费消息+手动提交+同步异步相关的知识,希望对你有一定的参考价值。
依赖
<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(); } }
以上是关于消费消息+手动提交+同步异步的主要内容,如果未能解决你的问题,请参考以下文章