当前位置: 首页 > 知识库问答 >
问题:

使用筛选的订阅从主题检索Azure服务总线消息

宗安翔
2023-03-14

我试图遵循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"]);

}

共有1个答案

蒯慈
2023-03-14

密码没问题。运行此示例后,allmessages包含三条消息,filtered1包含两条消息,filtered2包含一条消息。您确定没有另一个使用代码的实例正在运行并检索这些消息吗?

allmessages有一个默认的SQL筛选器,因此它是一个“catch-all”订阅。筛选不会对它产生影响,它应该获得发送到rightangle主题的任何消息。如果在它下面没有看到任何消息,那么要么消息已被使用,要么应用程序正在重新构建实体,从而删除所有消息。

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

 类似资料:
  • 我之所以要这样做,是因为我们的服务在部署时配置订阅规则,并且具有消息代理的服务可能会在具有订阅客户端的服务更改规则集和新的业务逻辑之前部署该服务,该服务会发送一组新的消息。我们不希望丢失在部署期间发送的消息,并在新服务退出时处理它们。 干杯。

  • 我试着按照1给出的教程学习。我创建了一个主题,其中有两个主题的订阅,分别使用sqlFilter(user_age<50)和(user_age>=50)。我为消息定义了一个自定义属性user_age。但当我发送关于这个主题的消息时,两个订阅都收到相同的消息,这是胡说八道!知道吗? 我的代码完全是从tuto中给出的代码中获得灵感的,只是我使用这段代码接收来自给定订阅的消息:

  • 有可能做到这一点吗?

  • 我想将一个小的JSON消息放入中。消息将具有附加到它的“ProviderID”属性,并且根据筛选规则,该消息将被筛选到特定于提供程序的上 但是,我似乎无法在上指定共享访问策略,以限制第三方提供商仅连接到他们自己的 我假设应该在订阅上设置以便将这些消息发送到另一个并在那里应用特定于提供程序的安全性,这样做是否正确。 或者有其他/更好的/推荐的方法来做这件事。

  • 我有一个名为“状态更改”的Azure服务总线主题,它有一个名为“混响”的订阅。我正在尝试使用设置订阅主题的方法,但出现错误: 我一直在使用这篇博客文章来尝试让一切正常运行:http://ramblingstechnical.blogspot.co.uk/p/using-azure-service-bus-with-spring-jms.html 我可以使用向主题添加消息,并使用Azure文档中概述

  • 我已经能够弄清楚如何设置Azure ARM模板,该模板创建/管理Azure服务总线名称空间、主题和订阅,以接收所有消息。但是,ARM Tempates上的Microsoft文档仍然非常缺乏,我无法弄清楚如何在模板中为订阅定义一个SqlFilter,您可以使用.NET SDK管理该模板。 有人知道如何在ARM模板中向服务总线主题订阅添加Sql筛选器吗? 这里有一个ARM模板的链接,用于创建服务总线主