HowTo: Send messages to a Message Broker

Official Content
This documentation is valid for:

With the Message Broker API, you can send messages to a Message Broker.

This article contains an example that shows how to do so using Azure Service Bus (Topic).

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 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.

Connect to 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

Send 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()

Variables defined in the example

   
&brokerReceiverOptions BrokerReceiverOptions, AzureServiceBus
&MessageBroker MessageBroker, GeneXusMessagingMessageBroker
&MessageProperty MessageProperty, GeneXusMessagingMessageBroker