From 0e18ab80a8f4c140b26a411ac6c2005c1d7ada6f Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 10 Sep 2022 11:13:03 +0200 Subject: [PATCH] WIP:kafkahandler --- .../juplo/kafka/ApplicationRecordHandler.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java index 521414f..61b265b 100644 --- a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java +++ b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java @@ -3,7 +3,13 @@ package de.juplo.kafka; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.springframework.kafka.annotation.KafkaHandler; import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.kafka.listener.adapter.ConsumerRecordMetadata; +import org.springframework.kafka.support.KafkaHeaders; +import org.springframework.messaging.handler.annotation.Header; +import org.springframework.messaging.handler.annotation.Headers; +import org.springframework.messaging.handler.annotation.Payload; import java.time.Duration; import java.util.HashMap; @@ -17,7 +23,6 @@ import java.util.Optional; id = "${spring.kafka.client-id}", idIsGroup = false, topics = "${sumup.adder.topic}", - batch = "true", autoStartup = "false") public class ApplicationRecordHandler implements RecordHandler { @@ -28,6 +33,26 @@ public class ApplicationRecordHandler implements RecordHandler private final Map state = new HashMap<>(); + @KafkaHandler + public void addNumber( + @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String user, + @Header(KafkaHeaders.RECEIVED_PARTITION_ID) Integer partition, + @Payload MessageAddNumber message) + { + state.get(partition).addToSum(user, message.getNext()); + } + + @KafkaHandler + public void calcSum( + @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String user, + @Header(KafkaHeaders.RECEIVED_PARTITION_ID) Integer partition, + @Payload MessageCalculateSum message) + { + AdderResult result = state.get(partition).calculate(user); + log.info("{} - New result for {}: {}", id, user, result); + results.addResults(partition, user, result); + } + @Override public void accept(ConsumerRecord record) { -- 2.20.1