WIP
[demos/kafka/seek] / src / main / java / de / juplo / kafka / seek / Consumer.java
index 0c232d6..7376945 100644 (file)
@@ -4,8 +4,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.apache.kafka.clients.consumer.ConsumerRecords;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.common.TopicPartition;
 import org.apache.kafka.common.errors.WakeupException;
-import org.apache.kafka.common.serialization.LongDeserializer;
 import org.apache.kafka.common.serialization.StringDeserializer;
 
 import javax.annotation.PreDestroy;
@@ -26,6 +26,7 @@ public class Consumer implements Runnable
   private final KafkaConsumer<Long, String> consumer;
 
   private boolean running = false;
+  Long offset = null;
   Future<?> future = null;
 
 
@@ -45,7 +46,8 @@ public class Consumer implements Runnable
     props.put("bootstrap.servers", bootstrapServer);
     props.put("group.id", groupId);
     props.put("client.id", clientId);
-    props.put("key.deserializer", LongDeserializer.class.getName());
+    props.put("commit.interval.ms", 500);
+    props.put("key.deserializer", StringDeserializer.class.getName());
     props.put("value.deserializer", StringDeserializer.class.getName());
 
     consumer = new KafkaConsumer<>(props);
@@ -65,6 +67,18 @@ public class Consumer implements Runnable
 
       while (running)
       {
+        if (offset != null)
+        {
+          log.info("{} - seeking to offset {}", id, offset);
+          consumer
+              .partitionsFor(topic)
+              .forEach(partition ->
+                  consumer.seek(
+                      new TopicPartition(topic, partition.partition()),
+                      offset));
+          offset = null;
+        }
+
         ConsumerRecords<Long, String> records = consumer.poll(Duration.ofSeconds(1));
         for (ConsumerRecord<Long, String> record : records)
           log.info(
@@ -81,14 +95,26 @@ public class Consumer implements Runnable
     {
       log.info("{} - RIIING!", id);
     }
+    catch(Exception e)
+    {
+      log.error("{} - Unexpected error: {}", id, e.toString());
+    }
     finally
     {
       log.info("{} - Unsubscribing...", id);
       consumer.unsubscribe();
       running = false;
+      offset = null;
     }
   }
 
+
+  public void seek(long offset)
+  {
+    this.offset = offset;
+  }
+
+
   public synchronized void start()
   {
     if (running)
@@ -109,6 +135,7 @@ public class Consumer implements Runnable
     future.get();
   }
 
+
   @PreDestroy
   public void destroy() throws ExecutionException, InterruptedException
   {