This article contains an example that shows how to send messages to a Message Broker with the Message Broker API, using Azure Service Bus (Topic).
The process 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 send the messages.
3. The SendMessages method returns a boolean result depending on the success of the execution and a variable of GeneXus.Common.Messages to process the errors.
The following code is the one used to connect to an Azure Service Bus Topic:
&brokerReceiverOptions = new() // BrokerReceiverOptions, AzureServiceBus
&brokerReceiverOptions .ReceiveMode = ReceiveModeOptions.ReceiveAndDelete
&MessageBroker = AzureServiceBus.MessageBrokerProvider.Connect(&topicName,&SubscriptionName,&connectionString,&IsSessionEnabled,&brokerReceiverOptions ,&senderIdentifier,&errorMessages,&success)
if not &success
msg(!"Connection failed", status)
for &errorMessage in &errorMessages
msg(format(!"%1 (%2)",&errorMessage.Description, &errorMessage.Id), status)
endfor
endif
The following code is the one used for sending a batch of messages:
&Message = new()
&Message.MessageId = GUID.NewGuid().ToString().Trim()
&Message.MessageBody = !"message body"
&MessageProperty = new()
&MessageProperty.PropertyKey = !"key1"
&MessageProperty.PropertyValue = !"Value1"
&Message.MessageAttributes.Add(&MessageProperty)
&MessageProperty = new()
&MessageProperty.PropertyKey = !"key2"
&MessageProperty.PropertyValue = !"Value2"
&Message.MessageAttributes.Add(&MessageProperty)
&Messages.Add(&message)
---------
//Send//
&success = &MessageBroker.SendMessages(&Messages,"",&errorMessages)
//Process &errorMessages
&MessageBroker.Dispose()