[英]How to check in C # that a message sent by a producer has successfully reached the Kafka server?
I can't find a way to confirm that a message sent to the Kafka server has been received correctly by the server.我找不到确认发送到 Kafka 服务器的消息是否已被服务器正确接收的方法。 I need a confirmation that the message was sent and received correctly in order to act on the C # code in case the message was not received by the server.
我需要确认消息已正确发送和接收,以便在服务器未收到消息的情况下对 C# 代码执行操作。 I have read that there is a property called ack to configure that, but I can't find how to receive such confirmation.
我读过有一个名为 ack 的属性来配置它,但我找不到如何接收这样的确认。
You'd use a DeliveryReport handler您将使用 DeliveryReport 处理程序
Refer documentation - https://docs.confluent.io/clients-confluent-kafka-dotnet/current/overview.html参考文档 - https://docs.confluent.io/clients-confluent-kafka-dotnet/current/overview.html
Async异步
var t = producer.ProduceAsync("topic", new Message<Null, string> { Value="hello world" });
t.ContinueWith(task => {
if (task.IsFaulted)
{
...
}
else
{
...
Console.WriteLine($"Wrote to offset: {task.Result.Offset}");
}
});
Synchronous同步
public static void handler(DeliveryReport<Null, string>)
{
...
}
public static process(...)
{
...
producer.Produce(
"my-topic", new Message<Null, string> { Value = "hello world" }, handler);
}
This worked for me:这对我有用:
bool messagePublished;
var result = await _producer.ProduceAsync(yourTopic, yourMessage);
if (result.Status == PersistenceStatus.Persisted)
messagePublished = true;
Notice that you have multiple PersistenceStatus
values.请注意,您有多个
PersistenceStatus
值。
You can find more info on the official confluent documentation .您可以在官方confluent 文档中找到更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.