public void QueueTest()
        {
            int    counter = 0;
            string queue   = $"integration.{queue1}";

            //消费
            var consumer = new RabbitConsumer(hosts)
            {
                Password    = password,
                Port        = port,
                UserName    = userName,
                VirtualHost = virtualHost
            };

            consumer.Listen(queue, new ConsumeQueueOptions()
            {
                Arguments  = arguments,
                AutoAck    = false,
                AutoDelete = true,
                Durable    = true,
                FetchCount = 1
            }, result =>
            {
                Output.WriteLine($"{queue}:" + result.Body);
                counter++;
                result.Commit();
            });

            //发布
            var producer = new RabbitProducer(hosts)
            {
                Password    = password,
                Port        = port,
                UserName    = userName,
                VirtualHost = virtualHost
            };

            producer.Publish(queue, "hello queue", new QueueOptions()
            {
                Arguments  = arguments,
                AutoDelete = true,
                Durable    = true
            });

            BlockUntil(() => counter >= 1, 3000);

            producer.Dispose();
            consumer.Dispose();

            Assert.Equal(1, counter);
        }
示例#2
0
        private static void MessageStuff()
        {
            var config = new RabbitConsumerConfig()
            {
                ExchangeBindings = new List <ExchangeBinding>()
                {
                    new ExchangeBinding
                    {
                        RoutingKey   = "billing.account.create",
                        ExchangeName = "Billing"
                    },
                    new ExchangeBinding
                    {
                        RoutingKey   = "billing.account.delete",
                        ExchangeName = "Billing"
                    },
                    new ExchangeBinding
                    {
                        RoutingKey   = "policy.term.created",
                        ExchangeName = "Policy"
                    }
                },
                QueueName = "PrototypeQueue",
                HostName  = "10.0.0.190",
                UserName  = "******",
                Password  = "******",
                Port      = 5672
            };

            var account = new CreateAccount
            {
                AccountName   = "Account Name",
                Amount        = 10.20m,
                CorrelationId = "1234"
            };

            var deleteAccount = new AccountDelete()
            {
                CorrelationId = "1234",
                AccountId     = 2
            };

            var stringValue = JsonConvert.SerializeObject(deleteAccount);

            var factory = new MessageFactory(MessageMap.GetMessageMap());

            UnityContainer container = new UnityContainer();

            container.RegisterType <IMessageHandler <CreateAccount>, CreateAccountHandler>();

            var handler    = new CreateAccountHandler();
            var badHandler = new DeleteAccountHandler();
            var dispatcher = new MessageDispatcher();

            dispatcher.RegisterHandler(handler);
            dispatcher.RegisterHandler(badHandler);

            dispatcher.Register <CreateAccount>((i) =>
            {
                System.Console.WriteLine($"From Action account Name {i.AccountName}");
            });

            var consumer = new RabbitConsumer(config, factory, dispatcher);

            string value = string.Empty;

            while (value != "exit")
            {
                value = System.Console.ReadLine();
            }

            consumer.Dispose();
        }
        public void DirectExchangeTest()
        {
            Dictionary <string, int> dict         = new Dictionary <string, int>(); //接收的消息数量
            Dictionary <string, int> expectedDict = new Dictionary <string, int>(); //发送的消息数量

            string[] queues      = new string[] { $"integration.{queue1}.direct", $"integration.{queue2}.direct" };
            var      routeQueues = queues.Select(f => new RouteQueue()
            {
                Arguments = null,
                Queue     = f,
                Route     = f,
                Options   = new QueueOptions()
                {
                    AutoDelete = true,
                    Durable    = true,
                    Arguments  = arguments
                }
            }).ToArray();

            //消费
            var consumer = new RabbitConsumer(hosts)
            {
                Password    = password,
                Port        = port,
                UserName    = userName,
                VirtualHost = virtualHost
            };

            foreach (var queue in queues)
            {
                dict[queue]         = 0;
                expectedDict[queue] = 0;
                consumer.Listen(direct, queue, new ExchangeConsumeQueueOptions()
                {
                    Arguments   = arguments,
                    AutoAck     = false,
                    AutoDelete  = true,
                    Durable     = true,
                    FetchCount  = 1,
                    RouteQueues = routeQueues,
                    Type        = RabbitExchangeType.Direct
                }, result =>
                {
                    Output.WriteLine($"{queue}:" + result.Body);
                    dict[queue]++;
                    result.Commit();
                });
            }

            //发布
            var producer = new RabbitProducer(hosts)
            {
                Password    = password,
                Port        = port,
                UserName    = userName,
                VirtualHost = virtualHost
            };

            for (var i = 0; i < queues.Length; i++)
            {
                var queue = queues[i];
                expectedDict[queue]++;
                producer.Publish(direct, queue, "direct" + i, new ExchangeQueueOptions()
                {
                    Arguments   = arguments,
                    AutoDelete  = true,
                    Durable     = true,
                    RouteQueues = routeQueues,
                    Type        = RabbitExchangeType.Direct
                });
            }

            BlockUntil(() => dict.Sum(f => f.Value) >= queues.Length, 3000);

            producer.Dispose();
            consumer.Dispose();

            foreach (var queue in queues)
            {
                Output.WriteLine($"{queue}:{expectedDict[queue]}-{dict[queue]}");
                Assert.Equal(expectedDict[queue], dict[queue]);
            }
        }