title: Kafka Consumer - Poll behaviour
body: <p>I'm facing some serious problems trying to implement a solution for my needs, regarding KafkaConsumer (>=0.9).</p>
  
  <p>Let's imagine I have a function that has to read just <strong>n</strong> messages from a kafka topic.</p>
  
  <p>For example: <code>getMsgs(5)</code> --> <em>gets next 5 kafka messages in topic.</em></p>
  
  <p>So, I have a loop that looks like this. Edited with actual correct parameters. In this case, the consumer's max<code>.poll.records</code> param was set to 1, so the actual loop only iterated once. Different consumers(some of them iterated through many messages) shared an abstract father (this one), that's why it's coded that way. The <code>numMss</code> part was ad-hoc for this consumer.</p>
  
  <pre><code>for (boolean exit= false;!exit;)
  {
     Records = consumer.poll(config.pollTime);
     for (Record r:records) 
     {
         processRecord(r); //do my things
         numMss++;
         if (numMss==maximum) //maximum=5
         {   
            exit=true;
            break;
         }
     }
  }
  </code></pre>
  
  <p>Taking this into account, the problem is that the poll() method could get more than 5 messages. For example, if it gets 10 messages, my code will forget forever those other 5 messages, since Kafka will think they're already consumed.</p>
  
  <p>I tried commiting the offset but doesn't seem to work:</p>
  
  <pre><code>    consumer.commitSync(Collections.singletonMap(partition,
      new OffsetAndMetadata(record.offset() + 1)));
  </code></pre>
  
  <p>Even with the offset configuration, whenever I launch again the consumer, it <strong>won't start from the 6th message</strong> (<em>remember, I just wanted 5 messages</em>), but from the <strong>11th</strong> (since the first poll consumed 10 messages).</p>
  
  <p>Is there any solution for this, or maybe (most surely) am I missing something?</p>
  
  <p>Thanks in advance!!</p>