[英]Sending message to a specific group of subscriber in azure service bus topic with masstransit
我是 azure 服务巴士和公共交通的新手。 我正在寻找针对特定情况的解决方案。
我有一个包含多个订阅者的 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();
}
}
在代码中,我们添加了Message 自定义属性,例如From 。
现在我想使用masstransit发送这样的消息。 在大众运输中,我找不到任何使用Publish()方法添加Message 自定义属性的选项。 有什么方法可以使用可以使用这些过滤器的大众运输发送这些消息吗?
注意:我已经阅读了这个问题的答案但是这里的答案告诉我们在订阅者端过滤消息。 我想要的是这种过滤会在到达订阅者之前发生。
将 Azure 服务总线与 MassTransit 结合使用时,除了常规终结点之外,还可以添加订阅终结点。 在配置订阅端点时,您应该能够指定规则和/或过滤器作为订阅的一部分。 这正是您在上面所做的,因此已处理。
另一部分,向消息添加属性,可以通过向SendContext
添加文本标SendContext
。 这些标头被复制到消息 Properties 集合中,我相信它可以用于使用“SQL”过滤器(在订阅端点上配置,或在接收端点上配置主题订阅)过滤消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.