Question

Hy,

I have a question regarding the LockDuration Property. I have this receive function:

// Use the MessagingFactory to create a queue client for the orderqueue.
QueueClient queueClient = factory.CreateQueueClient("orderqueue");

// Receive messages from the queue with a 10 second timeout.
while (true)
{
    // Receive a message using a 10 second timeout
    BrokeredMessage msg = queueClient.Receive(TimeSpan.FromSeconds(10));

    if (msg != null)
    {
        // Deserialize the message body to an order data contract.
        Order order = msg.GetBody<Order>();
        // Output the order.
        Console.WriteLine("{0} {1} {2} {3} {4} ${5}",
        order.OrderNumber,
        order.Customer.FirstName,
        order.Customer.LastName,
        order.ShipTo.City,
        order.ShipTo.Province,
        order.Total);
        // Update the database
        try
        {
            // Add the order to the database.
            OrdersData.AddOrder(order);
            // Mark the message as complete.
            msg.Complete();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: {0}", ex.Message);
            // Something went wrong, abandon the message.
            msg.Abandon();
        }
    }
    else
    {
        // No message has been received, we will poll for more messages.
        Console.WriteLine("Polling, polling, polling...");
    }
}

If I receive a message like in the example above. I delete the message with the Complete() function if everything is ok. If something went wrong I call the Abondon() function, so the message gets unlocked. So my queustion:

There is the QueueDescription.LockDuration Property and SubscriptionDescription.LockDuration Property to set the lock duration of a message when I use the peeklock recevie mode. You can change it to 5 minutes. Somewhere I read you should set the value of this proberty carefully. Why I shouldn't set it to the 5 minutes, because the message is unlocked anyway if there is an error with the abandom() function (see the catch block in the code example).

Best regards

Was it helpful?

Solution

The main considerations for deciding the lock duration are:

1) How long a delay are you read for in case of failure?

2) How long does it take to process a message?

Assume you set lock duration to 5 minutes, then lock a message and your processor dies. This means that message will be available to the next receiver after 5 minutes. If there is no failure and you complete or even abandon the message then it will be available right away.

Assume you need mostly 1 minute to process a message, you can set the lock duration to say 2 minutes and not have to renew locks but if you need 10 minutes to process then you will need to call RenewLock appropriately. So if you do not care much about the first case (latency in case of failure) and want to avoid renewing locks where your message processing can always complete in 5 minutes then choosing 5 minutes all the time is fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top