Official Content

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


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"

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

//Broker Properties 

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


&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)
    msg(!"sequenceNumber scheduled:" + &sequenceNumber, status)

Canceling the schedule

To cancel the schedule, use:

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

Variables defined in the samples

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



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