Created
May 22, 2017 06:00
-
-
Save scott-kloud/6f291a9d7ad1836739d6462efff77b2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Microsoft.ServiceBus" | |
using Microsoft.ServiceBus.Messaging; | |
using System; | |
using System.Threading.Tasks; | |
public static void RetryHandler(BrokeredMessage mySbMsg, Exception ex, TraceWriter log) | |
{ | |
RetryPolicy policy; | |
// Mock out calling service to gather retry policy settings | |
// Business errors policy | |
if (ex.GetType() == typeof(ApplicationException)) | |
{ | |
policy = new RetryPolicy() | |
{ | |
MaxRetryCount = 0, | |
RetryInterval = 0 | |
}; | |
} | |
// Protocol errors policy | |
else if (ex.GetType() == typeof(TimeoutException)) | |
{ | |
policy = new RetryPolicy() | |
{ | |
MaxRetryCount = 5, | |
RetryInterval = 3000 | |
}; | |
} | |
// Unsupported case | |
else | |
{ | |
// Take no action | |
log.Info($"RetryHandler: No policy found. No action taken"); | |
return; | |
} | |
// Check message delivery count against policy | |
if (mySbMsg.DeliveryCount >= policy.MaxRetryCount) | |
{ | |
// Move message to dead letter queue | |
mySbMsg.DeadLetter("Exceeded max retry policy", ex.Message); | |
// Log action taken | |
log.Info($"RetryHandler: Max delivery attempts {mySbMsg.DeliveryCount} exceeded. Message moved to dead letter queue"); | |
} | |
else | |
{ | |
// Log action taken | |
log.Info($"RetryHandler: Requeuing message. Delivery attempts {mySbMsg.DeliveryCount} of {policy.MaxRetryCount}"); | |
// Sleep for the defined interval | |
System.Threading.Thread.Sleep(policy.RetryInterval); | |
// Throw the exception to trigger requeue | |
throw ex; | |
} | |
} | |
public class RetryPolicy | |
{ | |
public int MaxRetryCount { get; set;} | |
public int RetryInterval { get; set;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment