In this article we will revisit the first sample about one way messaging and look at how the AppFx.ServiceBus framework implements error handling and retries. In this article we want to achieve the following:
- We will take the code from sample 1
- We will modify the message handler to throw errors.
- We will see that the message will be retried
- We will see that eventually the message will be processed successfully
Getting the Sample
The sample is available in the source code section on codeplex:
http://appfxservicebus.codeplex.com/releases
In the source code and samples download you will see a samples folder and you want the folder called Sample4-OneWayErrorHandling.
Im not going to go into the full details of the sample like we did in previous samples so I will assume you are familiar with sample 1 on the following link:
http://geekswithblogs.net/michaelstephenson/archive/2013/02/03/152012.aspx
In the rest of the article we will talk about how that sample has been modified.
Azure Setup
The setup of queues and topics to support this sample is exactly the same as for sample 1 and in fact it uses the same queues.
Modifying the message handler
In the message handler we are going to extend the logic. We will use the some context information from the MessageProcessorContext class. In that class there is an instance for each processed message which gives the message handler developer access to the Azure Service Bus Message and other things in an encapsulated way. From that class in our message handler we are interested in the MessageProcessorContext.Current.DeliveryCount property. This property tells us how many times the message has been attempted to be received from the Service Bus queue or subscription. In the error handler we will look to throw an error if the delivery count is less than three. The framework will reject this message and Azure Service Bus will make it available for collection again. The framework will then again try to process the message. In our logic the message handler will allow the message to be successfully processed once the delivery count is 3 or greater.
See below for an example of the message handler.
Running the sample
Rather than talking through the whole running of the sample, if you remember back to sample 1 you need to do the following:
- Compile the visual studio solution
- Start the AppFx.ServiceBus.Hosts.Console.exe application in the library folder
- Start the windows test application
- Click the button
When the button is clicked within a second or so your message will be received by the console application and you will see that in the red text indicates that the message was received and an error occurred. The message was rejected. This happens twice and then on the third attempt the message is processed successfully.
Other Considerations
Below are some other considerations you may want to think about when handling and throwing errors.
Azure Max Delivery Attempts
One thing to consider is that in the configuration or your Azure queue or subscription you can set a value for the maximum attempts for redelivery. This setting still comes into play when using the AppFx.ServiceBus framework. If the framework rejects a message 4 times and the Azure Queue max deliveries is set to 4 then Azure Service Bus will send the message to the Dead Letter queue.
AppFx Exception
In the framework there is an exception called AppFx.ServiceBus.Exceptions.ApplicationException. This exception allows you to throw an error in your message handler and to have the framework use the properties on the exception to allow you to control if a retry is attempted or not.
If an exception is thrown which is not the above type of exception then the framework will implement the behavior to keep log the error and keep retrying the message until the Azure Service Bus max delivery attempts kicks in and dead letters the message.
Summary
This sample should show you how the AppFx.ServiceBus framework deals with the retry and error handling scenarios you are most likely to need. This is one of the key areas we feel that AppFx.ServiceBus really makes life simpler for you.