package de.juplo.kafka;
+import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
+import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
+@RequiredArgsConstructor
@Slf4j
public class ApplicationRecordHandler implements RecordHandler<String, String>
{
+ private final AdderResults results;
+ private final Optional<Duration> throttle;
+ private final String id;
+
private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
if (message.equals("CALCULATE"))
{
AdderResult result = state.get(partition).calculate(user);
- log.info("New result for {}: {}", user, result);
- return;
+ log.info("{} - New result for {}: {}", id, user, result);
+ results.addResults(partition, user, result);
+ }
+ else
+ {
+ state.get(partition).addToSum(user, Integer.parseInt(message));
}
- state.get(partition).addToSum(user, Integer.parseInt(message));
+ if (throttle.isPresent())
+ {
+ try
+ {
+ Thread.sleep(throttle.get().toMillis());
+ }
+ catch (InterruptedException e)
+ {
+ log.warn("{} - Intrerrupted while throttling: {}", id, e);
+ }
+ }
}
protected void addPartition(Integer partition, Map<String, AdderResult> state)
{
return state;
}
+
+ public AdderBusinessLogic getState(Integer partition)
+ {
+ return state.get(partition);
+ }
}