简体   繁体   中英

mulithreaded kafka consumer with nosuchelement exception

I altered my code from a single kafka consumer to have multiple consumers read from the same topic with the same group ID in order to effectively consume the high volume topic. But it seems to get an error after successfully starting and I think it has to do with a consumer waiting for a message and then failing. But the problem is that when one consumer shuts down, so do the others.

    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);
    }

here is the exception error.

    NoSuchElementException exception 

Really desperate here to get his to work so would appreciate any help here. Thanks in advance.

Replacing System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message()));

with System.out.println("Thread " + m_threadNumber + ": " + mesg);

Should help.

For printing log you are iterating again, causing exception as there may not be next element in iterator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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