WIP
[demos/kafka/seek] / src / main / java / de / juplo / kafka / seek / Consumer.java
1 package de.juplo.kafka.seek;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.apache.kafka.clients.consumer.ConsumerRecords;
6 import org.apache.kafka.clients.consumer.KafkaConsumer;
7 import org.apache.kafka.common.TopicPartition;
8 import org.apache.kafka.common.errors.WakeupException;
9 import org.apache.kafka.common.serialization.StringDeserializer;
10
11 import javax.annotation.PreDestroy;
12 import java.time.Duration;
13 import java.util.Arrays;
14 import java.util.Properties;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Future;
18
19
20 @Slf4j
21 public class Consumer implements Runnable
22 {
23   private final ExecutorService executor;
24   private final String id;
25   private final String topic;
26   private final KafkaConsumer<Long, String> consumer;
27
28   private boolean running = false;
29   Long offset = null;
30   Future<?> future = null;
31
32
33   public Consumer(
34       ExecutorService executor,
35       String bootstrapServer,
36       String groupId,
37       String clientId,
38       String topic)
39   {
40     this.executor = executor;
41
42     this.id = clientId;
43     this.topic = topic;
44
45     Properties props = new Properties();
46     props.put("bootstrap.servers", bootstrapServer);
47     props.put("group.id", groupId);
48     props.put("client.id", clientId);
49     props.put("commit.interval.ms", 500);
50     props.put("key.deserializer", StringDeserializer.class.getName());
51     props.put("value.deserializer", StringDeserializer.class.getName());
52
53     consumer = new KafkaConsumer<>(props);
54   }
55
56
57   @Override
58   public void run()
59   {
60     try
61     {
62       log.info("{} - Subscribing to topic test", id);
63       consumer.subscribe(Arrays.asList(topic));
64
65       while (running)
66       {
67         if (offset != null)
68         {
69           log.info("{} - seeking to offset {}", id, offset);
70           consumer
71               .partitionsFor(topic)
72               .forEach(partition ->
73                   consumer.seek(
74                       new TopicPartition(topic, partition.partition()),
75                       offset));
76           offset = null;
77         }
78
79         ConsumerRecords<Long, String> records = consumer.poll(Duration.ofSeconds(1));
80         for (ConsumerRecord<Long, String> record : records)
81           log.info(
82               "{} - {}: {}/{} - {}",
83               id,
84               record.offset(),
85               record.topic(),
86               record.partition(),
87               record.value()
88           );
89       }
90     }
91     catch(WakeupException e)
92     {
93       log.info("{} - RIIING!", id);
94     }
95     catch(Exception e)
96     {
97       log.error("{} - Unexpected error: {}", id, e.toString());
98     }
99     finally
100     {
101       log.info("{} - Unsubscribing...", id);
102       running = false;
103       consumer.unsubscribe();
104       offset = null;
105     }
106   }
107
108
109   public void seek(long offset)
110   {
111     this.offset = offset;
112   }
113
114
115   public synchronized void start()
116   {
117     if (running)
118       throw new RuntimeException("Consumier instance " + id + " is already running!");
119
120     log.info("Running {}", id);
121     running = true;
122     future = executor.submit(this);
123   }
124
125   public synchronized void stop() throws ExecutionException, InterruptedException
126   {
127     if (!running)
128       throw new RuntimeException("Consumier instance " + id + " is not running!");
129
130     log.info("Stopping {}", id);
131     running = false;
132     consumer.wakeup();
133     future.get();
134   }
135
136   @PreDestroy
137   public void destroy() throws ExecutionException, InterruptedException
138   {
139     stop();
140     log.info("{} - Closing the KafkaConsumer", id);
141     consumer.close(Duration.ofSeconds(3));
142   }
143 }