コード例 #1
0
        private ConnectionFactory GetConnectionFactory(IProtocol protocol, RabbitMqAddress brokerAddress)
        {
            ConnectionFactory factory = null;
            var key = string.Format("{0}:{1}", protocol, brokerAddress);

            if(!connectionFactories.TryGetValue(key, out factory))
            {
                factory = new ConnectionFactory();
                factory.Endpoint = new AmqpTcpEndpoint(protocol, brokerAddress.Broker);

                if(!string.IsNullOrEmpty(brokerAddress.VirtualHost))
                    factory.VirtualHost = brokerAddress.VirtualHost;

                if(!string.IsNullOrEmpty(brokerAddress.Username))
                    factory.UserName = brokerAddress.Username;

                if(!string.IsNullOrEmpty(brokerAddress.Password))
                    factory.Password = brokerAddress.Password;

                factory = connectionFactories.GetOrAdd(key, factory);
                log.Debug("Opening new Connection Factory " + brokerAddress + " using " + protocol.ApiName);
            }

            return factory;
        }
コード例 #2
0
        private IConnection GetConnection(IProtocol protocol, RabbitMqAddress brokerAddress, ConnectionFactory factory)
        {
            IConnection connection = null;
            var key = string.Format("{0}:{1}", protocol, brokerAddress);

            if (!connections.TryGetValue(key, out connection))
            {
                var newConnection = factory.CreateConnection();
                connection = connections.GetOrAdd(key, newConnection);

                //if someone else beat us from another thread kill the connection just created
                if(newConnection.Equals(connection) == false)
                {
                    newConnection.Dispose();
                }
                else
                {
                    log.DebugFormat("Opening new Connection {0} on {1} using {2}",
                                    connection, brokerAddress, protocol.ApiName);
                }

            }

            return connection;
        }
コード例 #3
0
        private ConnectionFactory GetConnectionFactory(IProtocol protocol, RabbitMqAddress brokerAddress)
        {
            ConnectionFactory factory = null;
            var key = string.Format("{0}:{1}", protocol, brokerAddress);

            if (!connectionFactories.TryGetValue(key, out factory))
            {
                factory          = new ConnectionFactory();
                factory.Endpoint = new AmqpTcpEndpoint(protocol, brokerAddress.Broker);

                if (!string.IsNullOrEmpty(brokerAddress.VirtualHost))
                {
                    factory.VirtualHost = brokerAddress.VirtualHost;
                }

                if (!string.IsNullOrEmpty(brokerAddress.Username))
                {
                    factory.UserName = brokerAddress.Username;
                }

                if (!string.IsNullOrEmpty(brokerAddress.Password))
                {
                    factory.Password = brokerAddress.Password;
                }

                factory = connectionFactories.GetOrAdd(key, factory);
                log.Debug("Opening new Connection Factory " + brokerAddress + " using " + protocol.ApiName);
            }

            return(factory);
        }
コード例 #4
0
        private IModel OpenNew(string protocolName, RabbitMqAddress brokerAddress)
        {
            var protocol   = GetProtocol(protocolName);
            var factory    = GetConnectionFactory(protocol, brokerAddress);
            var connection = GetConnection(protocol, brokerAddress, factory);
            var model      = connection.CreateModel();

            return(model);
        }
コード例 #5
0
 public IModel Open(string protocolName, RabbitMqAddress brokerAddress, bool transactional)
 {
     if (!transactional)
     {
         var opened = OpenNew(protocolName, brokerAddress);
         //opened.Disposed += (sender, e) => log.Debug("Closing " + brokerAddress);
         return opened;
     }
     return OpenTransactional(protocolName, brokerAddress);
 }
コード例 #6
0
 public IModel Open(string protocolName, RabbitMqAddress brokerAddress, bool transactional)
 {
     if (!transactional)
     {
         var opened = OpenNew(protocolName, brokerAddress);
         //opened.Disposed += (sender, e) => log.Debug("Closing " + brokerAddress);
         return(opened);
     }
     return(OpenTransactional(protocolName, brokerAddress));
 }
コード例 #7
0
 public bool Equals(RabbitMqAddress other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other.Broker, Broker) && Equals(other.Exchange, Exchange) && Equals(other.QueueName, QueueName) && Equals(other.RoutingKeys, RoutingKeys) && other.RouteByType.Equals(RouteByType));
 }
コード例 #8
0
 public bool Equals(RabbitMqAddress other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other.broker, broker) && Equals(other.exchange, exchange) && Equals(other.queueName, queueName) && Equals(other.routingKeys, routingKeys) && other.routeByType.Equals(routeByType));
 }
コード例 #9
0
        public void Start()
        {
            CreateExchangesAndQueuesIfNecessary();
            BindExchangesAndQueues();

            inputAddress = new RabbitMqAddress(InputBroker, InputExchange, InputQueue, InputRoutingKeys);
            errorAddress = new RabbitMqAddress(ErrorBroker, ErrorExchange, ErrorQueue, ErrorRoutingKeys);

            for (var i = 0; i < numberOfWorkerThreads; ++i)
            {
                AddWorkerThread().Start();
            }
        }
コード例 #10
0
        public void Start()
        {
            inputAddress = new RabbitMqAddress(InputBroker, InputVirtualHost, InputUsername, InputPassword, InputExchange, InputQueue, InputRoutingKeys, false);
            errorAddress = new RabbitMqAddress(ErrorBroker, ErrorVirtualHost, ErrorUsername, ErrorPassword, ErrorExchange, ErrorQueue, ErrorRoutingKeys, false);

            CreateExchangesAndQueuesIfNecessary();
            BindExchangesAndQueues();

            for (var i = 0; i < numberOfWorkerThreads; ++i)
            {
                AddWorkerThread().Start();
            }
        }
コード例 #11
0
        private void DeclareQueue(RabbitMqAddress broker, string queue, bool durable)
        {
            if (string.IsNullOrEmpty(queue))
            {
                log.Info("No Queue Provided. Not attempting Declare");
                return;
                //var message = "No Input Queue Provided. Cannot Declare Queue";
                //log.Error(message);
                //throw new InvalidOperationException(message);
            }

            using (var channel = connectionProvider.Open(ProtocolName, broker, true))
            {
                log.InfoFormat("Declaring Queue {0} on Broker {1}", queue, broker);
                channel.QueueDeclare(queue, durable, false, false, null);
            }
        }
コード例 #12
0
        private void BindQueue(RabbitMqAddress broker, string exchange, string queue, string routingKeys)
        {
            if (string.IsNullOrEmpty(exchange))
            {
                return;
            }

            using (var channel = connectionProvider.Open(ProtocolName, broker, true))
            {
                var keys = routingKeys.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                keys = keys.Length == 0 ? new[] { queue } : keys;

                foreach (var key in keys)
                {
                    log.InfoFormat("Binding Key {0} on Queue {1} on Exchange {2}", key, queue, exchange);
                    channel.QueueBind(queue, exchange, key);
                }
            }
        }
コード例 #13
0
        private void DeclareExchange(RabbitMqAddress broker, string exchange, string exchangeType)
        {
            if (string.IsNullOrEmpty(exchange))
            {
                log.Info("No Exchange Provided. Not attempting Declare");
                return;
                //var message = "No Input Exchange Provided. Cannot Declare Exchange";
                //log.Error(message);
                //throw new InvalidOperationException(message);
            }

            using (var channel = connectionProvider.Open(ProtocolName, broker, true))
            {
                log.InfoFormat(
                    "Declaring Exchange {0} of Type {1} on Broker {2}",
                    exchange,
                    exchangeType,
                    broker);

                channel.ExchangeDeclare(exchange, exchangeType);
            }
        }
コード例 #14
0
        private IModel OpenTransactional(string protocolName, RabbitMqAddress brokerAddress)
        {
            //var key = string.Format("{0}:{1}", protocolName, brokerAddress);
            return(OpenNew(protocolName, brokerAddress));
            //opened.Disposed += (sender, e) =>
            //                    {
            //                        log.Debug("Closing " + brokerAddress);
            //                        if (state != null)
            //                        {
            //                            var cnx = opened.Connection;
            //                            connections.TryRemove(key, out cnx);
            //                            state.Remove(brokerAddress.ToString());
            //                        }
            //                    };

            /*
             * if (Transaction.Current != null)
             * {
             * Transaction.Current.EnlistVolatile(new RabbitMqEnlistment(opened), EnlistmentOptions.None);
             * opened.AddRef();
             * }
             *
             * return opened.AddRef();*/
        }
コード例 #15
0
        private IConnection GetConnection(IProtocol protocol, RabbitMqAddress brokerAddress, ConnectionFactory factory)
        {
            IConnection connection = null;
            var         key        = string.Format("{0}:{1}", protocol, brokerAddress);

            if (!connections.TryGetValue(key, out connection))
            {
                var newConnection = factory.CreateConnection();
                connection = connections.GetOrAdd(key, newConnection);

                //if someone else beat us from another thread kill the connection just created
                if (newConnection.Equals(connection) == false)
                {
                    newConnection.Dispose();
                }
                else
                {
                    log.DebugFormat("Opening new Connection {0} on {1} using {2}",
                                    connection, brokerAddress, protocol.ApiName);
                }
            }

            return(connection);
        }
コード例 #16
0
        private void DeclareQueue(RabbitMqAddress broker, string queue, bool durable)
        {
            if (string.IsNullOrEmpty(queue))
            {
                log.Info("No Queue Provided. Not attempting Declare");
                return;
                //var message = "No Input Queue Provided. Cannot Declare Queue";
                //log.Error(message);
                //throw new InvalidOperationException(message);
            }

            using (var channel = connectionProvider.Open(ProtocolName, broker, true))
            {
                log.InfoFormat("Declaring Queue {0} on Broker {1}", queue, broker);
                channel.QueueDeclare(queue, durable, false, false, null);
            }
        }
コード例 #17
0
        private void DeclareExchange(RabbitMqAddress broker, string exchange, string exchangeType)
        {
            if (string.IsNullOrEmpty(exchange))
            {
                log.Info("No Exchange Provided. Not attempting Declare");
                return;
                //var message = "No Input Exchange Provided. Cannot Declare Exchange";
                //log.Error(message);
                //throw new InvalidOperationException(message);
            }

            using (var channel = connectionProvider.Open(ProtocolName, broker, true))
            {
                log.InfoFormat(
                    "Declaring Exchange {0} of Type {1} on Broker {2}",
                    exchange,
                    exchangeType,
                    broker);

                channel.ExchangeDeclare(exchange, exchangeType);
            }
        }
コード例 #18
0
        private void BindQueue(RabbitMqAddress broker, string exchange, string queue, string routingKeys)
        {
            if (string.IsNullOrEmpty(exchange))
                return;

            using (var channel = connectionProvider.Open(ProtocolName, broker, true))
            {
                var keys = routingKeys.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                keys = keys.Length == 0 ? new[] { queue } : keys;

                foreach(var key in keys)
                {
                    log.InfoFormat("Binding Key {0} on Queue {1} on Exchange {2}", key, queue, exchange);
                    channel.QueueBind(queue, exchange, key);
                }
            }
        }
コード例 #19
0
 public bool Equals(RabbitMqAddress other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return Equals(other.broker, broker) && Equals(other.exchange, exchange) && Equals(other.queueName, queueName) && Equals(other.routingKeys, routingKeys) && other.routeByType.Equals(routeByType);
 }
コード例 #20
0
 public bool Equals(RabbitMqAddress other)
 {
   if (ReferenceEquals(null, other)) return false;
   if (ReferenceEquals(this, other)) return true;
   return Equals(other._broker, _broker) && Equals(other._exchange, _exchange) && Equals(other._routingKey, _routingKey);
 }
コード例 #21
0
        public void Start()
        {
            CreateExchangesAndQueuesIfNecessary();
            BindExchangesAndQueues();

            inputAddress = new RabbitMqAddress(InputBroker, InputExchange, InputQueue, InputRoutingKeys);
            errorAddress = new RabbitMqAddress(ErrorBroker, ErrorExchange, ErrorQueue, ErrorRoutingKeys);

            for (var i = 0; i < numberOfWorkerThreads; ++i)
                AddWorkerThread().Start();
        }
コード例 #22
0
        private IModel OpenTransactional(string protocolName, RabbitMqAddress brokerAddress)
        {
            //var key = string.Format("{0}:{1}", protocolName, brokerAddress);
            return OpenNew(protocolName, brokerAddress);
            //opened.Disposed += (sender, e) =>
            //                    {
            //                        log.Debug("Closing " + brokerAddress);
            //                        if (state != null)
            //                        {
            //                            var cnx = opened.Connection;
            //                            connections.TryRemove(key, out cnx);
            //                            state.Remove(brokerAddress.ToString());
            //                        }
            //                    };
            /*
              if (Transaction.Current != null)
              {
            Transaction.Current.EnlistVolatile(new RabbitMqEnlistment(opened), EnlistmentOptions.None);
            opened.AddRef();
              }

            return opened.AddRef();*/
        }
コード例 #23
0
 private IModel OpenNew(string protocolName, RabbitMqAddress brokerAddress)
 {
     var protocol = GetProtocol(protocolName);
     var factory = GetConnectionFactory(protocol, brokerAddress);
     var connection = GetConnection(protocol, brokerAddress, factory);
     var model = connection.CreateModel();
     return model;
 }
コード例 #24
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;
                }
            }
        }
コード例 #25
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;
                }
            }
        }
コード例 #26
0
 public bool Equals(RabbitMqAddress other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return Equals(other.Broker, Broker) && Equals(other.Exchange, Exchange) && Equals(other.QueueName, QueueName) && Equals(other.RoutingKeys, RoutingKeys) && other.RouteByType.Equals(RouteByType);
 }
コード例 #27
0
        public void Start()
        {
            inputAddress = new RabbitMqAddress(InputBroker, InputVirtualHost, InputUsername, InputPassword, InputExchange, InputQueue, InputRoutingKeys, false);
            errorAddress = new RabbitMqAddress(ErrorBroker, ErrorVirtualHost, ErrorUsername, ErrorPassword, ErrorExchange, ErrorQueue, ErrorRoutingKeys, false);

            CreateExchangesAndQueuesIfNecessary();
            BindExchangesAndQueues();

            for (var i = 0; i < numberOfWorkerThreads; ++i)
                AddWorkerThread().Start();
        }