为什么我的卡夫卡消费者有时候不会收听消息?

时间:2016-04-12 03:58:26

标签: apache-kafka kafka-consumer-api

我使用老卡夫卡消费者0.8V。 的步骤

  • 启动侦听器
  • 发送10条消息.Listener收听4条消息。
  • 发送单个邮件。听众不听?
  • 再次发布单个邮件。听众没有听。

任何人都可以解释这种行为吗?

消费

public class KafkaMessageListenerThread implements Runnable {

    private KafkaStream<byte[], byte[]> stream;

    private final KafkaMessageListener baseConsumer;

    public KafkaMessageListenerThread(KafkaMessageListener consumer, KafkaStream<byte[], byte[]> stream) {
        this.baseConsumer = consumer;
        this.stream = stream;
    }

    public void run() {

        ConsumerIterator<byte[], byte[]> itr = stream.iterator();

        System.out.println("listens....");
        while (itr.hasNext()) {
            System.out.println("kafka record : " + itr.next());
            byte[] data = itr.next().message();
            baseConsumer.receiveData(data);

        }
    }

}

BaseConsumer

public void start()  {
        try {

            Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
            topicCountMap.put(topic, CoreConstants.THREAD_SIZE);
            Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector
                    .createMessageStreams(topicCountMap);

            List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
            executor = Executors.newFixedThreadPool(CoreConstants.THREAD_SIZE);

            for (final KafkaStream stream : streams) {
                executor.submit(new KafkaMessageListenerThread(this, stream));
            }

        } catch (Exception e) {

            System.out.println("eror in polling");
        }
    }

消费者资产

key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
value.deserializer=com.xx.RawFileSerializer
zookeeper.connect=zookeeper.xx\:2181
serializer.class=com.xx.RawFileEncoderDecoder
bootstrap.servers=kafka.xx\:9092
group.id=test
consumer.timeout.ms=-1
fetch.min.bytes=1

0 个答案:

没有答案