4

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

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

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

编辑:

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);
4

2 回答 2

2

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

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

于 2013-04-25T17:09:51.253 回答
0

在上面的 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
            }
        }
    }
于 2013-02-22T17:11:04.307 回答