BlockingCollection Exception Handling

I have a lock BlockingCollection

that I am using to process the queue (i.e. send email).

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 start a separate thread where I am waiting for an item in the queue (there is a call somewhere Add()

). Then I process this element. This function can throw an exception: if we are talking about emails, for example, it might crash Send()

.

I would like to handle this situation in two different ways, depending on the specific application:

  • repeat the process function with the same element until it exits without any exceptions.
  • put this element at the end of the queue to try again later, but now process the rest of the elements.

Is there a better approach that handles these behaviors natively?

+3


source to share


1 answer


Both approaches have their drawbacks:

  • The first approach can pause the queue indefinitely if the item has persistent delivery issues.
  • The second approach will keep the item with persistent delivery problems in the queue indefinitely


The best approach would be to create a separate blocking collection for the elements that failed, insert them into this collection in an exception along with the exception object, and forget about them. A separate thread will dequeue these items, analyze the exception, and decide what to do next. It can schedule re-dispatch at the appropriate time, push the item back if its number of failures is under some fixed number, send a message for analysis by administrators, or simply discard it depending on the needs of your system.

+1


source







All Articles