配置kafka+zk在虚拟机上,自己在本机上的生产者可以正常发送消息,但是消费者在
ConsumerIterator iterator = stream.iterator();
之后 iterator 的任何操作都直接卡死了,请问这个是怎么回事?在这个操作之前debug是可以看到变量内部的数值的,这个操作之后就不能看了,value全部清空了。
贴源码
package com.weixinjia.recreation.queue.client;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.message.MessageAndMetadata;
public class ConsumerGroupExample extends Thread{
private final ConsumerConnector consumer;
private final String topic;
private ExecutorService executor;
public ConsumerGroupExample(String a_zookeeper, String a_groupId, String a_topic) {
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
createConsumerConfig(a_zookeeper, a_groupId));
this.topic = a_topic;
}
public void shutdown() {
if (consumer != null) consumer.shutdown();
if (executor != null) executor.shutdown();
try {
if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) {
System.out.println("Timed out waiting for consumer threads to shut down, exiting uncleanly");
}
} catch (InterruptedException e) {
System.out.println("Interrupted during shutdown, exiting uncleanly");
}
}
public void run(int a_numThreads) {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(a_numThreads));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
// now launch all the threads
//
// executor = Executors.newFixedThreadPool(a_numThreads);
System.out.println(streams.size());
// now create an object to consume the messages
//
int threadNumber = 0;
for (final KafkaStream<byte[], byte[]> stream : streams) {
executor.submit(new ConsumerTest(stream, threadNumber));
threadNumber++;
ConsumerIterator<byte[], byte[]> iterator = stream.iterator();
System.out.println(iterator);
while(iterator.hasNext()){
MessageAndMetadata<byte[], byte[]> next = iterator.next();
byte[] message = next.message();
String string = new String(message);
System.out.println(string);
}
}
System.out.println("消息输出完成");
}
private static ConsumerConfig createConsumerConfig(String a_zookeeper, String a_groupId) {
Properties props = new Properties();
props.put("zookeeper.connect", a_zookeeper);
props.put("group.id", a_groupId);
props.put("zookeeper.session.timeout.ms", "1500");
props.put("zookeeper.sync.time.ms", "4000");
props.put("auto.commit.interval.ms", "1000");
props.put("fetch.message.max.bytes", "10240000");
props.put("auto.commit.enable", "true");
return new ConsumerConfig(props);
}
public static void main(String[] args) {
String zooKeeper = "masterServce:2181";
String groupId = "group-1";
String topic = "test2";
int threads = 3;
ConsumerGroupExample example = new ConsumerGroupExample(zooKeeper, groupId, topic);
example.run(threads);
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {
}
example.shutdown();
}
}
消息在kafka那边直接用kafka-console-consumer.sh是可以查询到的