EventHubClient.SendBatchAsync-线程安全吗? [英] EventHubClient.SendBatchAsync - is it thread-safe?

查看:155
本文介绍了EventHubClient.SendBatchAsync-线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,EventHubClient的所有非静态调用都不是线程安全的.这意味着我无法在任何需要的地方轻松调用以下函数:

According to documentation all non-static calls of EventHubClient are not thread-safe. This means that I cannot easily call the following function from wherever I want:

    public async Task SendBatchAsync(IList<EventData> eventHubBatch)
    {
        try
        {
            await this.eventHubClient.SendBatchAsync(eventHubBatch);
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR (EH WRITER): {0}", ex.Message);
        }
    }

想知道有什么选择吗?

  1. 锁定
  2. 同步线程上下文
  3. 保存到某个队列并具有一个基于线程池的异步/等待while循环,该循环从队列读取并进一步发布到EventHub

更新:Event Hub团队的Eric确认可以安全地在多线程环境中使用SendAsync.

UPDATE: Eric from Event Hub team confirmed that it is safe to use SendAsync in multithreaded environment as is.

更新2 :MSDN文档刚刚更新(在2小时内,哇!).现在它说:任何类型的公共静态或实例成员都是线程安全的.".

UPDATE 2: MSDN documentation just got updated (within 2 hours, wow!). Now it says: "Any public static or instance members of this type are thread safe.".

推荐答案

Cesar提供了stackoverflow链接,可以很好地解决这个问题.为您的特定(EventHub)问题补充信息:

Cesar has provided the stackoverflow link that is fine as an answer. To supplement the info for your specific (EventHub) question:

  1. 如果您问如果多个线程使用同一实例调用API,我的数据是否会损坏",那么答案是否定的,我们保证在数据损坏的情况下线程安全.

  1. If you are asking "Will my data be corrupted if multiple threads call the API using the same instance" then answer is no, we do guarantee thread safety for data corruption scenario.

如果您问如果多个线程使用同一实例调用API,是否将保留我的数据顺序",答案取决于,因为我们在接收数据时会保留顺序,但是有多个线程调用相同的顺序实例意味着您不能真正确定哪个呼叫首先到达我们的网络实现层.如果您想订购,则将它们作为一个批次发送(S​​endBatchAsync),因为我们保证批量订购.请注意,虽然单个批次有大小限制(总计256k)

If you are asking "Will my data ordering be preserved if multiple threads call the API using the same instance" then answer is depends, since we do preserve order as we receive data, but having multiple threads call the same instance means you cannot really be sure which call comes to our network implementation layer first. If you want ordering, send them as a single batch (SendBatchAsync) since we do guarantee order in a batch. Note that a single batch has a size limit though (256k in total)

您提供的文档链接( https://msdn .microsoft.com/library/azure/microsoft.servicebus.messaging.eventhubclient.aspx )的信息不完整/不正确,我们将根据需要返回并进行审核/编辑.

The documentation link that you provided (https://msdn.microsoft.com/library/azure/microsoft.servicebus.messaging.eventhubclient.aspx) has incomplete/incorrect information and we will go back and review/edit as needed.

这篇关于EventHubClient.SendBatchAsync-线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆