Beispiel #1
0
        public static void NoticeManagerLargeNumberDemo()
        {
            Console.WriteLine("Press enter to start");
            Console.ReadLine();

            StringBuilder sbCompletedInfo1 = new StringBuilder();
            StringBuilder sbCompletedInfo2 = new StringBuilder();

            var count1 = 0;
            var count2 = 0;

            var manager = new NoticeManager <string>();

            var pre1 = 0;

            manager.AddSubscribe("Subscriber1", new Action <string>(message =>
            {
                var val = Convert.ToInt32(message.Split(' ')[1]);
                if (val > 0 && val - pre1 != 1)
                {
                    throw new Exception("Error");
                }
                pre1 = val;

                if (message == "Message 999999")
                {
                    throw new Exception("AAA");
                }

                count1++;
                Console.WriteLine($"Subscriber1 recieve notice: {message}");
            }), new Action <string, Exception>((message, ex) =>
            {
                Console.WriteLine("----------------------------------------------------------");
                Console.WriteLine($"Subscriber1 Message={message}, Exception={ex.Message}");
                Console.WriteLine("----------------------------------------------------------");
            }));

            var pre2 = 0;

            manager.AddSubscribe("Subscriber2", new Action <string>(message =>
            {
                var val = Convert.ToInt32(message.Split(' ')[1]);
                if (val > 0 && val - pre2 != 1)
                {
                    throw new Exception("Error");
                }
                pre2 = val;

                count2++;
                Console.WriteLine($"Subscriber2 recieve notice: {message}");
            }), new Action <string, Exception>((message, ex) =>
            {
                Console.WriteLine("----------------------------------------------------------");
                Console.WriteLine($"Subscriber2 Message={message}, Exception={ex.Message}");
                Console.WriteLine("----------------------------------------------------------");
            }));

            manager.StartSubscribeNotice();

            for (var i = 0; i < 1000000; i++)
            {
                manager.Publish($"Message {i}");
            }

            for (var i = 1000000; i < 1000020; i++)
            {
                Thread.Sleep(500);
                manager.Publish($"Message {i}");
            }

            Console.ReadLine();

            Console.WriteLine("Stop Subscribe");

            manager.StopSubscribeNotice().ContinueWith(task =>
            {
                Console.WriteLine($"Subscriber1 count={count1}");
                Console.WriteLine($"Subscriber2 count={count2}");
                Console.WriteLine($"Status={manager.Status}");
            });
        }
Beispiel #2
0
        public static void NoticeManagerDefaultStackDemo()
        {
            StringBuilder sbCompletedInfo1 = new StringBuilder();
            StringBuilder sbCompletedInfo2 = new StringBuilder();

            var count1 = 0;
            var count2 = 0;

            var manager = new NoticeManager <string>(null, new NoticeStack <string>());

            var pre1 = 1019;

            manager.AddSubscribe("Subscriber1", new Action <string>(message =>
            {
                var val = Convert.ToInt32(message.Split(' ')[1]);
                if (val < 1019 && pre1 - val != 1)
                {
                    throw new Exception("Error");
                }
                pre1 = val;

                if (message == "Message 100")
                {
                    throw new Exception("AAA");
                }

                count1++;
                Console.WriteLine($"Subscriber1 recieve notice: {message}");
            }), new Action <string, Exception>((message, ex) =>
            {
                Console.WriteLine("----------------------------------------------------------");
                Console.WriteLine($"Subscriber1 Message={message}, Exception={ex.Message}");
                Console.WriteLine("----------------------------------------------------------");
            }));

            var pre2 = 1019;

            manager.AddSubscribe("Subscriber2", new Action <string>(message =>
            {
                var val = Convert.ToInt32(message.Split(' ')[1]);
                if (val < 1019 && pre2 - val != 1)
                {
                    throw new Exception("Error");
                }
                pre2 = val;

                count2++;
                Console.WriteLine($"Subscriber2 recieve notice: {message}");
            }), new Action <string, Exception>((message, ex) =>
            {
                Console.WriteLine("----------------------------------------------------------");
                Console.WriteLine($"Subscriber2 Message={message}, Exception={ex.Message}");
                Console.WriteLine("----------------------------------------------------------");
            }));

            for (var i = 0; i < 1020; i++)
            {
                //if (i == 500)
                //{
                //    try
                //    {
                //        manager.RemoveSubscribe("Subscriber1");
                //    }
                //    catch (Exception ex)
                //    {
                //        Console.WriteLine(ex.Message);
                //    }
                //}
                manager.Publish($"Message {i}");
            }

            manager.StartSubscribeNotice();

            Console.ReadLine();

            Console.WriteLine("Stop Subscribe");

            manager.StopSubscribeNotice().ContinueWith(task =>
            {
                Console.WriteLine($"Subscriber1 count={count1}");
                Console.WriteLine($"Subscriber2 count={count2}");
                Console.WriteLine($"Status={manager.Status}");
            });
        }