Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有关循环消费的问题 #6

Open
caoironstick opened this issue Mar 23, 2022 · 0 comments
Open

有关循环消费的问题 #6

caoironstick opened this issue Mar 23, 2022 · 0 comments

Comments

@caoironstick
Copy link

您好,我最近一个参加比赛的项目中用到了springboot集成kafka。但在循环消费的部分,我的idea对如下语句报出了错误:for (ConsumerRecord<String, String> record : records) {,具体报错为:foreach 不适用于类型 'com.entor.service.ConsumerRecords<java.lang.String,java.lang.String>'。我在网上看到大家基本上都是这么写的,但不太明白为什么在我这里会报错。我曾经认为是Java版本的问题,但我从jdk从1.8换到15后也并没有解决问题。想请教一下这块儿为什么会报错。下面会附上部分源码,或许能方便您找到问题所在。
@Override public void run(){ //加载kafka消费者参数 Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "ytna"); props.put("enable.auto.commit", "true"); props.put("auto.commit.interval.ms", "1000"); props.put("session.timeout.ms", "15000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); //创建消费者对象 consumer = new KafkaConsumer<String,String>(props); consumer.subscribe(Arrays.asList(this.topic)); //死循环,持续消费kafka while (true){ try { //消费数据,并设置超时时间 ConsumerRecords<String, String> records = consumer.poll(100); //Consumer message for (ConsumerRecord<String, String> record : records) { //Send message to every client Iterable<? extends webSocket.WebSocket> wbSockets = null; for (com.entor.service.webSocket.WebSocket webSocket :wbSockets){ webSocket.sendMessage(record.value()); } } }catch (IOException e){ System.out.println(e.getMessage()); continue; } } }
感谢!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant