1
votes

I'm developing two WebJobs for azure: One which will be putting messages in the Service Bus Queue using a topic and another which is subscribed to the ServiceBusTrigger using the same topic.

The messages are sent to the service bus queue correctly but when run the WebJob subscribed to the ServiceBusTrigger those messages are not being processed in FIFO basis.

The code for the WebJob which puts messages in the service bus queue is the following:

NamespaceManager namespaceManager = NamespaceManager.Create();

// Delete if exists
if (namespaceManager.TopicExists("SampleTopic"))
{
    namespaceManager.DeleteTopic("SampleTopic");
}

TopicDescription td = new TopicDescription("SampleTopic");
td.SupportOrdering = true;
TopicDescription myTopic = namespaceManager.CreateTopic(td);

SubscriptionDescription myAuditSubscription = namespaceManager.CreateSubscription(myTopic.Path, "ImporterSubscription");

TopicClient topicClient = TopicClient.Create("SampleTopic");
for(int i = 1; i <= 10; i++)
{
    var message = new BrokeredMessage("message"+i);                   
    topicClient.Send(message);
}
topicClient.Close();

The WebJob which is subscrited to the service bus trigger has the following code:

namespace HO.Importer.Azure.WebJob.TGZProcessor
{
    public class Program
    {
        static void Main(string[] args)
        {
            JobHostConfiguration config = new JobHostConfiguration();
            config.UseServiceBus();
            JobHost host = new JobHost(config);
            host.RunAndBlock();
        }

        public static void WriteLog([ServiceBusTrigger("SampleTopic", "ImporterSubscription")] string message,
            TextWriter logger)
        {                
            Console.WriteLine(message));
        }
    }
}

How can I achieve to process the messages fromo the queue as FIFO?

Thanks in advance!

2

2 Answers

4
votes

While Azure Service Bus provides FIFO feature(Sessions), it is better not to assume this kind of behavior with a broker based queuing system. Ben Morris had a good post Don’t assume message ordering in Azure Service Bus on the fact that assuming ordering with asynchronous messaging is almost a fallacy and reasons for that.

5
votes

Use SessionId or PartitionKey, that will ensure the message is handled by the same message broker.

See: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-partitioning

"SessionId: If a message has the BrokeredMessage.SessionId property set, then Service Bus uses this property as the partition key. This way, all messages that belong to the same session are handled by the same message broker. This enables Service Bus to guarantee message ordering as well as the consistency of session states."