1 package de.juplo.kafka;
3 import lombok.RequiredArgsConstructor;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerRecord;
7 import java.time.Duration;
8 import java.util.HashMap;
10 import java.util.Optional;
13 @RequiredArgsConstructor
15 public class ApplicationRecordHandler implements RecordHandler<String, Message>
17 private final AdderResults results;
18 private final Optional<Duration> throttle;
19 private final String id;
21 private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
24 public void addNumber(
27 MessageAddNumber message)
29 state.get(partition).addToSum(user, message.getNext());
32 public void calculateSum(
35 MessageCalculateSum message)
37 AdderResult result = state.get(partition).calculate(user);
38 log.info("{} - New result for {}: {}", id, user, result);
39 results.addResults(partition, user, result);
43 public void accept(ConsumerRecord<String, Message> record)
45 Integer partition = record.partition();
46 String user = record.key();
47 Message message = record.value();
49 // TODO: JSON-Nachrichten verarbeiten
51 if (throttle.isPresent())
55 Thread.sleep(throttle.get().toMillis());
57 catch (InterruptedException e)
59 log.warn("{} - Intrerrupted while throttling: {}", id, e);
64 protected void addPartition(Integer partition, Map<String, AdderResult> state)
66 this.state.put(partition, new AdderBusinessLogic(state));
69 protected Map<String, AdderResult> removePartition(Integer partition)
71 return this.state.remove(partition).getState();
75 public Map<Integer, AdderBusinessLogic> getState()
80 public AdderBusinessLogic getState(Integer partition)
82 return state.get(partition);