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.errors.WakeupException;
8 import org.apache.kafka.common.serialization.LongDeserializer;
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", LongDeserializer.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     finally
85     {
86       log.info("{} - Unsubscribing...", id);
87       consumer.unsubscribe();
88       running = false;
89     }
90   }
91
92   public synchronized void start()
93   {
94     if (running)
95       throw new RuntimeException("Consumier instance " + id + " is already running!");
96
97     log.info("Running {}", id);
98     future = executor.submit(this);
99   }
100
101   public synchronized void stop() throws ExecutionException, InterruptedException
102   {
103     if (!running)
104       throw new RuntimeException("Consumier instance " + id + " is not running!");
105
106     log.info("Stopping {}", id);
107     running = false;
108     consumer.wakeup();
109     future.get();
110   }
111
112   @PreDestroy
113   public void destroy() throws ExecutionException, InterruptedException
114   {
115     stop();
116     log.info("{} - Closing the KafkaConsumer", id);
117     consumer.close(Duration.ofSeconds(3));
118   }
119 }