Example #1
0
        /// <summary>
        /// Gets the body.
        /// </summary>
        /// <param name="serializedMessage">The serialized message.</param>
        /// <returns>The message body as an <see cref="Object"/></returns>
        public static object GetBody(this SerializableMessage serializedMessage)
        {
            if (serializedMessage == null)
            {
                throw new ArgumentNullException("serializedMessage");
            }

            lock (serializedMessage)
            {
                // create an instance of the message formatter supplied in the serialized message.
                IMessageFormatter messageFormatter = (IMessageFormatter)Activator.CreateInstance(serializedMessage.FormatterType);

                // now use that formatter to read the message.
                using (MemoryStream memoryStream = new MemoryStream(serializedMessage.MessageData))
                {
                    using (Message message = new Message())
                    {
                        message.BodyStream = memoryStream;

                        if (messageFormatter.CanRead(message))
                        {
                            return(messageFormatter.Read(message));
                        }
                    }
                }

                return(null);
            }
        }
        /// <summary>
        /// Reads a message. Decomress and then applies the base formatter
        /// </summary>
        /// <param name="message">The message to be read</param>
        /// <returns>The result object - goes to the message Body</returns>
        public object Read(Message message)
        {
            long bodyLength = message.BodyStream.Length;

            OriginalBodyLength   = bodyLength;
            CompressedBodyLength = bodyLength;

            if (bodyLength < MessageHeader.Length)
            {
                return(m_BaseFormatter.Read(message)); // Message appeared to be uncompressed
            }

            MessageHeader messageHeader = new MessageHeader(message.BodyStream);

            if (!messageHeader.PrefixIsValid)
            {
                message.BodyStream.Position = 0;
                return(m_BaseFormatter.Read(message)); // Message appeared to be uncompressed
            }

            OriginalBodyLength = messageHeader.UncompressedBodyLength;
            using (MemoryStream decompressedBodyMemoryStream = new MemoryStream())
            {
                DeflateStream decompressor = new DeflateStream(message.BodyStream, CompressionMode.Decompress);
                decompressor.CopyTo(decompressedBodyMemoryStream);
                decompressor.Close();

                // Create a "clear" message and apply the base format to it
                Message clearMessage = new Message();
                clearMessage.BodyType            = message.BodyType;
                clearMessage.BodyStream          = decompressedBodyMemoryStream;
                clearMessage.BodyStream.Position = 0;

                return(m_BaseFormatter.Read(clearMessage));
            }
        }
Example #3
0
        private void PollForMessages(Action <IMessageContext <T> > messageHandler)
        {
            while (_isSubscribed)
            {
                var message = ReadMessageFromQueue();
                if (message != null)
                {
                    var body = _messageFormatter.Read <T>(message.Body);

                    var context = new AmazonSqsMessageContext <T>(_client, message, _uri)
                    {
                        Message = body
                    };
                    messageHandler(context);
                }
            }
        }
Example #4
0
        private void PollForMessages(IModel channel, Action <IMessageContext <T> > messageHandler, QueueingBasicConsumer consumer)
        {
            while (_isSubscribed)
            {
                try
                {
                    var message = (RabbitMQ.Client.Events.BasicDeliverEventArgs)consumer.Queue.Dequeue();

                    var context = new RabbitMqMessageContext <T>(channel, message.DeliveryTag);
                    context.Message = _messageFormatter.Read <T>(message.Body);

                    messageHandler(context);
                }
                catch (OperationInterruptedException)
                {
                }
            }
        }