【问题标题】:How to check specific record exist in azure service bus topic如何检查 Azure 服务总线主题中是否存在特定记录
【发布时间】:2020-04-02 17:14:02
【问题描述】:

我如何检查 Azure 服务总线主题中是否存在特定记录。就我而言,多个客户端将记录输入到服务总线主题中,因此我需要验证现有记录。如果主题中不存在记录,则输入记录。我需要验证 SendMessageAsync 方法中的记录 以下是我的代码

    private ITopicClient client;
    private DateTime tokenExpiresAtUtc = DateTime.MinValue;
    private string tokenValue = string.Empty;
    public async Task SendOrderMessageAsync(string OrderNumber, int RecipientStoreId)
    {
        await SendMessageAsync(GetMessage(JsonConvert.SerializeObject(new OrderNotificationViewModel
        {
            SenderStoreId = this.SenderStoreId.Value,
            NotificationType = NotificationTypes.Order,
            OrderNumber = OrderNumber,
            StoreId = RecipientStoreId
        })), RecipientStoreId);

    }
    private async Task SendMessageAsync(Message message, int StoreId)
    {
        try
        {
            await (await GetClient(GetTopicName(StoreId))).SendAsync(message);
        }
        catch (MessagingEntityNotFoundException ex)
        {
            var topic = await CreateTopic(GetTopicName(StoreId));
            var subscription = await CreateSubscription(topic.Name, GetSubscriptionName(StoreId));
            await (await GetClient(GetTopicName(StoreId))).SendAsync(message);
        }
    }
    public async Task SendOrderNotesMessageAsync(string OrderNumber, List<string> Notes, int RecipientStoreId)
    {
        await SendMessageAsync(GetMessage(JsonConvert.SerializeObject(new OrderNotesNotificationViewModel
        {
            SenderStoreId = this.SenderStoreId.Value,
            NotificationType = NotificationTypes.OrderNotes,
            OrderNumber = OrderNumber,
            Notes = Notes,
            StoreId = RecipientStoreId
        })), RecipientStoreId);
    }
    private async Task<ITopicClient> GetClient(string TopicName)
    {
        if (client != null && client.TopicName != TopicName)
        {
            await client.CloseAsync();
            client = null;
        }
        if (client == null)
        {
            client = new TopicClient(GlobalConfig.Instance.ServiceBusConnectionString, TopicName);
        }
        return client;
    }
    private string GetTopicName(int StoreId)
    {
        return GlobalConfig.Instance.ServiceBusTopicNamePattern.Replace("{StoreId}", StoreId.ToString());
    }
    private string GetSubscriptionName(int StoreId)
    {
        return GlobalConfig.Instance.ServiceBusSubscriptionNamePattern.Replace("{StoreId}", StoreId.ToString());
    }
    private Message GetMessage(string messageBody)
    {
        return new Message(Encoding.UTF8.GetBytes(messageBody));
    }

【问题讨论】:

    标签: azure azureservicebus azure-servicebus-queues azure-servicebus-topics


    【解决方案1】:

    不要这样做。服务总线是一种消息传递服务,而不是数据存储。 主题专为多播和将发送者/发布者与接收者/订阅者分离而设计​​。

    如果您需要重复消息检测,请使用专门为此设计的feature。您可以利用重复数据删除来处理您需要的任何内容,我描述了 here

    【讨论】:

    • 我的想法完全相同,但在我的情况下,有多个发布者,因此在发布任何消息之前必须进行验证。我正在开始部署您的解决方案并通知您。感谢您的支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多