/// <summary>Get a connection from stack
        /// </summary>
        public IConnection GetConnection()
        {
            if (!_connectionStack.TryPop(out IConnection connection))
            {
                if (_connectionCount < _maxConnectionCount)
                {
                    connection = _connectionFactory.CreateConnection(_option.ConnectionAddress);
                    //BindEvent
                    connection.OnConnectionClose += ConnectionCloseHandler;
                    Interlocked.Increment(ref _connectionCount);

                    if (!_connectionDict.TryAdd(connection.Id, connection))
                    {
                        _logger.LogWarning("Fail add connection to dict! ConnectionAddress:{0}", ConnectionAddress);
                    }

                    return(connection);
                }

                _logger.LogDebug("The connection stack is empty and created full, wait for get a return connection.Connection Pool Name '{0}',Server:'{1}'.", Name, _option.ConnectionAddress);
                _semaphoreSlim.Wait(5000);
                return(GetConnection());
            }
            return(connection);
        }
Beispiel #2
0
        public IConnection Connect()
        {
            var connection    = _connectionBuilder.CreateConnection(_configuration.EndpointUri);
            var outboundModel = connection.CreateModel();

            ApplyEntitiesDeclarations(outboundModel);

            return(CreateConnection(connection, outboundModel));
        }
Beispiel #3
0
 public static IDbConnection CreateConnection()
 {
     if (_instance == null)
     {
         lock (_object)
         {
             if (_instance == null)
             {
                 _instance = new ConnectionManager();
                 System.Diagnostics.Trace.WriteLine("DAL --> ConnectionBuilder : SqlServerConnectionBuilder");
                 _connectionBuilder = new Connections.Builders.SqlServerConnectionBuilder();
             }
             return(_connectionBuilder.CreateConnection());
         }
     }
     else
     {
         lock (_object)
         {
             return(_connectionBuilder.CreateConnection());
         }
     }
 }
Beispiel #4
0
        public IConnection Connect()
        {
            var connection    = _connectionBuilder.CreateConnection(_configuration.EndpointUri);
            var outboundModel = connection.CreateModel();

            foreach (var exchange in _exchanges)
            {
                exchange.Declare(outboundModel);
            }

            foreach (var queue in _queues)
            {
                queue.Declare(outboundModel);
            }

            foreach (var binding in _bindings)
            {
                binding.Declare(outboundModel);
            }

            var builder = new ConsumedMessageBuilder(_configuration.SerializationConfiguration,
                                                     _configuration.MessageTypeResolver);
            var outboundChannel = _configuration.OutboundChannelBuilder(outboundModel, _configuration);
            var consumers       = _promises.Select(_ =>
            {
                var model = CreateInboundModel(connection,
                                               _configuration.PrefetchSize,
                                               _configuration.PrefetchCount);
                var consumer = _(builder).BuildConsumer(new InboundChannel(model),
                                                        outboundChannel);
                return(new { Model = model, Consumer = consumer });
            })
                                  .ToList();

            foreach (var consumer in consumers)
            {
                consumer.Consumer.Declare(consumer.Model);
            }

            return(new Connection(connection,
                                  consumers.Select(_ => _.Consumer),
                                  outboundChannel,
                                  _configuration));
        }