Last month I had some time to play around and I did small project which implemented Azure Storage Queues in Business Central.

What are Azure Storage Queues?

They are service in Azure which allows to store some message in queue over HTTP and then poll these messages in some message receiver.

Why are they useful?

They are useful in some integration scenarios where you have large amounts of incoming data and you cannot process them in real time.

Take for example shop floor in manufacturing. There might be some machines reporting output every few seconds and do it for hours. When you post them directly to business central then you cannot guarantee that all the data is received in Business Central because there might not be enough processing power, outages etc.

You can then use queues to store order no and quantity in the message queue and then process these queues in Business Central and post them one by one.

All the code is visible here:

https://github.com/nocubicles/AzureStorageQueues

Basic working example:

codeunit 50101 "Manage Queues"
{

    procedure ProcessMessagesFromQueue(Queue: Text)
    begin
        if Process(Queue) then begin
            Sleep(2000);
            ProcessMessagesFromQueue(Queue);
        end;
    end;

    local procedure Process(Queue: Text): Boolean
    var
        AzureStorageQueueSdk: Codeunit AzureStorageQueuesSdk;
        MessageBody: Text;
        MessageId: Text;
        MessageText: Text;
        MessagePopreceipt: Text;
        ImportantTestTable: Record ImportantTestTable;
    begin
        MessageBody := AzureStorageQueueSdk.GetNextMessageFromQueue(Queue);
        MessageId := AzureStorageQueueSdk.GetMessageIdFromXmlText(MessageBody);
        MessageText := AzureStorageQueueSdk.GetMessageTextFromXmlText(MessageBody);
        MessagePopreceipt := AzureStorageQueueSdk.GetMessagePopReceiptFromXmlText(MessageBody);

        if (MessageId <> '') AND (MessageText <> '') then begin
            ImportantTestTable.Init();
            ImportantTestTable."Entry No." := ImportantTestTable.GetNextEntryNo();
            ImportantTestTable.Message := MessageText;
            if ImportantTestTable.Insert() then begin
                //important here to check if delete is succesful and then commit. Otherwise we end in loop where messages are reappearing
                if AzureStorageQueueSdk.DeleteMessageFromQueue(Queue, MessageId, MessagePopreceipt) then
                    Commit();
                Process(Queue);
            end;
        end;
        exit(true);
    end;

    procedure GenerateMessagesToQueue(Queue: Text; NumberOfMessages: Integer)
    var
        AzureStorageSdk: Codeunit AzureStorageQueuesSdk;
        Counter: Integer;
    begin
        for Counter := 0 to NumberOfMessages do begin
            AzureStorageSdk.PostMessageToQueue(Queue, 'This is important business data that needs queue: ' + format(Counter))
        end;
    end;
}

This example is just proof of concept and it implements recursive function that polls the message queue in Azure and processes all the messages in the queue.