Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var factory = BusBuilder.GetConnectionFactory();

            using (var conn = factory.CreateConnection())
            {
                using (var channel = conn.CreateModel())
                {
                    channel.QueueDeclare("hello", false, false, false, null);
                    var replyQueueName = channel.QueueDeclare().QueueName;
                    var consumer       = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(queue: replyQueueName,
                                         noAck: true,
                                         consumer: consumer);
                    var corrId     = Guid.NewGuid().ToString();
                    var properties = channel.CreateBasicProperties();
                    properties.DeliveryMode  = 2;
                    properties.ReplyTo       = replyQueueName;
                    properties.CorrelationId = corrId;
                    string msg = "";
                    do
                    {
                        msg = Console.ReadLine();
                        var body = Encoding.UTF8.GetBytes(msg);
                        channel.BasicPublish("", "hello", properties, body);
                        Console.WriteLine(" has send: {0}", msg);

                        while (true)
                        {
                            var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                            if (ea.BasicProperties.CorrelationId == corrId)
                            {
                                string retrunBody = Encoding.UTF8.GetString(ea.Body);
                                Console.WriteLine(retrunBody);
                                break;
                            }
                        }
                    } while (msg != "0");
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var factory = BusBuilder.GetConnectionFactory();

            using (var conn = factory.CreateConnection())
            {
                using (var channel = conn.CreateModel())
                {
                    channel.QueueDeclare("hello", false, false, false, null);
                    channel.BasicQos(0, 1, false);
                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume("hello", false, consumer);
                    Console.WriteLine(" waiting for message.");
                    while (true)
                    {
                        string response   = null;
                        var    ea         = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                        var    body       = ea.Body;
                        var    props      = ea.BasicProperties;
                        var    replyProps = channel.CreateBasicProperties();
                        replyProps.CorrelationId = props.CorrelationId;
                        var message = Encoding.UTF8.GetString(body);


                        int dots = message.Split('.').Length - 1;
                        Thread.Sleep(dots * 1000);

                        Console.WriteLine("Received {0}", message);

                        response = message + " has received!";
                        var responseBytes = Encoding.UTF8.GetBytes(response);
                        channel.BasicPublish(exchange: "",
                                             routingKey: props.ReplyTo,
                                             basicProperties: replyProps,
                                             body: responseBytes);
                        channel.BasicAck(ea.DeliveryTag, false);
                    }
                }
            }
        }