Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Ders

Üretici Gönderim Geri Çağrılarını ve Onaylarını Yönetme

Teslimat güvenilirliğini sağlamak için geri çağrılar, CompletableFuture ve acks yapılandırmasını kullanarak eşzamansız Kafka gönderimlerinin sonucuna nasıl tepki vereceğinizi öğrenin.

4. ders / 413 adım

Üretici Gönderim Geri Çağrılarını ve Onaylarını Yönetme, CoddyKit'te ücretsiz bir Advanced Spring Boot 4: Event-Driven Architecture (Kafka) dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Advanced Spring Boot 4: Event-Driven Architecture (Kafka) öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Why Send Results Matter

When you call KafkaTemplate.send(), the message is dispatched asynchronously. The call returns immediately, but the broker may not have stored the record yet.

To build reliable producers you must inspect the result of each send so you can log, retry, or alert on failures.

The CompletableFuture Result

In Spring Kafka, send() returns a CompletableFuture<SendResult<K,V>>.

  • SendResult holds the RecordMetadata (partition, offset, timestamp).
  • You attach a continuation to handle success or failure.
CompletableFuture<SendResult<String,String>> future =
    kafkaTemplate.send("orders", order.getId(), payload);

Attaching whenComplete

Use whenComplete to handle both outcomes in one place. The first argument is the result, the second is the exception (null on success).

future.whenComplete((result, ex) -> {
    if (ex == null) {
        var md = result.getRecordMetadata();
        log.info("Sent to partition {} offset {}", md.partition(), md.offset());
    } else {
        log.error("Send failed", ex);
    }
});

Reading RecordMetadata

On success, RecordMetadata tells you exactly where the record landed.

  • partition() — which partition received it.
  • offset() — its position in that partition.
  • timestamp() — broker-assigned timestamp.
RecordMetadata md = result.getRecordMetadata();
String location = md.topic() + "-" + md.partition() + "@" + md.offset();

Blocking for the Result

Sometimes you need a synchronous guarantee. Call get() with a timeout to block until the broker acknowledges.

Use this sparingly — it kills throughput, but it is useful in tests or critical writes.

SendResult<String,String> result =
    kafkaTemplate.send("orders", payload).get(10, TimeUnit.SECONDS);

The acks Configuration

Acknowledgment durability is controlled by the producer acks setting:

  • acks=0 — fire and forget, no guarantee.
  • acks=1 — leader writes, then acknowledges.
  • acks=all — leader plus all in-sync replicas acknowledge.

Configuring acks in Spring Boot

Set the strongest durability with acks=all in your application.yml.

spring:
  kafka:
    producer:
      acks: all
      properties:
        min.insync.replicas: 2

Trade-off: Latency vs Durability

Stronger acks mean higher latency.

  • acks=0 is fastest but loses data on broker failure.
  • acks=all is safest but waits for replica confirmation.

For financial events choose all; for high-volume metrics 1 may suffice.

Centralizing Callback Logic

Avoid duplicating callback code. Wrap sends in a helper method that always logs metadata and failures consistently.

public void sendTracked(String topic, String key, String value) {
    kafkaTemplate.send(topic, key, value)
        .whenComplete((res, ex) -> {
            if (ex != null) metrics.incrementFailures();
            else metrics.incrementSuccess();
        });
}

Handling Failures Gracefully

In the failure branch you can:

  • Persist the failed payload to a fallback store.
  • Increment a failure metric for alerting.
  • Schedule a retry on a separate executor.

Never swallow the exception silently.

Putting It Together

A robust producer combines acks=all, callback inspection, and failure handling. This gives you observability and delivery guarantees without blocking your main flow.

future.whenComplete((res, ex) -> {
    if (ex != null) deadLetterStore.save(payload);
});

Quick Check

Test your understanding of producer acknowledgments.

Recap

You learned to handle the asynchronous result of Kafka sends.

  • send() returns a CompletableFuture<SendResult>.
  • Use whenComplete to inspect metadata or failures.
  • acks controls the durability vs latency trade-off.
  • Centralize callbacks for consistent reliability handling.
Başlamak ücretsiz

Yapay zeka eğitmeniyle Advanced Spring Boot 4: Event-Driven Architecture (Kafka) öğren — ücretsiz

Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.

Kurslar
12
Dersler
48

Sıkça Sorulan Sorular

“Üretici Gönderim Geri Çağrılarını ve Onaylarını Yönetme” dersi ücretsiz mi?

Evet — “Üretici Gönderim Geri Çağrılarını ve Onaylarını Yönetme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Advanced Spring Boot 4: Event-Driven Architecture (Kafka) kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) kursu toplamda 4 dersten oluşur.

“Üretici Gönderim Geri Çağrılarını ve Onaylarını Yönetme” dersinde ne öğreneceğim?

Teslimat güvenilirliğini sağlamak için geri çağrılar, CompletableFuture ve acks yapılandırmasını kullanarak eşzamansız Kafka gönderimlerinin sonucuna nasıl tepki vereceğinizi öğrenin. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Advanced Spring Boot 4: Event-Driven Architecture (Kafka) öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Advanced Spring Boot 4: Event-Driven Architecture (Kafka), başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Üretici Gönderim Geri Çağrılarını ve Onaylarını Yönetme” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Advanced Spring Boot 4: Event-Driven Architecture (Kafka) dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Advanced Spring Boot 4: Event-Driven Architecture (Kafka) dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Spring Kafka Başlatıcısını Entegre Etme
  2. KafkaTemplate ile Mesaj Gönderme
  3. Üretici Yapılandırmalarını Özelleştirme
  4. Üretici Gönderim Geri Çağrılarını ve Onaylarını Yönetme
← Advanced Spring Boot 4: Event-Driven Architecture (Kafka) Sayfasına Dön