public void Receive()
        {
            try
            {
                var factory = new ConnectionRabbitMQ();

                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare(
                            queue: "messages",
                            durable: false,
                            exclusive: false,
                            autoDelete: false,
                            arguments: null
                            );

                        var consumer = new EventingBasicConsumer(channel);

                        consumer.Received += (model, ea) =>
                        {
                            var body        = ea.Body;
                            var jsonMessage = Encoding.UTF8.GetString(body);
                            //var message = ObjectConverter.getObject<MessageViewModel>(body);

                            var message = JsonConvert.DeserializeObject <MessageViewModel>(jsonMessage);

                            message.Content = GetStockPriceMessage(message.Content);

                            Debug.WriteLine("\n\n[x] Mensaje recibido: " + message.Content + "\n");
                        };

                        channel.BasicConsume(
                            queue: "messages",
                            autoAck: true,
                            consumer: consumer
                            );

                        Debug.WriteLine("\n\nServicio Receiver iniciado correctamente.\n");
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\nSe detuvo el servicio Receiver debido a lo siguiente: " + ex.Message + "\n");
            }
        }
示例#2
0
        public bool SendMessage(MessageViewModel message)
        {
            try
            {
                var factory = new ConnectionRabbitMQ();

                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare(
                            queue: "messages",
                            durable: false,
                            exclusive: false,
                            autoDelete: false,
                            arguments: null);

                        var jsonMessage = JsonConvert.SerializeObject(message);

                        var body = Encoding.UTF8.GetBytes(jsonMessage);
                        //var body = ObjectConverter.getBytes<MessageViewModel>(message);

                        if (body == null)
                        {
                            throw new Exception("El objeto no pudo ser convertido a bytes");
                        }

                        channel.BasicPublish(
                            exchange: "",
                            routingKey: "messages",
                            basicProperties: null,
                            body: body
                            );

                        Debug.WriteLine("\nMensaje enviado: " + jsonMessage + "\n");
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\nNo se pudo enviar el mensaje \"" + message.Content + "\" debido a la siguiente excepción: " + ex.Message + "\n");
                return(false);
            }

            return(true);
        }