【问题标题】:Sending message to a specific group of subscriber in azure service bus topic with masstransit使用 masstransit 向 Azure 服务总线主题中的特定订阅者组发送消息
【发布时间】:2021-07-31 03:31:02
【问题描述】:

我是 azure service bus 和 masstransit 的新手。我正在寻找针对特定情况的解决方案。

我有一个包含多个订阅者的天蓝色服务总线主题。订阅者将根据过滤器接收消息。我用下面的代码创建了主题和订阅者

    class Program
        {
            static void Main(string[] args)
            {
    
                string connectionString = "Endpoint connection string";
    
                // the names of topics and subscriptions we'll be working with
                const string topicName = "MyTestTopic";
                const string allMessagesSubName = "AllMessages";
                const string filteredSubName1 = "Filtered1";
                const string filteredSubName2 = "Filtered2";
    
                // let's create the topic if it doesn't already exist...
                var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
                if (!namespaceManager.TopicExists(topicName))
                {
                    var td = new TopicDescription(topicName);
                    namespaceManager.CreateTopic(td.Path);
                }
                if (!namespaceManager.SubscriptionExists(topicName, allMessagesSubName))
                {
                    namespaceManager.CreateSubscription(topicName, allMessagesSubName);
                }
    
                if (!namespaceManager.SubscriptionExists(topicName, filteredSubName1))
                {
                    namespaceManager.CreateSubscription(
                        new SubscriptionDescription(topicName, filteredSubName1),
                        new Microsoft.ServiceBus.Messaging.SqlFilter("From LIKE '%Smith'"));
                }
    
                if (!namespaceManager.SubscriptionExists(topicName, filteredSubName2))
                {
                    namespaceManager.CreateSubscription(
                        new SubscriptionDescription(topicName, filteredSubName2),
                        new Microsoft.ServiceBus.Messaging.SqlFilter("sys.Label='important'"));
                }
    
                var message1 = new BrokeredMessage("Hello World");
    
                var message2 = new BrokeredMessage("Second message");
                message2.Label = "important";
    
                var message3 = new BrokeredMessage("Third message");
                message3.Properties["From"] = "Kelly Smith";
                message3.Label = "information";
    
                var client = TopicClient.CreateFromConnectionString(connectionString, topicName);
                client.Send(message1);
                client.Send(message2);
                client.Send(message3);
                client.Close();
            }
        }

我们在代码中添加消息自定义属性,例如From

现在我想使用 masstransit 发送此类消息。在公共交通中,我找不到使用 Publish() 方法添加 Message 自定义属性 的任何选项。有什么方法可以使用可以使用这些过滤器的公共交通发送这些消息?

注意:我已经阅读了这个question 的答案,但是这里的答案告诉我们在订阅者端过滤消息。我想要的是这种过滤将在到达订阅者之前发生。

【问题讨论】:

    标签: azure azureservicebus masstransit azure-servicebus-topics azure-servicebus-subscriptions


    【解决方案1】:

    在将 Azure 服务总线与 MassTransit 结合使用时,除了常规端点之外,您还可以添加 订阅端点。配置订阅端点时,您应该能够指定规则和/或过滤器作为订阅的一部分。这正是您在上面所做的,因此已处理。

    另一部分,向消息添加属性,可以通过将文本标题添加到SendContext 来完成。这些标头被复制到消息属性集合中,我相信它可用于使用“SQL”过滤器(在订阅端点上配置,或在接收端点上配置主题订阅)过滤消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 2018-07-25
      • 2017-10-23
      • 2019-09-18
      • 1970-01-01
      相关资源
      最近更新 更多