I invoke client.Send(brokeredMessage);
once, but I receive the message several times. For handle queue I use this code
private static void HandleQueue(string queueName, MessageHandler messageHandler)
{
// Create the queue if it does not exist already
string connectionString =
Configuration.GetConnectionString("Microsoft.ServiceBus.ConnectionString",false);
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists(queueName))
{
namespaceManager.CreateQueue(queueName);
}
QueueClient client =
QueueClient.CreateFromConnectionString(connectionString, queueName);
while (true)
{
BrokeredMessage message = client.Receive();
if (message != null)
{
try
{
messageHandler(message);
// Remove message from queue
message.Complete();
}
catch (Exception)
{
// Indicate a problem, unlock message in queue
message.Abandon();
}
}
}
}
Problem is that BrokeredMessage message = client.Receive();
is invoked several times and return the same message, if execution of messageHandler(message);
takes long time. How can i fix it?
The message is getting unlocked while you are a processing it. The right place to set the lock timeout for the message is on the QueueDescription
http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.queuedescription.lockduration.aspx
The max time allowed here is 5 Minutes, so if you need to process the message for longer then you can call RenewLock
on the message to continue to keep it invisible to other consumers. You are right in that calling Complete
before you have finished processing is not advisable as if you process crashes then you will not get the message back again.
The above mentioned property of BrokeredMessage.ScheduledEnqueueTimeUtc
is used to "delay" when a message shows up to consumers from the Queue. Say you send a message on Day 1 and set the scheduled time to day 2 then the message will not be returned by the Recieve
call until Day 2.
There are a few things you should do:
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With