From 05ff677aad2231999d87ac3d5ce6ee5724ccf64f Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 12 Nov 2022 12:32:11 +0100 Subject: [PATCH] Der Producer versendet direkt in der for-Schleife --- src/main/java/de/juplo/kafka/Application.java | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/juplo/kafka/Application.java b/src/main/java/de/juplo/kafka/Application.java index 9781a7a..13ccd8f 100644 --- a/src/main/java/de/juplo/kafka/Application.java +++ b/src/main/java/de/juplo/kafka/Application.java @@ -11,7 +11,6 @@ import org.springframework.kafka.support.SendResult; import org.springframework.util.concurrent.ListenableFuture; - @Slf4j @SpringBootApplication public class Application implements ApplicationRunner @@ -19,32 +18,25 @@ public class Application implements ApplicationRunner @Autowired KafkaTemplate kafkaTemplate; - - void send(String key, String value) - { - ListenableFuture> listenableFuture = - kafkaTemplate.send("test", key, value); - - listenableFuture.addCallback( - result -> log.info( - "Sent {}={} to partition={}, offset={}", - result.getProducerRecord().key(), - result.getProducerRecord().value(), - result.getRecordMetadata().partition(), - result.getRecordMetadata().offset()), - e -> log.error("ERROR sendig message", e)); - } - @Override public void run(ApplicationArguments args) { for (int i = 0; i < 100; i++) { - send(Long.toString(i%10), Long.toString(i)); + ListenableFuture> listenableFuture = + kafkaTemplate.send("test", Long.toString(i%10), Long.toString(i)); + + listenableFuture.addCallback( + result -> log.info( + "Sent {}={} to partition={}, offset={}", + result.getProducerRecord().key(), + result.getProducerRecord().value(), + result.getRecordMetadata().partition(), + result.getRecordMetadata().offset()), + e -> log.error("ERROR sendig message", e)); } } - public static void main(String[] args) { SpringApplication.run(Application.class, args); -- 2.20.1