การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต
เรียนรู้การตอบสนองต่อผลลัพธ์ของการส่ง Kafka แบบอะซิงโครนัสด้วยการเรียกกลับ CompletableFuture และการกำหนดค่า acks เพื่อรับประกันความน่าเชื่อถือในการส่ง
การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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>>.
SendResultholds theRecordMetadata(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: 2Trade-off: Latency vs Durability
Stronger acks mean higher latency.
acks=0is fastest but loses data on broker failure.acks=allis 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 aCompletableFuture<SendResult>.- Use
whenCompleteto inspect metadata or failures. ackscontrols the durability vs latency trade-off.- Centralize callbacks for consistent reliability handling.
คำถามที่พบบ่อย
บทเรียน “การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต”
เรียนรู้การตอบสนองต่อผลลัพธ์ของการส่ง Kafka แบบอะซิงโครนัสด้วยการเรียกกลับ CompletableFuture และการกำหนดค่า acks เพื่อรับประกันความน่าเชื่อถือในการส่ง คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม
ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผนวก Spring Kafka Starter
- การส่งข้อความด้วย KafkaTemplate
- การปรับแต่งการกำหนดค่าผู้ผลิต
- การจัดการการตอบกลับและการยืนยันการส่งของผู้ผลิต