gpt4 book ai didi

azure - 使用masstransit向azure服务总线主题中的特定订阅者组发送消息

转载 作者:行者123 更新时间:2023-12-03 05:40:04 28 4
gpt4 key购买 nike

我是 Azure 服务巴士和公共(public)交通的新手。我正在寻找针对特定情况的解决方案。

我有一个具有多个订阅者的 azure 服务总线主题。订阅者将根据过滤器接收消息。我已经使用下面的代码创建了主题和订阅者

    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();
}
}

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

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

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

最佳答案

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

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

关于azure - 使用masstransit向azure服务总线主题中的特定订阅者组发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59553301/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com