Die Implementierung speichert Zustand & Offsets vor _jedem_ `poll()`
[demos/kafka/training] / src / main / java / de / juplo / kafka / EndlessConsumer.java
index 3ff479c..92802b9 100644 (file)
@@ -42,7 +42,6 @@ public class EndlessConsumer<K, V> implements Runnable
     try
     {
       log.info("{} - Subscribing to topic {}", id, topic);
-      rebalanceListener.enableCommits();
       consumer.subscribe(Arrays.asList(topic), rebalanceListener);
 
       while (true)
@@ -68,11 +67,14 @@ public class EndlessConsumer<K, V> implements Runnable
 
           consumed++;
         }
+
+        rebalanceListener.beforeNextPoll();
       }
     }
     catch(WakeupException e)
     {
-      log.info("{} - RIIING! Request to stop consumption.", id);
+      log.info("{} - RIIING! Request to stop consumption - commiting current offsets!", id);
+      consumer.commitSync();
       shutdown();
     }
     catch(RecordDeserializationException e)
@@ -86,12 +88,12 @@ public class EndlessConsumer<K, V> implements Runnable
           offset,
           e.getCause().toString());
 
+      consumer.commitSync();
       shutdown(e);
     }
     catch(Exception e)
     {
-      log.error("{} - Unexpected error: {}, disabling commits", id, e.toString(), e);
-      rebalanceListener.disableCommits();
+      log.error("{} - Unexpected error: {}", id, e.toString(), e);
       shutdown(e);
     }
     finally