Export (0) Print
Expand All

How to Use the BrokeredMessage.Defer Method

Updated: February 4, 2015

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.

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,

  1. Add references to the following namespaces.

    Imports Microsoft.ServiceBus.Messaging
    Imports Microsoft.ServiceBus
  2. Create a MessagingFactory instance, and call the CreateQueueClient method. To create the MessagingFactory object, retrieve the Service Bus namespace URI, the Shared Access Signature (SAS) key name (default: “RootManageSharedAccessKey”), and the SAS key itself from the Azure management portal.

    Private Shared Function CreateQueueClient(queueName As String) As QueueClient
        ' The serviceBusNamespace and serviceBusSASKey return the values specific to an Azure account. 
        Dim serviceBusSASKey As String = "yourServiceBusSASKey"
        Dim serviceBusNamespace As String = "yourServiceBusNamespace"
        Dim serviceBusKeyName As String = "RootManageSharedAccessKey" ' The default is 'RootManageSharedAccessKey'.
        Dim runtimeUri As Uri = ServiceBusEnvironment.CreateServiceUri("sb", serviceBusNamespace, String.Empty)
        Dim myMessagingFactory As MessagingFactory = MessagingFactory.Create(runtimeUri, TokenProvider.CreateSharedAccessSignatureTokenProvider(serviceBusKeyName, serviceBusSASKey))
        Return myMessagingFactory.CreateQueueClient(queueName, ReceiveMode.PeekLock)
    End Function

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

    Dim deferredSequenceNumbers As List(Of Long) = New List(Of 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)
            // Low-priority messages will be dealt with later:
            if (receivedMessage.Properties["Priority"].ToString() == "Low")
               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: 
    Dim receivedMessage As BrokeredMessage = myQueueC.Receive(TimeSpan.FromSeconds(10))
    If receivedMessage Is Nothing Then
        ' Low-priority messages will be dealt with later:
        If receivedMessage.Properties("Priority").ToString() = "Low" Then
         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: 
        End If
     End If

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

    For each(sequenceNumber as long in deferredSequenceNumbers)
  2. The ProcessMessage method calls the Complete method on each message.

    Private Shared Sub ProcessMessage(message As BrokeredMessage)
        ' To do: code to process the message.
    End Sub

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

See Also

© 2015 Microsoft