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.util.HashMap;
11 @RequiredArgsConstructor
13 public class ApplicationRecordHandler implements RecordHandler<String, String>
15 private final AdderResults results;
17 private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
21 public void accept(ConsumerRecord<String, String> record)
23 Integer partition = record.partition();
24 String user = record.key();
25 String message = record.value();
27 if (message.equals("CALCULATE"))
29 AdderResult result = state.get(partition).calculate(user);
30 log.info("New result for {}: {}", user, result);
31 results.addResults(partition, user, result);
35 state.get(partition).addToSum(user, Integer.parseInt(message));
38 protected void addPartition(Integer partition, Map<String, AdderResult> state)
40 this.state.put(partition, new AdderBusinessLogic(state));
43 protected Map<String, AdderResult> removePartition(Integer partition)
45 return this.state.remove(partition).getState();
49 public Map<Integer, AdderBusinessLogic> getState()
54 public AdderBusinessLogic getState(Integer partition)
56 return state.get(partition);