繁体   English   中英

具有nosuchelement异常的多线程kafka使用者

[英]mulithreaded kafka consumer with nosuchelement exception

我更改了单个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 

非常渴望在这里工作,所以很感谢在这里的任何帮助。 提前致谢。

替换System.out.println(“ Thread” + m_threadNumber +“:” + new String(it.next()。message()));

使用System.out.println(“ Thread” + m_threadNumber +“:” + mesg);

应该有帮助。

对于打印日志,您要再次进行迭代,这会导致异常,因为迭代器中可能没有下一个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM