Beispiel #1
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);
    }
Beispiel #2
0
        public static string RecoverMessage(string queue)
        {
            try
            {
                MessageRecover messageRecover = new MessageRecover();
                messageRecover.QueueName = queue;

                messageRecover = messageBrokerServiceClient.RecoverMessageAsync(messageRecover).Result;

                if (!messageRecover.RecoverSuceed)
                {
                    throw new Exception(messageRecover.ErrorMessage);
                }

                return(messageRecover.Message);
            }
            catch //(Exception ex)
            {
                //todo: Log error message in ex.message propertie
                return(string.Empty);
            }
        }