Skip to content

nikolasmagno/consumidor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Consumidor

A simple library to implements producer-consumer pattern

Overview

It's a library to give a simple way for implement producer and consumer pattern. Allowing the complex away from your code and time to focus in your task. All consumers and producer will run in a separate task, doing concurrency management for you.

Usage

Producer

 class Producer : IProducer<int>
    {
        public Producer()
        {
            Collection = new ConcurrentQueue<int>();
        }

        public ConcurrentQueue<int> Collection { get; set; }

        public void RefreshCollection()
        {
            for (int i = 0; i < 15; i++)
                Collection.Enqueue(i);
        }
    }

Consumer

 class Consumer : IConsumer<int>
    {
        public string Id { get; set; }

        public void Process<T>(T item) => Console.WriteLine($"Consumer {Id} - processing item {item}");
    }

Starting

            var producer = new Producer();
            var consumers = new List<Consumer>()
            {
                new Consumer { Id = "Consumer 1" },
                new Consumer { Id = "Consumer 2" },
                new Consumer { Id = "Consumer 3" },
                new Consumer { Id = "Consumer 4" },
                new Consumer { Id = "Consumer 5" },
            };

            var service = new Service<int>(producer, consumers);            
            service.Start();
            Console.WriteLine("Press something to stop producer");
            Console.ReadLine();
            service.ProducerCancelationTokenSource.Cancel();

            Console.WriteLine("Press something to stop consumers");
            Console.ReadLine();
            service.ConsumerCancelationTokenSource.Cancel();
            Console.WriteLine("Done!");
            Console.ReadKey();

Options

You can define:

  • ConsumerIntervalDelay;
  • ProducerRefreshIntervalDelay;
  • ProducerRefreshTimes;
  • ThrowExceptionOnConsumerError ;
  • ReturnToQueueOnError;

About

Simple implementation of producer and consumer

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages