I have a blocking BlockingCollection that I use to process a queue (i.e. sending emails).
BlockingCollection<ItemQueue> queue = new BlockingCollection<ItemQueue>();
Thread threadQueue = new Thread(ProcessQueue);
threadQueue.Start();
private void ProcessQueue()
{
while (true)
{
var item = queue.Take();
process(item);
}
}
public void process(ItemQueue item)
{
try
{
// do something
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Basically I run a separate thread where I wait for an element in the queue (somewhere there is an Add() call). Then I process that item.
This function might raise an exception: if we're talking about emails, for example, the Send() may fails.
I would like to handle this situation in two different ways, depending on the specific application:
Is there a better approach that handles these behaviors natively?
Both approaches have their shortcomings:
A better approach would establish a separate blocking collection for items that failed, push them into that collection on exception, along with the exception object, and forget about them. A separate thread would take these items off the queue, analyze the exception, and decide what to do next. It could schedule re-sending at an appropriate time, push the item back if its failure count is under some fixed number, file the message for analysis by administrators, or simply discard it, depending on the needs of your system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With