Official Content

This article contains an example that shows how to schedule a message in Azure Service Bus. 

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 schedule the message.

3. Use the ScheduleMessage method, passing to it ScheduleMessageOptions. The method returns the message SequenceNumber, to be able to cancel it later.

Connecting to Azure Service Bus Queue

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

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

Send a message to be scheduled

The following code is the one used for sending a message to be scheduled:

&Message = new()
&Message.MessageId = GUID.NewGuid().ToString()
&Message.MessageBody = !"message body"

&MessageProperty = new()
&MessageProperty.PropertyKey = !"propKey1"
&MessageProperty.PropertyValue = !"propValue1"
&Message.MessageAttributes.Add(&MessageProperty)

&MessageProperty = new()
&MessageProperty.PropertyKey = !"propKey2"
&MessageProperty.PropertyValue = !"propValue2"
&Message.MessageAttributes.Add(&MessageProperty)

//Broker Properties 

&MessageProperty = new()
&MessageProperty.PropertyKey = !"subject"
&MessageProperty.PropertyValue = !"Subject of message"
&Message.MessageAttributes.Add(&MessageProperty)

//SEND

&datetime = Datetime.Now() 
&ScheduleMessageOptions.ScheduledEnqueueTime = &datetime.AddMinutes(60)

&sequenceNumber = &MessageBroker.ScheduleMessage(&Message,&ScheduleMessageOptions.ToJson(),&errorMessages)

if &sequenceNumber = 0
    for &errorMessage in &errorMessages
        msg(format(!"%1 (%2)",&errorMessage.Description, &errorMessage.Id), status)
    endfor
else 
    msg(!"sequenceNumber scheduled:" + &sequenceNumber, status)
endif

Canceling the schedule

To cancel the schedule, use:

&isOK= &MessageBroker.CancelSchedule(&sequenceNumber,&errorMessages)
//Process &errorMessages 
&MessageBroker.Dispose()

Variables defined in the samples

   
&MessageBroker MessageBroker, GeneXusMessagingMessageBroker
&ScheduleMessageOptions ScheduleMessageOptions
&Message Message
&MessageProperty MessageProperty
&sequenceNumber Numeric(18)

 

 

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