From: Kai Moritz Date: Sat, 12 Nov 2022 11:32:11 +0000 (+0100) Subject: Der Producer versendet direkt in der for-Schleife X-Git-Tag: supersimple-producer---lvm-2-tage--easy-path~3 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=05ff677aad2231999d87ac3d5ce6ee5724ccf64f;p=demos%2Fkafka%2Ftraining Der Producer versendet direkt in der for-Schleife --- 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);