public void Send(object obj, string label, MessageQueueTransaction transaction)
        {
            if (typeof(Message) == obj.GetType())
            {
                Message m = (Message)obj;
                m.Label = label;

                if (m.BodyStream == null)
                {
                    IMessageFormatter f = (m.Formatter == null) ? Formatter : m.Formatter;
                    f.Write(m, m.Body);
                }

                try
                {
                    delegateQueue.Send(m.DelegateMessage, transaction.DelegateTx);
                }
                catch (ConnectionException e)
                {
                    throw new MessageQueueException(MessageQueueErrorCode.QueueNotAvailable, e.Message);
                }
                catch (MonoMessagingException e)
                {
                    throw new MessageQueueException(MessageQueueErrorCode.Generic, e.Message);
                }
            }
            else
            {
                Message m = new Message(obj);
                Send(m, label, transaction);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Sets the body.
        /// </summary>
        /// <param name="serializedMessage">The serialized message.</param>
        /// <param name="body">The body.</param>
        /// <returns>The <see cref="SerializableMessage"/></returns>
        public static SerializableMessage SetBody(this SerializableMessage serializedMessage, object body)
        {
            if (serializedMessage == null)
            {
                throw new ArgumentNullException("serializedMessage");
            }

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

                // in order to ensure that the message is written to the MessageData property correctly,
                // use the message formatter to write to a new Message object, then extract the serialized
                // data from the Message.BodyStream.
                using (Message message = new Message())
                {
                    // setup the message object...
                    message.Formatter = messageFormatter;
                    message.Body      = body;

                    // use the formatter to write the object to the message
                    // this populates the message.BodyStream property.
                    messageFormatter.Write(message, body);

                    // reset the stream position to zero just in case the message formatter neglects to do so...
                    message.BodyStream.Position = 0;

                    serializedMessage.MessageData = StreamUtility.ReadFully(message.BodyStream);

                    return(serializedMessage);
                }
            }
        }
Esempio n. 3
0
        public void Send <T>(T message)
        {
            var formattedMessage = _messageFormatter.Write(message);

            var messageRequest = new SendMessageRequest()
                                 .WithQueueUrl(_uri)
                                 .WithMessageBody(formattedMessage);

            _client.SendMessage(messageRequest);
        }
        /// <summary>
        /// Writes an object to the message's body. First applies the base formatter, then compresses.
        /// </summary>
        /// <param name="message">The message to be written to</param>
        /// <param name="obj">The message to write</param>
        public void Write(Message message, object obj)
        {
            Message clearMessage = new Message();

            m_BaseFormatter.Write(clearMessage, obj);

            message.BodyType = clearMessage.BodyType;

            // In case the original body length is less than 1 kilobyte - don't compress it
            long uncompressedBodyLength = clearMessage.BodyStream.Length;

            if (uncompressedBodyLength <= 1024)
            {
                message.BodyStream          = clearMessage.BodyStream;
                message.BodyStream.Position = 0;
                return;
            }

            MemoryStream compressedBodyMemoryStream = new MemoryStream();

            using (DeflateStream compressor = new DeflateStream(compressedBodyMemoryStream, CompressionMode.Compress, true))
            {
                MessageHeader messageHeader = new MessageHeader(uncompressedBodyLength);
                messageHeader.CopyTo(compressedBodyMemoryStream); // Header goes uncompressed!

                clearMessage.BodyStream.Position = 0;
                clearMessage.BodyStream.CopyTo(compressor);
            }

            long compressedBodyLength = compressedBodyMemoryStream.Length;

            if (compressedBodyLength < uncompressedBodyLength)
            {
                message.BodyStream          = compressedBodyMemoryStream;
                message.BodyStream.Position = 0;
            }
            else
            {
                message.BodyStream          = clearMessage.BodyStream;
                message.BodyStream.Position = 0;
            }
        }
        public void Send <T>(T message)
        {
            var connectionFactory = new ConnectionFactory {
                Uri = _uri
            };

            using (var connection = connectionFactory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    var exchange    = _exchangeNameFactory.Build <T>();
                    var routingKey  = _routingKeyFactory.Build <T>();
                    var messageBody = _messageFormatter.Write(message);

                    var basicProperties = channel.CreateBasicProperties();
                    basicProperties.ContentType = _messageFormatter.GetMIMEContentType();

                    channel.ExchangeDeclare(exchange, _configuration.ExchangeType, _configuration.IsExchangeDurable);

                    channel.BasicPublish(exchange, routingKey, basicProperties, messageBody);

                    channel.Close();
                    connection.Close();
                }
        }