示例#1
0
        public void PublishReply <TRequestMessage, TReplyMessage>(PublicationAddress publicationAddress,
                                                                  TReplyMessage replyMessage,
                                                                  IBasicProperties replyProperties)
        {
            IModel channel = _connection.CreateModel();

            if (publicationAddress.ExchangeName != string.Empty)
            {
                channel.ExchangeDeclare(publicationAddress.ExchangeName, publicationAddress.ExchangeType, false, true, null);
            }
            IConsumeInfo           consumeInfo           = _consumeRouteConfiguration.GetRouteInfo(typeof(TRequestMessage));
            ISerializationStrategy serializationStrategy = consumeInfo.SerializationStrategy ?? _defaultSerializationStrategy;

            byte[] bytes = serializationStrategy.Serialize(replyMessage);
            channel.BasicPublish(publicationAddress, replyProperties, bytes);
            channel.Close();

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

            Logger.Current.Write(log, TraceEventType.Information);
        }
示例#2
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);
        }
        public static string Serialize <T>(this T obj, ISerializationStrategy serializationStrategy) where T : class
        {
            if (typeof(T) == typeof(String))
            {
                return(obj as string);
            }

            return(serializationStrategy.Serialize(obj));
        }
示例#4
0
        public void Publish <TMessage>(TMessage message, string routingKey, ISerializationStrategy serializationStrategy)
        {
            Console.WriteLine(
                string.Format("Publishing message to exchange:\'{0}\' routing key:\'{1}\'", _exchangeName, routingKey),
                TraceEventType.Information);
            byte[] msg        = serializationStrategy.Serialize(message);
            var    properties = new BasicProperties();

            properties.Timestamp = new AmqpTimestamp((long)(DateTime.Now - _unixEpoch).TotalSeconds);
            _channel.BasicPublish(_exchangeName, routingKey, properties, msg);
        }
示例#5
0
 public string Serialize(object data)
 {
     return(_serializationStrategy.Serialize(data));
 }