/// <summary> /// Configures the Azure Event Hub connection using the provided delegate. /// </summary> public void ConfigureEventHubConnection(CreateConnectionDelegate createConnection, string eventHubName, string consumerGroup) { EventHubName = eventHubName; ConsumerGroup = consumerGroup; ValidateValues(eventHubName, consumerGroup); CreateConnection = createConnection ?? throw new ArgumentNullException(nameof(createConnection)); }
private PooledStream Create(CreateConnectionDelegate createConnectionCallback) { PooledStream stream = null; try { stream = createConnectionCallback(this); if (stream == null) { throw new InternalException(); } if (!stream.CanBePooled) { throw new InternalException(); } stream.PrePush(null); lock (this.m_ObjectList.SyncRoot) { this.m_ObjectList.Add(stream); this.m_TotalObjects = this.m_ObjectList.Count; } } catch (Exception exception) { stream = null; this.m_ResError = exception; this.Abort(); } return(stream); }
internal static ConnectionPool GetConnectionPool(ServicePoint servicePoint, string groupName, CreateConnectionDelegate createConnectionCallback) { string key = GenerateKey(servicePoint.Host, servicePoint.Port, groupName); lock(InternalSyncObject) { ConnectionPool connectionPool = (ConnectionPool) m_ConnectionPools[key]; if (connectionPool == null) { connectionPool = new ConnectionPool(servicePoint, servicePoint.ConnectionLimit, 0, servicePoint.MaxIdleTime, createConnectionCallback); m_ConnectionPools[key] = connectionPool; } return connectionPool; } }
internal ConnectionPool(System.Net.ServicePoint servicePoint, int maxPoolSize, int minPoolSize, int idleTimeout, CreateConnectionDelegate createConnectionCallback) { this.m_CreateConnectionCallback = createConnectionCallback; this.m_MaxPoolSize = maxPoolSize; this.m_MinPoolSize = minPoolSize; this.m_ServicePoint = servicePoint; this.Initialize(); if (idleTimeout > 0) { this.m_CleanupQueue = TimerThread.GetOrCreateQueue((idleTimeout == 1) ? 1 : (idleTimeout / 2)); this.m_CleanupQueue.CreateTimer(s_CleanupCallback, this); } }
public void ShouldAllocateReturnConnectionToPool() { CreateConnectionDelegate handler = delegate(string host) { return(connection); }; MessagingContextPool pool = new MessagingContextPool(messageFactory, handler); IMessagingContext context1 = pool.GetContext("foo"); Assert.IsNotNull(context1); context1.Dispose(); IMessagingContext context2 = pool.GetContext("foo"); Assert.AreEqual(context1, context2); }
/// <summary> /// <para>Constructor - binds pool with a servicePoint and sets up a cleanup Timer to nuke Idle Connections</para> /// </summary> internal ConnectionPool(ServicePoint servicePoint, int maxPoolSize, int minPoolSize, int idleTimeout, CreateConnectionDelegate createConnectionCallback) : base() { m_State = State.Initializing; m_CreateConnectionCallback = createConnectionCallback; m_MaxPoolSize = maxPoolSize; m_MinPoolSize = minPoolSize; m_ServicePoint = servicePoint; Initialize(); if (idleTimeout > 0) { m_CleanupQueue = TimerThread.GetOrCreateQueue(idleTimeout / 2); m_CleanupQueue.CreateTimer(s_CleanupCallback, this); } }
/// <summary> /// <para>Creates a new PooledStream, performs checks as well on the new stream</para> /// </summary> private PooledStream Create(CreateConnectionDelegate createConnectionCallback) { GlobalLog.Enter("ConnectionPool#" + ValidationHelper.HashString(this) + "::Create"); PooledStream newObj = null; try { newObj = createConnectionCallback(this); if (null == newObj) { throw new InternalException(); // Create succeeded, but null object } if (!newObj.CanBePooled) { throw new InternalException(); // Create succeeded, but non-poolable object } newObj.PrePush(null); lock (m_ObjectList.SyncRoot) { m_ObjectList.Add(newObj); m_TotalObjects = m_ObjectList.Count; } GlobalLog.Print("Create pooledStream#" + ValidationHelper.HashString(newObj)); } catch (Exception e) { GlobalLog.Print("Pool Exception: " + e.Message); newObj = null; // set to null, so we do not return bad new object // Failed to create instance m_ResError = e; Abort(); } catch { GlobalLog.Print("Pool Exception: Non-CLS Compliant Exception"); newObj = null; // set to null, so we do not return bad new object // Failed to create instance m_ResError = new Exception(SR.GetString(SR.net_nonClsCompliantException)); Abort(); } GlobalLog.Leave("ConnectionPool#" + ValidationHelper.HashString(this) + "::Create", ValidationHelper.HashString(newObj)); return(newObj); }
/// <summary> /// <para>Constructor - binds pool with a servicePoint and sets up a cleanup Timer to remove Idle Connections</para> /// </summary> internal ConnectionPool(ServicePoint servicePoint, int maxPoolSize, int minPoolSize, int idleTimeout, CreateConnectionDelegate createConnectionCallback) : base() { m_State = State.Initializing; m_CreateConnectionCallback = createConnectionCallback; m_MaxPoolSize = maxPoolSize; m_MinPoolSize = minPoolSize; m_ServicePoint = servicePoint; Initialize(); if (idleTimeout > 0) { // special case: if the timeout value is 1 then the timer thread should have a duration // of 1 to avoid having the timer callback run constantly m_CleanupQueue = TimerThread.GetOrCreateQueue(idleTimeout == 1 ? 1 : (idleTimeout / 2)); m_CleanupQueue.CreateTimer(s_CleanupCallback, this); } }
public MessagingContext(MessageFactory factory, string host, CreateConnectionDelegate createConnection) { this.helper = factory; this.host = host; this.createConnection = createConnection; }
public MessagingContextPool(MessageFactory messageFactory, CreateConnectionDelegate createConnectionDelegate) { this.messageFactory = messageFactory; this.createConnectionDelegate = createConnectionDelegate; }
public MessagingContext (MessageFactory factory, string host, CreateConnectionDelegate createConnection) { this.helper = factory; this.host = host; this.createConnection = createConnection; }
/// <summary> /// <para>Creates a new PooledStream, performs checks as well on the new stream</para> /// </summary> private PooledStream Create(CreateConnectionDelegate createConnectionCallback) { GlobalLog.Enter("ConnectionPool#" + ValidationHelper.HashString(this) + "::Create"); PooledStream newObj = null; try { newObj = createConnectionCallback(this); if (null == newObj) throw new InternalException(); // Create succeeded, but null object if (!newObj.CanBePooled) throw new InternalException(); // Create succeeded, but non-poolable object newObj.PrePush(null); lock (m_ObjectList.SyncRoot) { m_ObjectList.Add(newObj); m_TotalObjects = m_ObjectList.Count; } GlobalLog.Print("Create pooledStream#"+ValidationHelper.HashString(newObj)); } catch(Exception e) { GlobalLog.Print("Pool Exception: " + e.Message); newObj = null; // set to null, so we do not return bad new object // Failed to create instance m_ResError = e; Abort(); } catch { GlobalLog.Print("Pool Exception: Non-CLS Compliant Exception"); newObj = null; // set to null, so we do not return bad new object // Failed to create instance m_ResError = new Exception(SR.GetString(SR.net_nonClsCompliantException)); Abort(); } GlobalLog.Leave("ConnectionPool#" + ValidationHelper.HashString(this) + "::Create",ValidationHelper.HashString(newObj)); return newObj; }
internal static ConnectionPool GetConnectionPool(ServicePoint servicePoint, string groupName, CreateConnectionDelegate createConnectionCallback) { string key = GenerateKey(servicePoint.Host, servicePoint.Port, groupName); lock (InternalSyncObject) { ConnectionPool connectionPool = (ConnectionPool)m_ConnectionPools[key]; if (connectionPool == null) { connectionPool = new ConnectionPool(servicePoint, servicePoint.ConnectionLimit, 0, servicePoint.MaxIdleTime, createConnectionCallback); m_ConnectionPools[key] = connectionPool; } return(connectionPool); } }
public MessagingContextPool (MessageFactory messageFactory, CreateConnectionDelegate createConnectionDelegate) { this.messageFactory = messageFactory; this.createConnectionDelegate = createConnectionDelegate; }
private PooledStream Create(CreateConnectionDelegate createConnectionCallback) { PooledStream stream = null; try { stream = createConnectionCallback(this); if (stream == null) { throw new InternalException(); } if (!stream.CanBePooled) { throw new InternalException(); } stream.PrePush(null); lock (this.m_ObjectList.SyncRoot) { this.m_ObjectList.Add(stream); this.m_TotalObjects = this.m_ObjectList.Count; } } catch (Exception exception) { stream = null; this.m_ResError = exception; this.Abort(); } return stream; }