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; }
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; }
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); }
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); }
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); }
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)); }
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)); }
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)); }
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(); } }
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(); } }
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); } }
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); } } }
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); } }
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();*/ }
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); }
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); } } }
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); }
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); }
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(); }
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();*/ }
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; }
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; } } }
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; } } }
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); }
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(); }