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
        /// <summary>
        /// 返回保存发布者
        /// </summary>
        /// <param name="rabbitProducer"></param>
        public void ReturnProducer(RabbitProducer rabbitProducer)
        {
            if (Disposed)
            {
                return;
            }

            lock (rabbitProducers)
            {
                if (rabbitProducers.Count < InitializeCount && rabbitProducer != null && rabbitProducer.IsOpen)
                {
                    rabbitProducers.Enqueue(rabbitProducer);
                }
                else
                {
                    rabbitProducer?.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]);
            }
        }