KafkaStream没有收到来自Topic的任何消息

时间:2016-11-17 13:58:16

标签: kafka-consumer-api apache-kafka-connect

我正在尝试使用KafkaStreamsKafkaConnect来尝试使用主题中的消息。我为这个主题设置了一个“标准”批量消费者,它就像一个魅力。我首先向卡夫卡发送了几张唱片然后消费。现在我想使用Kakfa流做同样的事情,但我没有收到该主题的单个消息。这是我正在使用的消费者代码。

final int NUMBER_OF_PARTITIONS = 4;
final Properties consumerConfig = new Properties();
consumerConfig.setProperty("zookeeper.connect", RULE.getConfiguration().kafka.getZookeeperUrl());
consumerConfig.setProperty("backoff.increment.ms", "100");
consumerConfig.setProperty("group.id", "java-consumer-example");
consumerConfig.setProperty("consumer.timeout.ms", "1000000");
consumerConfig.setProperty("client.id", "someclient");
consumerConfig.setProperty("auto.offset.reset", "smallest");
consumerConfig.setProperty("enable.auto.commit", "false");
consumerConfig.setProperty("bootstrap.servers", RULE.getConfiguration().kafka.getHosts());

final ConsumerConnector connector = Consumer.createJavaConsumerConnector(new ConsumerConfig(consumerConfig));
final TopicFilter sourceTopicFilter = new Whitelist(RULE.getConfiguration().kafka.getTopic());

final VerifiableProperties decoderProps = new VerifiableProperties();
decoderProps.props().setProperty("schema.registry.url", RULE.getConfiguration().kafka.getRegistry());
decoderProps.props().setProperty("max.schemas.per.subject", "1");
final List<KafkaStream<String, Object>> streams = connector
    .createMessageStreamsByFilter(sourceTopicFilter, NUMBER_OF_PARTITIONS, new StringDecoder(decoderProps), new KafkaAvroDecoder(decoderProps));

final ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_PARTITIONS);
for (final KafkaStream stream : streams) {
    executorService.submit(() -> {
        try {
            final ConsumerIterator it = stream.iterator();
            while (it.hasNext()) {
                final MessageAndMetadata messageAndMetadata = it.next();
                final String key = (String) messageAndMetadata.key();
                System.out.println("KEY" + key);
            }
        } catch (final Exception ex) {
            LOGGER.error("ERROR", ex);
        }
    });
}

我的问题是,我的代码在it.hasNext()条件下一直等待,直到达到超时。我可能在这里遗漏了一些细节,但无法弄清楚,为什么我没有从主题中得到任何东西。作为此测试的一部分,我有一个生产者,它在消费者启动之前就会向该主题发送一些记录,因此它不能成为偏移问题。任何想法都会受到欢迎。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。错误超出了我发布的代码。我为ExecutorService的关闭提供的超时时间太短,因此只是在没有足够的时间来完成消费者工作的情况下将其终止。

相关问题