在服务总线主题中找到重复的消息

时间:2019-01-02 20:00:50

标签: c# azureservicebus

我想在ServiceBusTopics中找到重复的消息。下面是我的逻辑。如果我发现任何重复的消息,则添加到列表中并发送到另一个服务总线主题。但是我无法在遍历该列表之前读取订阅中存在的所有服务总线消息。我想从sevicebus中读取所有消息,然后遍历它。我不确定如何我应该停止执行循环,直到我从suscription读取所有消息为止

    private static string _serviceBusConn = "servicebusconnectionstring";
    private static string _serviceBustopic = "topic1";
    private static string _topic = "test_v1";

    static void Main(string[] args)
    {
        IList<string> items = new List<string>();
        int count; 
        IList<string> itemsRepeated = new List<string>();

        var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "DevTest");

        subClient.OnMessage(m =>
        {
            Stream stream = m.GetBody<Stream>();
            StreamReader reader = new StreamReader(stream);
            string s = reader.ReadToEnd();

            Console.WriteLine(s);

            items.Add(s);

        });


        List<string> copy1= new List<string>(items);
        List<string> copy2 = new List<string>(items);


        foreach (var item in copy1)
        {
            count = 0;
            foreach (var itemtopic in copy2)
            {
                count++;
                Console.WriteLine("{0}{1}{2}", items, itemtopic, count);
                if (item.Equals(itemtopic))
                {
                    count++;
                }

                if (count > 1)
                {
                    Console.WriteLine(count);
                    itemsRepeated.Add(itemtopic);
                }
            }
        }
        foreach (var repeateditem in itemsRepeated)
        {
            SendMessage(repeateditem);
        }

    }

    static void SendMessage(string message)
        {
            var topicClient = TopicClient.CreateFromConnectionString(_serviceBusConn, _topic);
            var msg = new BrokeredMessage(message);
            topicClient.Send(msg);
        }

2 个答案:

答案 0 :(得分:0)

您可以使用ReceiveBatch(Int32)方法来批量接收来自主题订阅的消息。您可以使用名称空间管理器的Getsubscription()方法获取主题订阅中的消息数。

var namespaceManager = NamespaceManager.CreateFromConnectionString(connnectionString);
var subscriptionDescription = namespaceManager.GetSubscription(topicName, subscriptionName);
var totalMessageCount= subscriptionDescription .MessageCount;

现在,您可以在循环中调用ReceiveBatch(minimumMessageCount),并在收到的消息计数达到totalMessageCount时终止循环。

int receivedMessageCount = 0;
List<BrokeredMessage> MessageList = new List<BrokeredMessage>();
do{
var messageList = subClient.ReceiveBatch(100);
receivedMessageCount += messageList.Count;
MessageList.AddRange(messageList); 
}while(receivedMessageCount < totalMessageCount);

现在,消息列表将在主题订阅中包含所有消息。您可以在消息列表上使用自定义逻辑来执行重复检测,并将其转发给另一个订阅。

  

注意:要接收来自ServiceBusTopic的所有消息,您必须接收来自该主题内所有主题订阅的所有消息。

答案 1 :(得分:0)

 if (count > 1)
                {
                    Console.WriteLine(count);
                    itemsRepeated.Add(itemtopic);
                }

您可以通过使用break来中断循环

   if (count > 1)
                        {
                            Console.WriteLine(count);
                            itemsRepeated.Add(itemtopic);
                            break;
                        }