1 package de.juplo.kafka;
3 import lombok.RequiredArgsConstructor;
4 import lombok.extern.slf4j.Slf4j;
6 import java.time.Duration;
7 import java.util.HashMap;
9 import java.util.Optional;
12 @RequiredArgsConstructor
14 public class ApplicationRecordHandler implements RecordHandler
16 private final AdderResults results;
17 private final Optional<Duration> throttle;
18 private final String id;
20 private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
24 public void addNumber(
29 MessageAddNumber message)
31 state.get(partition).addToSum(user, message.getNext());
36 public void calculateSum(
41 MessageCalculateSum message)
43 AdderResult result = state.get(partition).calculate(user);
44 log.info("{} - New result for {}: {}", id, user, result);
45 results.addResults(partition, user, result);
49 private void throttle()
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);