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     log.info("{} - Subscribing to topic test", id);
61     consumer.subscribe(Arrays.asList(topic));
62
63     try
64     {
65
66       running = true;
67
68       while (running)
69       {
70         if (offset != null)
71         {
72           log.info("{} - seeking to offset {}", id, offset);
73           consumer
74               .partitionsFor(topic)
75               .forEach(partition ->
76                   consumer.seek(
77                       new TopicPartition(topic, partition.partition()),
78                       offset));
79           offset = null;
80         }
81
82         ConsumerRecords<Long, String> records = consumer.poll(Duration.ofSeconds(1));
83         for (ConsumerRecord<Long, String> record : records)
84           log.info(
85               "{} - {}: {}/{} - {}",
86               id,
87               record.offset(),
88               record.topic(),
89               record.partition(),
90               record.value()
91           );
92       }
93     }
94     catch(WakeupException e)
95     {
96       log.info("{} - RIIING!", id);
97     }
98     catch(Exception e)
99     {
100       log.error("{} - Unexpected error: {}", id, e.toString());
101     }
102     finally
103     {
104       log.info("{} - Unsubscribing...", id);
105       consumer.unsubscribe();
106       running = false;
107       offset = null;
108     }
109   }
110
111
112   public void seek(long offset)
113   {
114     this.offset = offset;
115   }
116
117
118   public synchronized void start()
119   {
120     if (running)
121       throw new RuntimeException("Consumier instance " + id + " is already running!");
122
123     log.info("Running {}", id);
124     future = executor.submit(this);
125   }
126
127   public synchronized void stop() throws ExecutionException, InterruptedException
128   {
129     if (!running)
130       throw new RuntimeException("Consumier instance " + id + " is not running!");
131
132     log.info("Stopping {}", id);
133     running = false;
134     consumer.wakeup();
135     future.get();
136   }
137
138
139   @PreDestroy
140   public void destroy() throws ExecutionException, InterruptedException
141   {
142     stop();
143     log.info("{} - Closing the KafkaConsumer", id);
144     consumer.close(Duration.ofSeconds(3));
145   }
146 }