FromString() public static method

public static FromString ( string value ) : RabbitMqAddress
value string
return RabbitMqAddress
コード例 #1
0
        public void Send(TransportMessage transportMessage, string destination)
        {
            var address = RabbitMqAddress.FromString(destination);

            //if sending locally we need to munge some stuff (remove routing keys)
            if (address == inputAddress)
            {
                address =
                    new RabbitMqAddress(
                        inputAddress.Broker,
                        inputAddress.VirtualHost,
                        inputAddress.Username,
                        inputAddress.Password,
                        inputAddress.Exchange,
                        inputAddress.QueueName,
                        string.Empty,
                        false);
            }

            using (var stream = new MemoryStream())
            {
                this.MessageSerializer.Serialize(transportMessage.Body, stream);
                using (var channel = connectionProvider.Open(this.ProtocolName, address, true))
                {
                    var messageId  = Guid.NewGuid().ToString();
                    var properties = channel.CreateBasicProperties();
                    properties.MessageId = messageId;
                    if (!String.IsNullOrEmpty(transportMessage.CorrelationId))
                    {
                        properties.CorrelationId = transportMessage.CorrelationId;
                    }

                    properties.Timestamp = DateTime.UtcNow.ToAmqpTimestamp();
                    properties.ReplyTo   = this.InputAddress;
                    properties.SetPersistent(transportMessage.Recoverable);
                    var headers = transportMessage.Headers;
                    if (headers != null && headers.Count > 0)
                    {
                        var dictionary = headers
                                         .ToDictionary <HeaderInfo, string, object>
                                             (entry => entry.Key, entry => entry.Value);

                        properties.Headers = dictionary;
                    }

                    if (address.RouteByType)
                    {
                        var    type     = transportMessage.Body[0].GetType();
                        string typeName = type.FullName;
                        log.InfoFormat("Sending message (routed) " + address.ToString(typeName) + " of " + type.Name);
                        channel.BasicPublish(address.Exchange, typeName, properties, stream.ToArray());
                        return;
                    }

                    var routingKeys = address.GetRoutingKeysAsArray();

                    if (routingKeys.Length > 1)
                    {
                        var message = "Too many Routing Keys specified for endpoint: " + address.ToString();
                        message += Environment.NewLine + "Keys: " + address.RoutingKeys;
                        log.Error(message);
                        throw new InvalidOperationException(message);
                    }

                    if (routingKeys.Length > 0)
                    {
                        var type = transportMessage.Body[0].GetType();
                        log.InfoFormat("Sending message (routed) " + address.ToString() + " of " + type.Name);
                        channel.BasicPublish(address.Exchange, routingKeys[0], properties, stream.ToArray());
                        return;
                    }

                    log.Info("Sending message " + address.ToString() + " of " + transportMessage.Body[0].GetType().Name);
                    channel.BasicPublish(address.Exchange, address.QueueName, properties, stream.ToArray());
                    transportMessage.Id = properties.MessageId;
                }
            }
        }