Esempio n. 1
0
        void PublishMessage(object message, MessageProperties messageProperties,
                            Action <IBasicProperties, IPublishInfo> replyAction)
        {
            IPublishInfo publishInfo = _publishRouteConfiguration.GetRouteInfo(message.GetType());
            IModel       channel     = _connection.CreateModel();

            channel.ExchangeDeclare(publishInfo.ExchangeName, publishInfo.ExchangeType,
                                    publishInfo.IsDurable,
                                    publishInfo.IsAutoDelete, null);
            ISerializationStrategy serializationStrategy = publishInfo.SerializationStrategy ?? _defaultSerializationStrategy;

            byte[] bytes = serializationStrategy.Serialize(message);

            var properties = new BasicProperties();

            ListDictionary messageHeaders = GetHeaders(messageProperties.Headers, publishInfo.DefaultHeaders);

            if (messageHeaders.Count != 0)
            {
                properties.Headers = messageHeaders;
            }

            properties.SetPersistent(publishInfo.IsPersistent);
            properties.ContentType     = serializationStrategy.ContentType;
            properties.ContentEncoding = serializationStrategy.ContentEncoding;
            if (publishInfo.IsSigned)
            {
                properties.UserId = _userName;
            }
            properties.CorrelationId = Guid.NewGuid().ToString();

            if (messageProperties.Expiration.HasValue)
            {
                properties.Expiration = messageProperties.Expiration.Value.TotalMilliseconds.ToString();
            }
            else if (publishInfo.Expiration.HasValue)
            {
                properties.Expiration = publishInfo.Expiration.Value.TotalMilliseconds.ToString();
            }

            if (replyAction != null)
            {
                replyAction(properties, publishInfo);
            }

            channel.BasicPublish(publishInfo.ExchangeName, messageProperties.RoutingKey ?? publishInfo.DefaultRoutingKey,
                                 properties, bytes);
            channel.Close();

            string log = string.Format("Published message to host: {0}, port: {1}, exchange: {2}, routingKey: {3}",
                                       _connection.Endpoint.HostName,
                                       _connection.Endpoint.Port,
                                       publishInfo.ExchangeName,
                                       messageProperties.RoutingKey);

            Logger.Current.Write(log, TraceEventType.Information);
        }
Esempio n. 2
0
        public void Send(SendParams sendParams)
        {
            sendParams.BusMessage.Sent = DateTime.Now;
            sendParams.BusMessage.BusId = sendParams.BusId;

            BasicProperties basicProperties = new BasicProperties
            {
                AppId = sendParams.BusMessage.BusId,
                Timestamp = sendParams.BusMessage.Sent.ToAmqpTimestamp(),
                ContentType = sendParams.Serializer.ContentType,
                Headers = new Dictionary<string, object>()
            };

            byte[] bytes;

            if (sendParams.BusMessage.Data != null)
            {
                Type type = sendParams.BusMessage.Data.GetType();
                DataContractKey contractKey = _nameMappings.GetOrAdd(type, t => t.GetDataContractKey());

                basicProperties.Type = contractKey.Name;
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.Name, contractKey.Name);
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.NameSpace, contractKey.Ns);

                bytes = sendParams.Serializer.Serialize(sendParams.BusMessage);
            }
            else
            {
                bytes = new byte[0];
            }

            foreach (BusHeaderBase header in sendParams.BusMessage.Headers)
            {
                basicProperties.Headers.Add(header.Name, header.GetValue());
            }

            if (sendParams.PersistentDelivery)
            {
                basicProperties.SetPersistent(true);
            }

            if (!string.IsNullOrEmpty(sendParams.ReplyTo))
            {
                basicProperties.ReplyTo = sendParams.ReplyTo;
            }

            if (!string.IsNullOrEmpty(sendParams.CorrelationId))
            {
                basicProperties.CorrelationId = sendParams.CorrelationId;
            }

            sendParams.Model.BasicPublish(sendParams.Exchange, sendParams.RoutingKey, sendParams.MandatoryDelivery, false, basicProperties, bytes);
        }
Esempio n. 3
0
        public void Send(SendParams sendParams)
        {
            sendParams.BusMessage.Sent  = DateTime.Now;
            sendParams.BusMessage.BusId = sendParams.BusId;

            BasicProperties basicProperties = new BasicProperties
            {
                AppId       = sendParams.BusMessage.BusId,
                Timestamp   = sendParams.BusMessage.Sent.ToAmqpTimestamp(),
                ContentType = sendParams.Serializer.ContentType,
                Headers     = new Dictionary <string, object>()
            };

            byte[] bytes;

            if (sendParams.BusMessage.Data != null)
            {
                Type            type        = sendParams.BusMessage.Data.GetType();
                DataContractKey contractKey = _nameMappings.GetOrAdd(type, t => t.GetDataContractKey());

                basicProperties.Type = contractKey.Name;
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.Name, contractKey.Name);
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.NameSpace, contractKey.Ns);

                bytes = sendParams.Serializer.Serialize(sendParams.BusMessage);
            }
            else
            {
                bytes = new byte[0];
            }

            foreach (BusHeaderBase header in sendParams.BusMessage.Headers)
            {
                basicProperties.Headers.Add(header.Name, header.GetValue());
            }

            if (sendParams.PersistentDelivery)
            {
                basicProperties.SetPersistent(true);
            }

            if (!string.IsNullOrEmpty(sendParams.ReplyTo))
            {
                basicProperties.ReplyTo = sendParams.ReplyTo;
            }

            if (!string.IsNullOrEmpty(sendParams.CorrelationId))
            {
                basicProperties.CorrelationId = sendParams.CorrelationId;
            }

            sendParams.Model.BasicPublish(sendParams.Exchange, sendParams.RoutingKey, sendParams.MandatoryDelivery, false, basicProperties, bytes);
        }
Esempio n. 4
0
        public void Send(RawBusMessage busMessage)
        {
            DataContractKey contractKey;
            Type            type = busMessage.Data.GetType();

            if (!_nameMappings.TryGetValue(type, out contractKey))
            {
                contractKey = type.GetDataContractKey();

                _nameMappings.TryAdd(type, contractKey);
            }

            busMessage.Sent  = DateTime.Now;
            busMessage.BusId = _busId;

            BasicProperties basicProperties = new BasicProperties
            {
                AppId       = busMessage.BusId,
                Timestamp   = busMessage.Sent.ToAmqpTimestamp(),
                Type        = contractKey.Name,
                ContentType = _configuration.Serializer.ContentType,
                Headers     = new Dictionary <string, object>
                {
                    { MessagingConstants.HeaderNames.Name, contractKey.Name },
                    { MessagingConstants.HeaderNames.NameSpace, contractKey.Ns }
                }
            };

            foreach (BusHeader header in busMessage.Headers)
            {
                basicProperties.Headers.Add(header.Name, header.Value);
            }

            if (_configuration.PersistentDelivery)
            {
                basicProperties.SetPersistent(true);
            }

            byte[] bytes = _configuration.Serializer.Serialize(busMessage);

            _model.BasicPublish(_configuration.Exchange, _configuration.RoutingKey, _configuration.MandatoryDelivery, false, basicProperties, bytes);
        }