示例#1
0
        public void Test1()
        {
            string inQ = "in", outQ = "out";

            var steps = new List <PipelineStep>()
            {
                new PipelineAction(item => { item.Content = (int)item.Content + 5; item.Status = ItemStatus.OnGoing; return(item); }, inQ, "has5", 2),
                new PipelineRouter(item => ((int)item.Content % 2 == 0?"pair":"impair"), "has5", new[] { "pair", "impair" }, 2),
                new PipelineAction(item => { Assert.AreEqual(0, (int)item.Content % 2); item.Status = ItemStatus.Complete; return(item); }, "pair", outQ, 2),
                new PipelineAction(item => { Assert.AreEqual(1, (int)item.Content % 2); item.Status = ItemStatus.Complete; return(item); }, "impair", outQ, 2)
            };
            var outP   = new PipelineQueue(outQ);
            var queues = new List <PipelineQueue>()
            {
                new PipelineQueue(inQ),
                outP,
                new PipelineQueue("has5"),
                new PipelineQueue("pair"),
                new PipelineQueue("impair")
            };

            Task.Run(() => {
                foreach (var o in outP.GetConsumingEnumerable())
                {
                    Console.WriteLine(string.Concat(o.Treatment.Message));
                }
            });


            var p = PipelineFactory.CreatePipeline(
                inQ,
                outQ,
                steps,
                queues,
                item => {
                Console.WriteLine(string.Concat(item.Treatment.Message));
                return(true);
            }
                );

            p.Execute(new item()
            {
                Content = 5
            });
        }
示例#2
0
        public static Task[] CreatePump <TIn, TOut>(
            this PipelineQueue <TIn> queue, int threadCount,
            Func <IPipelineTask <TIn, TOut> > pipeFactory,
            Action <TOut> action)
        {
            var tasks = new Task[threadCount];

            for (var i = 0; i < threadCount; i++)
            {
                tasks[i] = Task.Run(async() =>
                {
                    var p = pipeFactory();
                    await p.Start();
                    await foreach (var item in queue.GetConsumingEnumerable())
                    {
                        var result = await p.Execute(item);
                        action?.Invoke(result);
                    }
                    await p.End();
                });
                Console.WriteLine($"Completed Pump {queue}::{i}");
            }
            return(tasks);
        }