【问题标题】:After finishing the iteration, call is not coming out of the while loop完成迭代后,调用不会从 while 循环中出来
【发布时间】:2016-03-11 13:39:15
【问题描述】:

我有这个 Apache kafka 消费者的示例代码

public class WorkerThread implements Callable<Object> {
    ConsumerConnector consumerConnector;
    private static ArrayList<Object> list = new ArrayList<>();

    @Override
    public Object call() throws Exception {
        processMsg();
        return list;
    }

    private void processMsg() {
        StringMessage msg = new StringMessage();
        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
        topicCountMap.put(KafkaConstants.LOB_TOPIC, new Integer(1));
        Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector
                .createMessageStreams(topicCountMap);
        KafkaStream<byte[], byte[]> stream = consumerMap.get(KafkaConstants.LOB_TOPIC).get(0);
        ConsumerIterator<byte[], byte[]> itr = stream.iterator();

            while (itr.hasNext()) {
                msg.setMessage(new String((itr.next().message())));
                list.add(msg);
                System.out.println("&&&" + msg);
            }

        System.out.println("++++++++++++");
    }
}

在上面的示例中,我的"++++++" sysout 无法正常工作。任何人都知道原因..?

【问题讨论】:

  • 有&&&打印吗?有什么错误吗?
  • +++ 不起作用,但是会发生什么&amp;&amp;&amp; 循环输出是无限输出还是根本不处理还是...?
  • @JoachimIsaksson 如果有一条消息 &&& 打印一条消息然后它会停止..没有例外

标签: java apache-kafka


【解决方案1】:

【讨论】:

  • 你能解释一下吗?我是 Kafka 的新手。我正在尝试将对象数组从 java 线程返回到我的消息侦听器,这将使用 executor 生成新线程(上面的示例代码)
  • 可以在属性consumer.timeout.ms中更改阻塞的超时时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
相关资源
最近更新 更多