Beispiel #1
0
        static void Main2(string[] args)
        {
            var sbLogFactory = new StringBuilderLogFactory();
            LogManager.LogFactory = sbLogFactory;
            var log = LogManager.GetLogger(typeof(Program));

            var clientManager = new PooledRedisClientManager(new[] { "localhost" })
            {
                PoolTimeout = 1000,
            };

            var mqHost = new RedisMqServer(clientManager, retryCount: 2);

            var msgsProcessed = 0;
            var sum = 0;
            mqHost.RegisterHandler<Incr>(c =>
            {
                var dto = c.GetBody();
                sum += dto.Value;
                log.InfoFormat("Received {0}, sum: {1}", dto.Value, sum);
                msgsProcessed++;
                return null;
            });

            mqHost.Start();

            10.Times(i =>
            {
                ThreadPool.QueueUserWorkItem(x =>
                {
                    using (var client = mqHost.CreateMessageQueueClient())
                    {
                        try
                        {
                            log.InfoFormat("Publish: {0}...", i);
                            client.Publish(new Incr { Value = i });
                        }
                        catch (Exception ex)
                        {
                            log.InfoFormat("Start Publish exception: {0}", ex.Message);
                            clientManager.GetClientPoolActiveStates().PrintDump();
                            clientManager.GetReadOnlyClientPoolActiveStates().PrintDump();
                        }
                        Thread.Sleep(10);
                    }
                });
            });

            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var client = (RedisClient)clientManager.GetClient())
                {
                    client.SetConfig("timeout", "1");
                    var clientAddrs = client.GetClientList().ConvertAll(x => x["addr"]);
                    log.InfoFormat("Killing clients: {0}...", clientAddrs.Dump());

                    try
                    {
                        clientAddrs.ForEach(client.ClientKill);
                    }
                    catch (Exception ex)
                    {
                        log.InfoFormat("Client exception: {0}", ex.Message);
                    }
                }
            });

            20.Times(i =>
            {
                using (var client = mqHost.CreateMessageQueueClient())
                {
                    try
                    {
                        log.InfoFormat("Publish: {0}...", i);
                        client.Publish(new Incr { Value = i });
                    }
                    catch (Exception ex)
                    {
                        log.InfoFormat("Publish exception: {0}", ex.Message);
                        clientManager.GetClientPoolActiveStates().PrintDump();
                        clientManager.GetReadOnlyClientPoolActiveStates().PrintDump();
                    }
                }

                Thread.Sleep(1000);
            });

            Thread.Sleep(2000);
            "Messages processed: {0}".Print(msgsProcessed);
            "Logs: ".Print();
            sbLogFactory.GetLogs().Print();
            Console.ReadKey();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var clientManager = new PooledRedisClientManager(new[] { "localhost" })
            {
                PoolTimeout = 1000,
            };
            using (var client = clientManager.GetClient())
            {
                client.FlushAll();
            }

            var mqHost = new RedisMqServer(clientManager);

            var msgsProcessed = 0;
            var msgsQueued = 0;
            var sum = 0;
            mqHost.RegisterHandler<Incr>(c =>
            {
                var dto = c.GetBody();
                sum += dto.Value;
                Console.WriteLine("Received {0}, sum: {1}", dto.Value, sum);
                msgsProcessed++;
                return null;
            });

            mqHost.Start();
            var processes = Process.GetProcessesByName("redis-server");
            var timer = new Timer(s =>
            {
                using (var client = mqHost.MessageFactory.CreateMessageProducer())
                {
                    try
                    {
                        client.Publish(new Incr { Value = 1 });
                        msgsQueued++;
                        Console.WriteLine("Message #{0} published.", msgsQueued);
                    }
                    catch { }
                }
            }, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));

            Thread.Sleep(5000);
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            Thread.Sleep(1000);

            int msgsQueuedBeforeKill = msgsQueued;
            int msgsProcessedBeforeKill = msgsProcessed;
            processes[0].Kill();

            timer.Change(TimeSpan.Zero, TimeSpan.FromSeconds(1));
            Thread.Sleep(15000);
            timer.Dispose();

            Thread.Sleep(1000);

            mqHost.GetStats().PrintDump();
            mqHost.GetStatus().Print();

            "Messages queued before kill: {0}".Print(msgsQueuedBeforeKill);
            "Messages processed before kill: {0}".Print(msgsProcessedBeforeKill);

            "Messages queued: {0}".Print(msgsQueued);
            "Messages processed: {0}".Print(msgsProcessed);

            Console.ReadKey();
        }