Пример #1
0
        public IActionResult Post(UserBehavior userBehavior)
        {
            #region Opens the channel and connection to RabbitMQ server
            ConnectionRabbitMQ connectionRabbitMQ = new ConnectionRabbitMQ();
            connectionRabbitMQ.GetConnection();
            connectionRabbitMQ.routingKey = "UserBehaviorQueue";
            connectionRabbitMQ.OpenChannel();
            #endregion

            #region Sets the visitor IP
            userBehavior.ip = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            #endregion

            #region Serializes the entity UserBehavior to JSon format
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            var jsonUserBehavior = javaScriptSerializer.Serialize(userBehavior);
            #endregion

            #region Publishes the message on RabbitMQ
            connectionRabbitMQ.PublishMessage(jsonUserBehavior);
            #endregion

            #region Closes the connection to RabbitMQ
            connectionRabbitMQ.CloseChannel();
            #endregion

            return(Ok("User Behavior has been sent."));
        }
Пример #2
0
    public MessageRecover RecoverMessage(MessageRecover msgRecover)
    {
        try
        {
            #region Validates message publish object
            if (msgRecover == null || string.IsNullOrWhiteSpace(msgRecover.QueueName.Trim()))
            {
                throw new ArgumentNullException("Argumento inválido!");
            }
            #endregion

            #region Opens the channel and connection to RabbitMQ server
            ConnectionRabbitMQ connectionRabbitMQ = new ConnectionRabbitMQ();
            connectionRabbitMQ.GetConnection();
            connectionRabbitMQ.routingKey = msgRecover.QueueName;
            connectionRabbitMQ.OpenChannel();
            #endregion

            #region Reads messages from RabbitMQ queue
            var messageGetResult = connectionRabbitMQ.GetIndividualMessage();

            #region Verifies a message was returned
            if (messageGetResult == null)
            {
                msgRecover.RecoverSuceed = false;
                msgRecover.ErrorMessage  = "No messages avaiable on the queue at the moment.";
                return(msgRecover);
            }

            msgRecover.RecoverSuceed = true;
            #endregion

            #region Defines the message to be returned in the service response
            msgRecover.Message = System.Text.Encoding.UTF8.GetString(messageGetResult.Body);
            #endregion

            #endregion

            #region Closes the connection to RabbitMQ
            connectionRabbitMQ.channel.BasicAck(messageGetResult.DeliveryTag, false);
            connectionRabbitMQ.CloseChannel();
            #endregion
        }
        catch (Exception e)
        {
            msgRecover.RecoverSuceed = false;
            msgRecover.ErrorMessage  = e.Message;
        }

        return(msgRecover);
    }
        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");
            }
        }
Пример #4
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);
        }
Пример #5
0
    public MessagePublish PublishMessage(MessagePublish msgPublish)
    {
        try
        {
            #region Validates message publish object
            if (msgPublish == null || string.IsNullOrWhiteSpace(msgPublish.QueueName.Trim()) ||
                string.IsNullOrWhiteSpace(msgPublish.Message.Trim()))
            {
                throw new ArgumentNullException("Argumento inválido!");
            }
            #endregion

            #region Opens the channel and connection to RabbitMQ server
            ConnectionRabbitMQ connectionRabbitMQ = new ConnectionRabbitMQ();
            connectionRabbitMQ.GetConnection();
            connectionRabbitMQ.routingKey = msgPublish.QueueName;
            connectionRabbitMQ.OpenChannel();
            #endregion

            #region Publishes the message on RabbitMQ
            msgPublish.PublishSuceed = connectionRabbitMQ.PublishMessage(msgPublish.Message);
            #endregion

            #region Closes the connection to RabbitMQ
            try
            {
                connectionRabbitMQ.CloseChannel();
            }
            catch { }
            #endregion
        }
        catch (Exception e)
        {
            msgPublish.PublishSuceed = false;
            msgPublish.ErrorMessage  = e.Message;
        }

        return(msgPublish);
    }