Beispiel #1
0
        public void SuscribirMensaje()
        {
            string mensaje = string.Empty;

            try
            {
                IConnection connection = _servicioConexion.Conectar();
                using (var channel = connection.CreateModel())
                {
                    // This instructs the channel not to prefetch more than one message
                    channel.BasicQos(0, 1, false);

                    // Create a new, durable exchange
                    channel.ExchangeDeclare("Servicios", ExchangeType.Direct, true, false, null);
                    // Create a new, durable queue
                    channel.QueueDeclare("Colas", true, false, false, null);
                    // Bind the queue to the exchange
                    channel.QueueBind("Colas", "Servicios", "optional-routing-key");

                    using (var subscription = new Subscription(channel, "Colas", false))
                    {
                        Console.WriteLine("Waiting for messages...");
                        var encoding = new UTF8Encoding();
                        while (channel.IsOpen)
                        {
                            BasicDeliverEventArgs eventArgs;
                            var success = subscription.Next(2000, out eventArgs);
                            if (success == false)
                            {
                                continue;
                            }
                            var msgBytes = eventArgs.Body;
                            var message  = encoding.GetString(msgBytes);
                            mensaje = message;
                            Console.WriteLine(message);
                            Console.WriteLine("van esta cantidad de mensajes:" + recibidas);
                            channel.BasicAck(eventArgs.DeliveryTag, false);
                            _servicioEnviarMensaje.PublicarMensaje(obtenerMensaje(mensaje), recibidas);
                            recibidas++;
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                string mensanje = "No se pudo recibir mensajes";
                _servicioLog.RegistrarExcepcion(mensanje, ex);
            }
        }
        public string SuscribirMensaje(Mensaje mensajes)
        {
            try
            {
                string      mensaje    = string.Empty;
                int         hilo       = Thread.CurrentThread.ManagedThreadId;
                IConnection connection = _servicioConexion.Conectar();
                using (var channel = connection.CreateModel())
                {
                    // This instructs the channel not to prefetch more than one message
                    channel.BasicQos(0, 1, false);

                    // Create a new, durable exchange
                    channel.ExchangeDeclare("Respuesta", ExchangeType.Direct, true, false, null);
                    // Create a new, durable queue
                    channel.QueueDeclare("ColasRespuesta", true, false, false, null);
                    // Bind the queue to the exchange
                    channel.QueueBind("ColasRespuesta", "Respuesta", "optional-routing-key");

                    using (var subscription = new Subscription(channel, "ColasRespuesta", false))
                    {
                        Console.WriteLine("Waiting for messages...");
                        var encoding = new UTF8Encoding();
                        while (channel.IsOpen)
                        {
                            BasicDeliverEventArgs eventArgs;
                            var success = subscription.Next(2000, out eventArgs);
                            if (success == false)
                            {
                                continue;
                            }
                            var msgBytes = eventArgs.Body;
                            var message  = encoding.GetString(msgBytes);
                            mensaje = message.ToString();

                            channel.BasicAck(eventArgs.DeliveryTag, false);
                            return(mensaje);
                        }
                        return(mensaje);
                    }
                }
            }

            catch (Exception ex)
            {
                string mensanje = "No se pudo recibir mensajes";
                _servicioLog.RegistrarExcepcion("El mensaje con el id:" + mensajes.id + "no puedo ser enviado", ex);
                return(mensanje);
            }
        }
Beispiel #3
0
        public bool PublicarMensaje(Mensaje mensaje, int recibidas)
        {
            int contador = 1;

            try
            {
                IConnection connection = _servicioConexion.Conectar();

                if (connection.IsOpen)
                {
                    using (var channel = connection.CreateModel())
                    {
                        Thread.Sleep(2000);
                        channel.ExchangeDeclare("Respuesta", ExchangeType.Direct, true, false, null);

                        channel.QueueDeclare("ColasRespuesta", true, false, false, null);

                        channel.QueueBind("ColasRespuesta", "Respuesta", "optional-routing-key");


                        var properties = channel.CreateBasicProperties();
                        properties.DeliveryMode = 1;
                        properties.ClearMessageId();
                        properties.ReplyTo = "Respuesta";
                        var encoding = new UTF8Encoding();
                        var hour     = DateTime.Now.Hour;
                        var min      = DateTime.Now.Minute;
                        var sec      = DateTime.Now.Second;
                        var msg      = string.Format(@"He recibido tu mensajes con el id: {3} desde Ping! Hora:{0} : {1} : {2}", hour, min, sec, mensaje.id);
                        var msgBytes = encoding.GetBytes(msg);

                        channel.BasicPublish("Respuesta", "optional-routing-key", false, properties, msgBytes);
                        enviadas++;
                        channel.Close();
                        _respuesta.respuestas(mensaje.id.ToString(), contador, recibidas);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                _servicioLog.RegistrarExcepcion("El mensaje con el id:no puedo ser enviado", ex);
                return(false);
            }
        }
        public bool PublicarMensaje(Mensaje mensajes)
        {
            try
            {
                int         hilo       = Thread.CurrentThread.ManagedThreadId;
                IConnection connection = _servicioConexion.Conectar();

                if (connection.IsOpen)
                {
                    using (var channel = connection.CreateModel())
                    {
                        channel.ExchangeDeclare("Servicios", ExchangeType.Direct, true, false, null);

                        channel.QueueDeclare("Colas", true, false, false, null);

                        channel.QueueBind("Colas", "Servicios", "optional-routing-key");


                        var properties = channel.CreateBasicProperties();
                        properties.DeliveryMode = 1;
                        properties.ClearMessageId();
                        properties.ReplyTo = "Servicios";
                        var encoding = new UTF8Encoding();
                        var hora     = DateTime.Now;
                        var msg      = string.Format(@"El Id:{1} a Enviando un mensaje desde Ping! a las {0} que dice {2}", hora, mensajes.id, mensajes.Cuerpo);
                        var msgBytes = encoding.GetBytes(msg);

                        channel.BasicPublish("Servicios", "optional-routing-key", false, properties, msgBytes);

                        channel.Close();
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                _servicioLog.RegistrarExcepcion("El mensaje con el id:" + mensajes.id + "no puedo ser enviado", ex);
                return(false);
            }
        }