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;
id = "${spring.kafka.client-id}",
idIsGroup = false,
topics = "${sumup.adder.topic}",
- batch = "true",
autoStartup = "false")
public class ApplicationRecordHandler implements RecordHandler<String, Message>
{
private final Map<Integer, AdderBusinessLogic> 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<String, Message> record)
{