【问题标题】:How to check message arrival in service bus at every 20 seconds?如何每 20 秒检查一次消息到达服务总线?
【发布时间】:2015-12-28 01:40:20
【问题描述】:

我正在使用 azure 的 Service Bus,它将保存我的消息列表,这意味着消息可以随时进入服务总线。

所以我想监视我的服务总线以检查是否有任何消息在服务中。这就像我只想监视我的服务总线关于消息到达我的服务总线的时间间隔0f 20 秒。

我想每隔 20 秒检查一次消息到达我的服务总线,我想在后台异步执行。

我想在后台每 20 秒调用一次以下方法:

 private static void ReceiveMessages()
        {
            // For PeekLock mode (default) where applications require "at least once" delivery of messages 
            SubscriptionClient agentSubscriptionClient = SubscriptionClient.Create(TopicName, "AgentSubscription");
            BrokeredMessage message = null;
            while (true)
            {
                try
                {
                    //receive messages from Agent Subscription
                    message = agentSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
                    if (message != null)
                    {
                        Console.WriteLine("\nReceiving message from AgentSubscription...");
                        Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
                        // Further custom message processing could go here...
                        message.Complete();
                    }
                    else
                    {
                        //no more messages in the subscription
                        break;
                    }
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                    else
                    {
                        HandleTransientErrors(e);
                    }
                }
            }

            // For ReceiveAndDelete mode, where applications require "best effort" delivery of messages
            SubscriptionClient auditSubscriptionClient = SubscriptionClient.Create(TopicName, "AuditSubscription", ReceiveMode.ReceiveAndDelete);
            while (true)
            {
                try
                {
                    message = auditSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
                    if (message != null)
                    {
                        Console.WriteLine("\nReceiving message from AuditSubscription...");
                        Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
                        // Further custom message processing could go here...
                    }
                    else
                    {
                        Console.WriteLine("\nno more messages in the subscription");
                        //no more messages in the subscription
                        break;
                    }

                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                }
            }

            agentSubscriptionClient.Close();
            auditSubscriptionClient.Close();
        }

那么谁能告诉我如何每 20 秒调用一次上述方法?

【问题讨论】:

    标签: c# azure scheduled-tasks azureservicebus servicebus


    【解决方案1】:

    最适合初学者的解决方案是:

    从工具箱中拖出一个计时器,为其命名,设置所需的时间间隔并将“启用”设置为 True。然后双击 Timer,Visual Studio(或任何你正在使用的工具)将为你编写以下代码:

    private void wait_Tick(object sender, EventArgs e)
    {
        refreshText(); //add the method you want to call here.
    }
    

    或者这个

    private Timer timer1; 
    public void InitTimer()
    {
        timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 2000; // in miliseconds
        timer1.Start();
    }
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        isonline()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 2022-10-08
      • 2012-03-09
      • 2015-08-05
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多