【问题标题】:mulithreaded kafka consumer with nosuchelement exception没有此类元素异常的多线程 kafka 消费者
【发布时间】:2017-01-12 13:59:32
【问题描述】:

我将我的代码从单个 kafka 消费者更改为让多个消费者从具有相同组 ID 的同一主题中读取,以便有效地使用大容量主题。但它似乎在成功启动后出现错误,我认为这与消费者等待消息然后失败有关。但问题是,当一个消费者关闭时,其他消费者也会关闭。

    ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
    try {

       while (it.hasNext())
      { 
        try{

             String mesg = new String(it.next().message());
             System.out.println( mesg);
             if (StringUtils.isEmpty(mesg)){
                 continue;
             }
             System.out.println("Thread " + m_threadNumber + ": " + 
                    new     String(it.next().message()));
             mesg = messageFormat.createMsg(mesg);
             System.out.println("MESSAGE TRANSMISSION SUCCESSFUL!");
           }        
             catch(Exception e)
             {
                e.printStackTrace();
                continue;
             }

          }
          }catch(Exception e)
          {
        e.printStackTrace();

     }

    System.out.println("Shutting down Thread: " + m_threadNumber);
    //System.out.println("Shutting down Thread: " + m_threadNumber);
    }

这里是异常错误。

    NoSuchElementException exception 

真的很想在这里让他工作,所以很感激这里的任何帮助。提前致谢。

【问题讨论】:

  • 发布异常堆栈跟踪可能会有所帮助。

标签: apache-kafka


【解决方案1】:

更换 System.out.println("线程" + m_threadNumber + ": " + 新字符串(it.next().message()));

与 System.out.println("线程" + m_threadNumber + ": " + 消息);

应该有帮助。

为了打印日志,您再次迭代,导致异常,因为迭代器中可能没有下一个元素。

【讨论】:

    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 2020-11-15
    • 2021-09-29
    • 2018-10-07
    相关资源
    最近更新 更多