Stefano Ricciardi
Stefano Ricciardi

Reputation: 2973

Producer and consumer with notifications in C#

I have a producer (P) which pushes items into a consumer (C). C may decide not to consume all the items as soon as they arrive; instead it may asynchronously wait until there are n items in the queue or until a certain interval of time t has elapsed and then consume all items in the queue and restart the timer.

P needs to be notified whenever an item it has pushed to C has indeed been consumed. This is because P is in charge of recovery in case of a failure (P persists the current state to a DB). So, in case of a restart, P needs to know which items it needs to push again because they have not been acknowledged by the consumer.

My first idea was to have C notify P through a callback function (delegate): whenever an item is consumed, P's callback function is invoked with a list of consumed items.

But I am wondering whether there might be other (better) ways of notifying the producer. What are your thoughts?

Stefano

UPDATE: thank you for your answers so far. I am currently investigating whether using asynchronous methods might be an elegant way to synchronize the publisher and the consumer on the status of the items being published.

Upvotes: 3

Views: 1040

Answers (5)

Stefano Ricciardi
Stefano Ricciardi

Reputation: 2973

I have ended up implementing an Asynchronous Design (that is, creating a BeginXXX and EndXXX pair of methods at the producer/consumer interface), since I believe this is more idiomatic for the .NET development (I expect any future maintainer of this code to be familiar with it).

When the consumer has done handling the item, it invokes the callback as per the pattern; The client can either synchornize on the callback or use the other interaction patterns via the IAsyncResult (see MSDN documentation).

Upvotes: 0

Felice Pollano
Felice Pollano

Reputation: 33252

I think you can achieve what you need without having C notifying P, but using a Transactional Queue to feed C. Just an example link talking about the MSMQ implementation:

http://msdn.microsoft.com/en-us/library/ms978430.aspx, or if MSMQ is not available a pure BCL solutions can be:

https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/branches/rhino-queues-1.0/ ( or at least just to grab some idea from )

EDIT I changed the link to a more official one about the MSMQ solution.

Upvotes: 4

PVitt
PVitt

Reputation: 11760

I would extend the item to throw an CosumedEvent. P registers for this event. When C consumes the item, P is notified and just has to detach the event handler plus the operations it has to do when the item has been consumed.

Upvotes: 1

Kieren Johnstone
Kieren Johnstone

Reputation: 42003

Personally not aware of anything too fantastic - you could use an event, or the producer might implement an INotifyProcessComplete of your design that you pass to the consumer, or you could use an event (e.g. ManualResetEvent) which the consumer sets/flags when it's finished.

Upvotes: 1

Emond
Emond

Reputation: 50682

A delegate is fine although I'd use an event to implement it.

Upvotes: 1

Related Questions