[英]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.