SALES: 1-800-867-1380

How to Use the BrokeredMessage.Defer Method

Updated: January 18, 2013

When using the BrokeredMessage class you have the option of leaving a message in the queue using the Defer method. The deferred message can be processed later, at your discretion. Message deferral is useful for prioritizing messages, or for postponing a message that depends on some other process. After processing the message, call the Complete method to delete the message from the queue.

To use either the Defer or Complete method, set the queue client’s mode to ReceiveMode.PeekLock. Set the mode by calling the CreateQueueClient method of the MessagingFactory class.

When you defer the message, add the message's SequenceNumber to a collection. To retrieve the message, call the Receive method of the QueueClient class. Pass the sequence number of the message as an argument to the Receive method.

noteNote
To ensure that the message can be recalled, store the sequence numbers in a durable store. Otherwise, if the process fails, the record of the deferred messages will be lost.

To end the deferral, call the Complete method on each message, as needed.

For a the sample app that demonstrates the Defer method, see: Brokered Messaging: Deferred Messages,

To create the queue client

  1. Add references to the following namespaces.

    using Microsoft.ServiceBus.Messaging;
    using Microsoft.ServiceBus;
    
    
  2. Create a MessagingFactory instance, and call the CreateQueueClient method. To create the MessagingFactory, retrieve the Service Bus namespace URI, the issuer name (default: “owner”), and the access key from your Azure management portal.

    private static QueueClient CreateQueueClient(string queueName) 
    {
        // The serviceBusNamespace and serviceBusIssuerKey return the values specific to an Azure account. 
        string serviceBusIssuerKey = "yourServiceBusIssuerKey";
        string serviceBusNamespace = "yourServiceBusNamespace";
        string serviceBusIssuerName = "owner" ' The default is 'owner'.
        Uri runtimeUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceBusNamespace, string.Empty);
        MessagingFactory messagingFactory = MessagingFactory.Create(runtimeUri, 
            TokenProvider.CreateSharedSecretTokenProvider(serviceBusIssuerName, serviceBusIssuerKey));
        return messagingFactory.CreateQueueClient(queueName,ReceiveMode.PeekLock); 
    } 
    
    

To defer the message

  1. Create a generic List of type long to store the numbers.

    List<long> deferredSequenceNumbers = new List<long>();
    
  2. Call the Receive method on the client. Check each message for deferral criteria, and call the Defer method as needed. Add the sequence number to the List collection.

    BrokeredMessage receivedMessage = queueClient.Receive(TimeSpan.FromSeconds(10));
        if (receivedMessage == null)
        {
            break;
        }
        else
        {
            // Low-priority messages will be dealt with later:
            if (receivedMessage.Properties["Priority"].ToString() == "Low")
            {
               receivedMessage.Defer();
               Console.WriteLine("Deferred message with id {0}.", receivedMessage.MessageId);
               // Deferred messages can only be retrieved by message receipt. Here, keeping track of the
               // message receipt for a later retrieval: 
               deferredSequenceNumbers.Add(receivedMessage.SequenceNumber);
         }
         else
         {
               ProcessMessage(receivedMessage);
         }
      
    
    
    Dim receivedMessage As BrokeredMessage = myQueueC.Receive(TimeSpan.FromSeconds(10))
    If receivedMessage Is Nothing Then
        Resume
     Else
     
        ' Low-priority messages will be dealt with later:
        If receivedMessage.Properties("Priority").ToString() = "Low" Then
     
         receivedMessage.Defer()
         Console.WriteLine("Deferred message with id {0}.", receivedMessage.MessageId)
         ' Deferred messages can only be retrieved by message receipt. Here, keeping track of the
         '  message receipt for a later retrieval: 
         deferredSequenceNumbers.Add(receivedMessage.SequenceNumber)
        Else
            ProcessMessage(receivedMessage)
        End If
     End If
    
    

To process the deferred message

  1. Iterate through the collection of sequence numbers using the Receive method.

    foreach (long sequenceNumber in deferredSequenceNumbers)
    {
        ProcessMessage(queueClient.Receive(sequenceNumber));
    }
    
  2. The ProcessMessage method calls the Complete method on each message.

    private static void ProcessMessage(BrokeredMessage message)
    {
        // To do: code to process the message.
        message.Complete();
    }
    
    

For a the sample app that demonstrates the Defer method, see: Brokered Messaging: Deferred Messages,

See Also


Build Date:

2014-07-11
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft