Kafka: Spring KafkaTemplate returns a CompletableFuture

Since Spring Kafka 3.0 it is a CompletableFuture rather than a ListenableFuture.

Code
kafkaTemplate.send("orders", order.id(), payload)
        .whenComplete((result, ex) -> {
            if (ex != null) { log.error("send failed", ex); return; }
            RecordMetadata md = result.getRecordMetadata();
            log.info("sent to {}-{} offset {}", md.topic(), md.partition(), md.offset());
        });
Output
sent to orders-3 offset 118
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11