Die Implementierung speichert Zustand & Offsets vor _jedem_ `poll()`
[demos/kafka/training] / src / main / java / de / juplo / kafka / EndlessConsumer.java
index 58557f2..92802b9 100644 (file)
@@ -25,8 +25,8 @@ public class EndlessConsumer<K, V> implements Runnable
   private final String id;
   private final String topic;
   private final Consumer<K, V> consumer;
-  private final PollIntervalAwareConsumerRebalanceListener pollIntervalAwareRebalanceListener;
-  private final RecordHandler<K, V> handler;
+  private final RebalanceListener rebalanceListener;
+  private final RecordHandler<K, V> recordHandler;
 
   private final Lock lock = new ReentrantLock();
   private final Condition condition = lock.newCondition();
@@ -42,7 +42,7 @@ public class EndlessConsumer<K, V> implements Runnable
     try
     {
       log.info("{} - Subscribing to topic {}", id, topic);
-      consumer.subscribe(Arrays.asList(topic), pollIntervalAwareRebalanceListener);
+      consumer.subscribe(Arrays.asList(topic), rebalanceListener);
 
       while (true)
       {
@@ -63,17 +63,18 @@ public class EndlessConsumer<K, V> implements Runnable
               record.value()
           );
 
-          handler.accept(record);
+          recordHandler.accept(record);
 
           consumed++;
         }
 
-        pollIntervalAwareRebalanceListener.beforeNextPoll();
+        rebalanceListener.beforeNextPoll();
       }
     }
     catch(WakeupException e)
     {
       log.info("{} - RIIING! Request to stop consumption - commiting current offsets!", id);
+      consumer.commitSync();
       shutdown();
     }
     catch(RecordDeserializationException e)
@@ -87,6 +88,7 @@ public class EndlessConsumer<K, V> implements Runnable
           offset,
           e.getCause().toString());
 
+      consumer.commitSync();
       shutdown(e);
     }
     catch(Exception e)