public void CanProcessByAddingProcessor()
        {
            // Add the named processor "my_handler", w/ the ProcessString lamda, and handle 5 items per dequeue.
            Queues.AddProcessor("my_handler", new QueueProcessor <string>(5, items => ProcessString(items, false, "my_handler")));
            Queues.Enqueue <string>("my_handler", "kdog");
            Queues.Enqueue <string>("my_handler", new List <string>()
            {
                "kishore", "reddy"
            });
            Queues.Process("my_handler");

            Assert.IsTrue(Queues.IsIdle("my_handler"));
        }
        /// <summary>
        /// Run the application.
        /// </summary>
        public override BoolMessageItem Execute()
        {
            // NOTE:
            // 1. Just showing the API for queue processing using lamdas as delegates.
            // 2. Typically the Queues.Process method will be called by a scheduler to process something periodically.

            // 1. Add queue processing handler for strings WITH OUT specifying a name.
            //    By default, the items in teh queues are processed 5 at a time on each call to Process.
            Queues.AddProcessorFor <string>(items => items.ForEach(item => Console.WriteLine(item)));
            Queues.Enqueue <string>(new List <string>()
            {
                "a", "b", "c", "d", "e", "1", "2", "3", "4", "5"
            });

            // 1st call only prints a-e
            // 2nd call prints the remainder 1-5
            Queues.Process <string>();
            Queues.Process <string>();
            Console.WriteLine();

            // 2. Add queue processing by specifying the handler name.
            //    This is ideal if you have 2 handlers for the same type.
            Queues.AddProcessorFor <int>("my_handler", items => items.ForEach(i => Console.WriteLine(i)));
            Queues.Enqueue <int>("my_handler", new List <int>()
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9
            });
            Queues.Process("my_handler");
            Console.WriteLine();

            // 3. Add queue processing custom type.
            Queues.AddProcessorFor <Quote>(items => PrintQuotes(items));
            Queues.Enqueue <Quote>(new List <Quote>()
            {
                new Quote("MSFT", 20), new Quote("GOOG", 300), new Quote("CITI", 0),
                new Quote("AIG", -1), new Quote("HONDA", 80), new Quote("BankOfAmerica", 30),
                new Quote("TOYO", 20), new Quote("CS", 32), new Quote("GS", -1)
            });
            Queues.Process <Quote>();

            // 4. Add queue processing by specifying the queue processor, custom name("my_quote_queue"), custom type(Quote), custom dequeue size(2).
            Queues.AddProcessor("my_quote_queue", new QueueProcessor <Quote>(2, items => PrintQuotes(items)));
            Queues.Enqueue <Quote>("my_quote_queue", new List <Quote>()
            {
                new Quote("MSFT", 20), new Quote("GOOG", 300), new Quote("CITI", 0),
                new Quote("AIG", -1), new Quote("HONDA", 80), new Quote("BankOfAmerica", 30),
                new Quote("TOYO", 20), new Quote("CS", 32), new Quote("GS", -1)
            });
            Queues.Process("my_quote_queue");
            return(BoolMessageItem.True);
        }
Exemplo n.º 3
0
        public void CanGetQueueStates()
        {
            // Easiest way to add named processor for a specific type.
            // Defaults to 5 items per dequeue.
            Queues.AddProcessorFor <string>(items => ProcessString(items, true, null));
            Queues.AddProcessor("tags_queue",
                                new QueueProcessor <string>(2, items => items.ForEach(item => Console.WriteLine(item))));

            Queues.Enqueue <string>("tags_queue", new List <string>()
            {
                "python", "ruby", "erlang", "closure", "scala"
            });
            Queues.Enqueue <string>(new List <string>()
            {
                "kishore", "reddy"
            });

            Queues.Process <string>();
            Queues.Process("tags_queue");
            Queues.Process("tags_queue");

            var states = Queues.GetMetaInfo();
            var map    = new Dictionary <string, QueueStatus>();

            foreach (var state in states)
            {
                map[state.Name] = state;
            }

            Assert.AreEqual(states.Count, 2);
            Assert.AreEqual(map["System.String"].Count, 0);
            Assert.AreEqual(map["System.String"].DequeueSize, 10);
            Assert.AreEqual(map["System.String"].NumberOfTimesProcessed, 1);
            Assert.AreEqual(map["System.String"].State, QueueProcessState.Idle);
            Assert.AreEqual(map["tags_queue"].Count, 1);
            Assert.AreEqual(map["tags_queue"].DequeueSize, 2);
            Assert.AreEqual(map["tags_queue"].NumberOfTimesProcessed, 2);
            Assert.AreEqual(map["tags_queue"].State, QueueProcessState.Idle);
        }