【问题标题】:Retrieving Azure Service Bus messages from Topic using a filtered subscription使用筛选订阅从主题中检索 Azure 服务总线消息
【发布时间】:2018-02-03 06:01:45
【问题描述】:

我正在尝试关注 Mark Heath 的控制台应用程序 example 以使用过滤订阅检索服务总线主题消息。但是,我实际上无法检索实际过滤的消息(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"]);

}

【问题讨论】:

    标签: azure azureservicebus azure-servicebus-topics


    【解决方案1】:

    代码很好。运行此示例后,AllMessages 包含三条消息,Filtered1 有两条消息,Filtered2 有一条消息。您确定没有其他使用代码的实例正在运行并检索这些消息吗?

    AllMessages 有一个默认的 SQL 过滤器,所以它是一个“包罗万象”的订阅。过滤不会影响它,它应该将 any 消息发送到rightangle 主题。如果您在其下看不到任何消息,则表明消息已被消耗,或者应用程序正在重新构建实体并因此删除所有消息。

    尝试更改接收方代码以不删除实体,而仅接收。并修改发件人以确保实体存在,如果不创建实体,但不删除。或者,您可以将两者结合起来(首先是接收者代码,然后是发送者),您将看到这些消息。问题仅在于如何管理(删除)实体。

    【讨论】:

    • 谢谢,您的回答迫使我更仔细地查看我的代码。我的示例代码不包括我添加了一个 try..catch..finally 块的事实,并且在 finally 部分中我包含了一个 Console.Readline() 方法。显然 AllMessages 订阅会立即得到处理,然后它会在 Readline() 处等待,直到按下一个键,然后再处理其余代码。删除 Readline() 允许相应地处理和过滤消息。谢谢。
    • 很高兴它帮助了你。
    猜你喜欢
    • 2019-02-12
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多