public NmsSession BorrowSession() { NmsPooledSession session = null; lock (this) { if (this.idleSessions.Count == 0 && this.settings.AutoGrowSessions && this.referencedSessions.Count < this.settings.MaximumSessionsPerConnection) { var newSession = this.connection.connection.CreateSession(this.AcknowledgementMode); session = new NmsPooledSession(this.connection.connection, newSession, this); } else if (this.idleSessions.Count > 0) { session = this.idleSessions.Dequeue(); } else { log.Warn("[{0}] The maximum number of sessions ({1}) has been reached for the session pool.", this.connection.id, this.settings.MaximumSessionsPerConnection); throw new InvalidOperationException("The maximum number of allowed sessions for the session pool has been reached."); } this.referencedSessions.Add(session); } log.Debug("[{0}] Borrowing session #{1}.", this.connection.id, session.id); return session; }
public void Start() { log.Info("[{0}] Session pool is starting.", this.connection.id); lock (this.idleSessions) { for (int i = 0; i < this.settings.MinimumSessionsPerConnection; i++) { sessionID++; log.Info("[{0}] Creating a new session #{1}.", this.connection.id, sessionID); var newSession = this.connection.connection.CreateSession(this.AcknowledgementMode); var session = new NmsPooledSession(this.connection.connection, newSession, this); session.sessionPool = this; session.id = sessionID; this.idleSessions.Enqueue(session); } } this.IsStarted = true; }