Exemple #1
0
        public void TestProduce()
        {
            var conn    = new RabbitMqConnection().GetConnection();
            var channel = conn.CreateModel();

            channel.ExchangeDeclare(_exchangeName, ExchangeType.Direct);
            channel.QueueDeclare(queueName, false, false, false, null);
            channel.QueueDeclare(queueName + 2, false, false, false, null);

            channel.QueueBind(queueName, _exchangeName, routingKey, null);
            channel.QueueBind(queueName + 2, _exchangeName, routingKey, null);

            byte[]           messageBodyBytes = Encoding.UTF8.GetBytes("rabbitmq direct test");
            IBasicProperties properties       = channel.CreateBasicProperties();

            properties.ContentEncoding = "text/plain";

            channel.BasicPublish(_exchangeName, routingKey, properties, messageBodyBytes);
        }
Exemple #2
0
        public void TestConsume()
        {
            //var tcs = new TaskCompletionSource<bool>();
            Console.WriteLine("start received");
            var conn    = new RabbitMqConnection().GetConnection();
            var channel = conn.CreateModel();

            channel.ExchangeDeclare(_exchangeName, ExchangeType.Direct);
            channel.QueueDeclare(queueName, false, false, false, null);
            channel.QueueDeclare(queueName + 2, false, false, false, null);


            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (obj, eventsouce) =>
            {
                var message = Encoding.UTF8.GetString(eventsouce.Body);
                Console.WriteLine($"received {message}");
                // tcs.SetResult(true);
            };
            channel.BasicConsume(queueName, true, consumer);
            channel.BasicConsume(queueName + 2, true, consumer);
            //tcs.Task.Wait(2000);
        }