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   Future<?> future = null;
30
31
32   public Consumer(
33       ExecutorService executor,
34       String bootstrapServer,
35       String groupId,
36       String clientId,
37       String topic)
38   {
39     this.executor = executor;
40
41     this.id = clientId;
42     this.topic = topic;
43
44     Properties props = new Properties();
45     props.put("bootstrap.servers", bootstrapServer);
46     props.put("group.id", groupId);
47     props.put("client.id", clientId);
48     props.put("key.deserializer", StringDeserializer.class.getName());
49     props.put("value.deserializer", StringDeserializer.class.getName());
50
51     consumer = new KafkaConsumer<>(props);
52   }
53
54
55   @Override
56   public void run()
57   {
58     log.info("{} - Subscribing to topic test", id);
59     consumer.subscribe(Arrays.asList(topic));
60
61     try
62     {
63
64       running = true;
65
66       while (running)
67       {
68         ConsumerRecords<Long, String> records = consumer.poll(Duration.ofSeconds(1));
69         for (ConsumerRecord<Long, String> record : records)
70           log.info(
71               "{} - {}: {}/{} - {}",
72               id,
73               record.offset(),
74               record.topic(),
75               record.partition(),
76               record.value()
77           );
78       }
79     }
80     catch(WakeupException e)
81     {
82       log.info("{} - RIIING!", id);
83     }
84     catch(Exception e)
85     {
86       log.error("{} - Unexpected error: {}", id, e.toString());
87     }
88     finally
89     {
90       log.info("{} - Unsubscribing...", id);
91       consumer.unsubscribe();
92       running = false;
93     }
94   }
95
96
97   public void seek(long offset)
98   {
99     consumer
100         .partitionsFor(topic)
101         .forEach(partition ->
102             consumer.seek(
103                 new TopicPartition(topic, partition.partition()),
104                 offset));
105   }
106
107
108   public synchronized void start()
109   {
110     if (running)
111       throw new RuntimeException("Consumier instance " + id + " is already running!");
112
113     log.info("Running {}", id);
114     future = executor.submit(this);
115   }
116
117   public synchronized void stop() throws ExecutionException, InterruptedException
118   {
119     if (!running)
120       throw new RuntimeException("Consumier instance " + id + " is not running!");
121
122     log.info("Stopping {}", id);
123     running = false;
124     consumer.wakeup();
125     future.get();
126   }
127
128
129   @PreDestroy
130   public void destroy() throws ExecutionException, InterruptedException
131   {
132     stop();
133     log.info("{} - Closing the KafkaConsumer", id);
134     consumer.close(Duration.ofSeconds(3));
135   }
136 }