【问题标题】:Azure Service Bus messages not being received未收到 Azure 服务总线消息
【发布时间】:2013-02-21 10:10:09
【问题描述】:

我正在从网络角色向工作角色发送消息。早些时候,这些消息可以立即正确接收,但现在有些消息没有被接收到,即使所有消息的内容都相同。就像我现在发送一条消息一样,它没有收到,但在我发送另一条消息之后立即收到最新的一条。之前没有收到的消息,几秒钟后突然收到,或者有时会因为生存时间到期而变成死信消息。

我无法弄清楚问题可能是什么,有什么想法吗?或者这种行为对于服务总线是否正常?

这就是我在工作角色中接收消息的方式

编辑:

public override void Run()
    {
        while (!IsStopped)
        {
            try
            {
                if (BroadcastReceived)
                {
                    BroadcastReceived = false;
                    // Receive the message from Web Role to upload the broadcast to queue
                    BroadcastClient.BeginReceive(OnWebRoleMessageReceived, null);
                }

                if (SignalRMessageReceived)
                {
                    SignalRMessageReceived = false;
                    // Receive the message from SignalR BroadcastHub
                    SignalRClient.BeginReceive(OnSignalRMessageReceived, null);
                }

                if (SignalRFirstTimeMessageReceived)
                {
                    SignalRFirstTimeMessageReceived = false;
                    // Receive the message from SignalR BroadcastHub
                    SignalRFirstTimeClient.BeginReceive(OnSignalRFirstTimeMessageReceived, null);
                }          
         }
     }


public void OnWebRoleMessageReceived(IAsyncResult iar)
    {
        BrokeredMessage receivedBroadcastMessage = null;
        receivedBroadcastMessage = BroadcastClient.EndReceive(iar);

        if (receivedBroadcastMessage != null)
        {
            // Process the message
           receivedBroadcastMessage.Complete();
        }
BroadcastReceived = true;
     }

更新:

BroadcastClient = CloudStorageHelper.GetServiceBusQueueClient(Queuenames.ApiToWorkerRole);

public static QueueClient GetServiceBusQueueClient(string queuename)
    {
        string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

        var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

        if (!namespaceManager.QueueExists(queuename))
        {
            namespaceManager.CreateQueue(queuename);
        }

        QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, queuename);



        return Client;
    }

网络角色:

var queueClient = CloudStorageHelper.GetServiceBusQueueClient(Queuenames.ApiToWorkerRole);

            //for testing purposes
            record.Username = "owner";
            record.Channel = "World";
            record.Title = "Sample";
            record.Duration = 10;

            BrokeredMessage message = new BrokeredMessage(record);

            queueClient.Send(message);

【问题讨论】:

  • 你能分享你在你的网站上发送消息的代码吗?另外,BroadcastClient 是如何调用 servicebus API 的?
  • 已根据您的要求进行了更新。如果您发现任何问题,请告诉我

标签: c# asp.net azure servicebus azure-servicebus-queues


【解决方案1】:

之所以出现问题,是因为在我们的工作者角色中,我们不断检查服务总线队列以查看是否有消息到达。当我们同时启动本地和云工作者角色时,两个工作者角色都会尝试从同一个服务总线队列接收新消息。因此,有时云端接受它,有时本地接受它。

为云和本地项目使用不同的服务总线队列名称解决了这个问题。

【讨论】:

    【解决方案2】:

    在上面的 worker 角色中,您只在 Run 中执行代码一次。所以本质上这表明每次运行工作者角色时都会处理一条消息?在循环中处理消息的典型代码如下所示:

      public override void Run()
        {
            while (!IsStopped)
            {
                try
                {
                    // Receive the message
                    BrokeredMessage receivedMessage = null;
                    receivedMessage = Client.Receive();
    
                    if (receivedMessage != null)
                    {
                        // Process the message
                        receivedMessage.Complete(); // Unless you do this the message goes back in the Queue
                    }
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Trace.WriteLine(e.Message);
                        throw;
                    }
    
                    Thread.Sleep(10000); // optional if you want to add a delay
                }
            }
        }
    

    【讨论】:

    • 抱歉回复晚了!我实际上是从 3 个不同的服务总线队列客户端发送和接收消息!我已经更新了工人角色代码。每次接收到的消息完成时,都将相应的布尔标志设置为 true,因此服务总线客户端将再次等待接收消息。如果您发现我的解决方案有任何问题,请告诉我,因为我仍然面临我提到的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    相关资源
    最近更新 更多