This article contains an example that shows how to consume (complete) a message from Azure Service Bus Queue.
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).
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)
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
To complete the message, use:
If &isOK
&ConsumeMessageOptions = new()
&ConsumeMessageOptions.ConsumeMode = ConsumeModeOptions.Complete
&success = &MessageBroker.ConsumeMessage(&receivedMessage,&ConsumeMessageOptions.ToJson(),&errorMessages)
if &success
msg(!"Message completed successfully",status)
else
for &errorMessage in &errorMessages
msg(format(!"%1 (%2)",&errorMessage.Description, &errorMessage.Id), status)
endfor
endif
endif
&MessageBroker.Dispose()
It may happen that the message cannot be consumed, for example, due to a MessageLockLost exception.
&ConsumeMessaOptions = new()
&ConsumeMessaOptions.ConsumeMode = ConsumeModeOptions.Complete
&success = &MessageBroker.ConsumeMessage(&receivedMessage,&ConsumeMessaOptions.ToJson(),&errorMessages)
if not &success
if &errorMessages.Item(1).Id = !"MessageLockLost"
//Here receive the message again
endif
endif
The ErrorMessage Id contains the Reason of the exception, which is given by the provider. See
ServiceBus failure reason list.
Message settlement with peek lock mode