4
votes

I am sending a message from web role to worker role. Earlier these messages were received properly and instantly but now out of nowhere some messages are not being received, even though the content of all messages are same. Like if i send one message now, its not received but immediately after that if i send another then the latest one is received. The one which was not received earlier, suddenly gets received after a few seconds or sometimes goes into dead letter messages as the time to live has expired.

I am not able to figure out what the problem can be, any ideas? or is this behaviour normal with service bus?

This is how am receiving the message in worker role

EDIT:

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;
     }

UPDATE :

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;
    }

Web Role:

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);
2
Can you share the code where you send the messages on your webrole? Also, how does BroadcastClient calls the servicebus APIs?Ramiro Berrelleza
Updated with what you asked for. Let me know if u find anything wrongBitsian

2 Answers

2
votes

The issue was arising because in our worker role we continuously check the service bus queues to see if a message has arrived. And when we start both the local and cloud worker roles, then both the worker roles try to receive the new message from the same service bus queue. And hence sometimes the cloud one accepts it and sometimes the local one.

Using different service bus queue names for cloud and local projects resolved the issue.

0
votes

In the worker role above, you are executing the code in Run only once. So essentially this indicates that a single message will be process every time the worker role is run? typical code to process messages in a loop looks as follows:

  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
            }
        }
    }