使用具有多个规则描述的单个订阅读取Azure消息

时间:2016-07-07 06:41:22

标签: azure azure-servicebus-topics

我有一个名为" AZTopic"的Azure主题我订阅了三个规则说明添加到它。规则描述具有基于属性优先级的sql过滤器。当我尝试从订阅访问数据时,我将作为代理消息获取null。

private static void CreateSubscription(string filterName, string subscriptionName)
    {            
        if (!namespaceManager.SubscriptionExists(Constants.Topic, subscriptionName))
        {
           namespaceManager.CreateSubscription(Constants.Topic, subscriptionName);                
        }
        SubscriptionClient subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, Constants.Topic, subscriptionName);

        var rules = namespaceManager.GetRules(Constants.Topic,Constants.P0Subscription);
        foreach (var rule in rules)
        {
            subscriptionClient.RemoveRule(rule.Name);
        }

        var P0FilterRule = new RuleDescription()
        {
            Filter = new SqlFilter(Constants.P0Filter),
            Name = "P0"
        };
        var P1FilterRule = new RuleDescription()
        {
            Filter = new SqlFilter(Constants.P1Filter),
            Name = "P1"
        };
        var P2FilterRule = new RuleDescription()
        {
            Filter = new SqlFilter(Constants.P2Filter),
            Name = "P2"
        };

        subscriptionClient.AddRule(P0FilterRule);
        subscriptionClient.AddRule(P1FilterRule);
        subscriptionClient.AddRule(P2FilterRule);           
    }

这是我的订阅创建代码

private QueueMessage GetMessage(string filterName, string subscriptionName)
    {
        BrokeredMessage receivedMessage = new BrokeredMessage();
        QueueMessage messageObject = new QueueMessage();
        SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString(Settings.connectionString, Constants.Topic, subscriptionName);
        try
        {
            receivedMessage = Client.Receive();
            if (receivedMessage != null)
            {
                messageObject = receivedMessage.GetBody<QueueMessage>();
                receivedMessage.Complete();
            }
        }
        catch (Exception ex)
        {
            Logger.Log(ex.GetBaseException().Message, LogLevel.Informational, "");
            receivedMessage.Abandon();
        }

        return messageObject;
    }

这是我的消息阅读代码

0 个答案:

没有答案