簡體   English   中英

使用篩選的訂閱從主題檢索Azure Service Bus消息

[英]Retrieving Azure Service Bus messages from Topic using a filtered subscription

我正在嘗試遵循Mark Heath的控制台應用程序示例,以使用過濾的訂閱檢索服務總線主題消息。 但是,我實際上無法檢索實際的已過濾郵件(Filtered1,Filtered2)。 消息確實被消耗了,但我無法查看它們,因為代碼從未像使用未過濾的消息(AllMessages)一樣使它進入回調函數。 關於我所缺少的任何想法嗎?

發件人代碼

var body = "Hello World";
var message1 = new BrokeredMessage(body);
message1.Properties["From"] = "Ian Wright";

var message2 = new BrokeredMessage("Second message");
message2.Properties["From"] = "Alan Smith";
message2.Label = "important";

var message3 = new BrokeredMessage("Third message");
message3.Properties["From"] = "Kelly Smith";
message3.Label = "information";

var client =  TopicClient.CreateFromConnectionString(servicebusConnectionString, topicName);
 client.Send(message1);
 client.Send(message2);
 client.Send(message3);

接收方代碼

const string topicName = "rightangle";
const string subscriptionName = "AllMessages";
const string sub1Name = "Filtered1";
const string sub2Name = "Filtered2";

NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.SubscriptionExists(topicName, subscriptionName))
{
    namespaceManager.CreateSubscription(topicName, subscriptionName);
}
if (namespaceManager.SubscriptionExists(topicName, sub1Name))
{
    Console.WriteLine("Deleting subscription {0}", sub1Name);
    namespaceManager.DeleteSubscription(topicName, sub1Name);
}
Console.WriteLine("Creating subscription {0}", sub1Name);
namespaceManager.CreateSubscription(topicName, sub1Name, new SqlFilter("From LIKE '%Smith'"));

if (namespaceManager.SubscriptionExists(topicName, sub2Name))
{
    Console.WriteLine("Deleting subscription {0}", sub2Name);
    namespaceManager.DeleteSubscription(topicName, sub2Name);
}
Console.WriteLine("Creating subscription {0}", sub2Name);
namespaceManager.CreateSubscription(topicName, sub2Name, new SqlFilter("sys.Label='important'"));

var options = new OnMessageOptions();

var subClient =                    SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);
subClient.OnMessage(m => MessageReceived(subscriptionName, m), options);

var subClient1 =                    SubscriptionClient.CreateFromConnectionString(connectionString, topicName, sub1Name);

subClient1.OnMessage(m => MessageReceived(sub1Name, m), options);

var subClient2 =                    SubscriptionClient.CreateFromConnectionString(connectionString, topicName, sub2Name);

subClient2.OnMessage(m => MessageReceived(sub2Name, m), options);

private static void MessageReceived(string subscriptionName, BrokeredMessage message)
{
   Console.WriteLine("{0} '{1}' Label: '{2}' From: '{3}'", subscriptionName,
            message.GetBody<string>(),
            message.Label,
            message.Properties["From"]);

}

代碼很好。 運行此示例后, AllMessages包含三則消息, Filtered1有兩則消息, Filtered2有一則消息。 您確定沒有其他正在使用消耗代碼的實例正在運行並檢索這些消息嗎?

AllMessages具有默認的SQL篩選器,因此它是“全部捕獲”訂閱。 過濾不會影響它,它應該使任何消息發送到rightangle主題。 如果您沒有看到任何消息,則表明消息已被占用,或者應用程序正在重建實體,因此刪除了所有消息。

嘗試更改接收者代碼,而不是刪除實體,而僅刪除接收者。 並修改發送方以確保實體存在,如果不存在則創建實體,但不能刪除實體。 另外,您可以將二者結合在一起(首先是接收方代碼,然后是發送方),然后您將看到消息。 問題僅在於如何管理(刪除)實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM