Official Content

This article contains an example that shows how to consume (defer) a message in Azure Service Bus Queue. 

Steps

The code consists of the following:

1. Connect to the Azure Service Bus using the AzureServiceBus.MessageBrokerProvider external object.
2. The Connect method returns a MessageBroker external object that should be used to receive the messages.
3. The message has to be received prior to consuming it.
The ReceiveMessage method returns a Message and a variable of GeneXus.Common.Messages to process the errors.
4. The ConsumeMessage method consumes the message received in step 3, also passing the ConsumeMessageOptions parameter depending on what you want to do with the message (complete, abandon, defer, etc).

Consider that:

  • The receive mode option has to be Peek Lock. See Settling receive operations in Azure Service Bus.
  • When a message is received in PeekLock mode, the message is locked in the server for this receiver instance for the duration specified in the Queue/Subscription creation (LockDuration).

Connecting to Azure Service Bus Queue

The following code is the one used to connect to an Azure Service Bus Queue:

brokerReceiverOptions = new()
&brokerReceiverOptions.ReceiveMode = ReceiveModeOptions.PeekLock
&brokerReceiverOptions.PrefetchCount = 10

&MessageBroker = AzureServiceBus.MessageBrokerProvider.Connect(&queueName,&queueConnection,&isSessionEnabled,&brokerReceiverOptions,&senderIdentifier,&errorMessages,&isOK)

Receiving a message from the Service Bus Queue

The following code is the one used for receiving messages from the Azure Service Bus Queue:

receiveMessageOptions.MaxWaitTime = 10
&receivedMessage = &MessageBroker.ReceiveMessage(&receiveMessageOptions.ToJson(),&errorMessages,&isOK)
if not &isOK
   for &errorMessage in &errorMessages
      msg(format(!"%1 (%2)",&errorMessage.Description, &errorMessage.Id), status)
   endfor
endif

Defer the message

To defer the message, use:

If &isOK
  &ConsumeMessaOptions = new()
  &ConsumeMessaOptions.ConsumeMode = ConsumeModeOptions.Defer
  &success =  &MessageBroker.ConsumeMessage(&receivedMessage,&ConsumeMessaOptions.ToJson(),&errorMessages)
endif

//retrieve sequenceNumber to be able to receive the message again
For &MessageProperty in &receivedMessage.MessageAttributes
    if (&MessageProperty.PropertyKey = !"SequenceNumber")
        &sequenceNumber = &MessageProperty.PropertyValue.ToNumeric()
        exit
    endif
endfor

Receiving the deferred message

To receive the deferred message, use:

&receiveMessageOptions = new()
&receiveMessageOptions.ReceiveDeferredSequenceNumbers.Add(&sequenceNumber)

&receivedMessage = &MessageBroker.ReceiveMessage(&receiveMessageOptions.ToJson(),&errorMessages,&success)

if not &isOK
    for &errorMessage in &errorMessages
        msg(format(!"%1 (%2)",&errorMessage.Description, &errorMessage.Id), status)
    endfor
endif
&MessageBroker.Dispose()

Variables defined in the samples

   
&MessageBroker MessageBroker, GeneXusMessagingMessageBroker
&receiveMessageOptions ReceiveMessageOptions, AzureServiceBus
&receivedMessage Message
&ConsumeMessageOptions ConsumeMessageOptions
&brokerReceiverOptions BrokerReceiverOptions, AzureServiceBus

See Also

Message settlement with peek lock mode

 

Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant