An Azure Function App that schedules messages to be enqueued in a Azure Service Bus Queue and processes the messages once they are enqueued.
The Azure Function App consists of two functions: ScheduleMessage
and ProcessMessage
.
The ScheduleMessage
function has an HTTP
trigger that takes a body and an interval
query param.
It creates a ServiceBusMessage
out of the body and sets message.ScheduledEnqueueTime
to interval
minutes from now before publishing it to a queue.
The ProcessMessage
function has a ServiceBus
trigger on the queue ScheduleMessage
publishes to, so once the message is enqueued, it pulls the messages off and logs it.
Where ProcessMessage
logs the message would be where additional logic that needed to occur at the scheduled time would be implemented.
- Create a Service Bus Namespace and a Queue in the Azure Portal.
- Create an entry in the
local.settings.json
file with a key ofAzureWebJobsServiceBus
and a value of the Service Bus Namespace connection string. - Paste the name of the Service Bus Queue where
<queue-name>
is used in theScheduleMessage.cs
andProcessMessage.cs
files. - Build and run the app using Visual Studio or Docker.
- Send a HTTP request to the
ScheduleMessage
function with a body to send as theServiceBusMessage
and aninterval
query parameter.
TheServiceBusMessage
will be scheduled forinterval
minutes from now.
The body can contain any properties. For example:
curl --location 'http://localhost:7071/api/ScheduleMessage?interval=1' \
--header 'Content-Type: application/json' \
--data '{
"message": "Service Bus Scheduler"
}'
- Once the interval has passed and the message is enqueued, the
ProcessMessage
function will pick the message off of the queue and log the key/value pairs of the object sent as the body.