Skip to main content

More about 'OnMessage' method of Service Bus

If you are using Service Bus, you should know that you have the ability to be notified when a message is available in the queue or in a subscription. Using this feature you don’t need to call the "Receive" method and catch the timeout exception when there are no new messages are available.
OnMessageOptions messageOptions = new OnMessageOptions()
  {
    AutoComplete = true,
    ExceptionReceived += LogErrorsOnMessageReceived
  };
queueClient.OnMessage((message) => 
  {
                ...
  }, messageOptions);
As you can see, you register to an event that will be called when a new message is available. If there are more than 1 messages available the event will be triggered more than one. Each notification will run on a different thread. To be able to control the number of concurrent messages that can be received by a client you should change the value of "MaxConcurrentCalls".
OnMessageOptions messageOptions = new OnMessageOptions()
  {
    AutoComplete = true,
    ExceptionReceived += LogErrorsOnMessageReceived
    MaxConcurrentCalls = 5
  };
queueClient.OnMessage((message) => 
  {
                ...
  }, messageOptions);
But what about the costs?
When using Service Bus, you pay for each transaction. Because of this each call of “Receive” method will consume a transaction. Even if there are no available messages and the call finish with a timeout exception, you will have to pay for one transaction.
Base on this, we should know that a similar thing is happening when we are using “OnMessage”. The behind implementation of this method use “Reveive” method and has a timeout value. Because of this, even if you register to “OnMessage” once you will notify that more than one transactions are consumed even if you don’t have messages in the Service Bus.
This is happening because “OnMessage” calls “Receive” method that will consume a transaction when a message is available or when a timeout exception occurs. The default timeout value is 60 seconds. This timeout value can be easily change using MessagingFactory.
From the cost perceptive you should not have any kind of problems. The cost of each client that use “OnMessage” method and has the timeout value set to 60 seconds is 4.3 cents per month.
In conclusion “OnMessage” is very useful because offer out of the box a mechanism that give us the possibility to be notified when a message is available – until now we had to implement this mechanism every time. You should not forget that this mechanism will handle each new message on a different thread and there are times when you want to control the concurrent level.  Also it generates some costs, but the cost is similar with the “Receive” mechanism – in the end “OnMessage” use “Receive”.

Comments

  1. Do you have any guidance for choosing a value for MaxConcurrentCalls?

    ReplyDelete
  2. A magic number don't exists. Depends on what kind of action you are executing when a new message is available. For example if the action will call the DB that you may be limited of the number of connection that you can have to DB. On the other hand if you have an action that is CPU intensive that you will not be able to run in parallel to many tasks.
    Based on my experience this value should be around 5 or 10.

    ReplyDelete
  3. But how to gracefull shutdown/stop a message pump!?

    ReplyDelete
    Replies
    1. There is now yet a gracefull mechanism. You can cal 'close' method, but odd behavior can appear.

      Delete
  4. whats the default value of MaxConcurrentCalls property?

    ReplyDelete

Post a Comment

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...