internal static async Task <AdvisoryConsumer> CreateAsync(Connection connection, ConsumerId consumerId) { var advisoryConsumer = new AdvisoryConsumer(connection, consumerId); connection.AddDispatcher(consumerId, advisoryConsumer); await connection.SyncRequestAsync(advisoryConsumer.info).Await(); return(advisoryConsumer); }
/// <summary> /// Check and ensure that the connection objcet is connected. If it is not /// connected or is closed, a ConnectionClosedException is thrown. /// </summary> internal void CheckConnected() { if (closed.Value) { throw new ConnectionClosedException(); } if (!connected.Value) { DateTime timeoutTime = DateTime.Now + this.RequestTimeout; int waitCount = 1; while (true) { if (Monitor.TryEnter(checkConnectedLock)) { try { if (!connected.Value) { if (!this.userSpecifiedClientID) { this.info.ClientId = this.clientIdGenerator.GenerateId(); } try { if (null != transport) { // Send the connection and see if an ack/nak is returned. Response response = transport.Request(this.info, this.RequestTimeout); if (!(response is ExceptionResponse)) { connected.Value = true; if (this.watchTopicAdviosires) { ConsumerId id = new ConsumerId( new SessionId(info.ConnectionId, -1), Interlocked.Increment(ref this.consumerIdCounter)); this.advisoryConsumer = new AdvisoryConsumer(this, id); } } } } catch { } } } finally { Monitor.Exit(checkConnectedLock); } } if (connected.Value || DateTime.Now > timeoutTime) { break; } // Back off from being overly aggressive. Having too many threads // aggressively trying to connect to a down broker pegs the CPU. Thread.Sleep(5 * (waitCount++)); } if (!connected.Value) { throw new ConnectionClosedException(); } } }
public void Close() { if (!this.closed.Value && !transportFailed.Value) { this.Stop(); } lock (myLock) { if (this.closed.Value) { return; } try { Tracer.Info("Connection.Close(): Closing Connection Now."); this.closing.Value = true; if (this.advisoryConsumer != null) { this.advisoryConsumer.Dispose(); this.advisoryConsumer = null; } lock (sessions.SyncRoot) { foreach (Session session in sessions) { session.Shutdown(); } } sessions.Clear(); foreach (ActiveMQTempDestination dest in this.tempDests.Values) { dest.Delete(); } // Connected is true only when we've successfully sent our ConnectionInfo // to the broker, so if we haven't announced ourselves there's no need to // inform the broker of a remove, and if the transport is failed, why bother. if (connected.Value && !transportFailed.Value) { DisposeOf(ConnectionId); ShutdownInfo shutdowninfo = new ShutdownInfo(); transport.Oneway(shutdowninfo); } executor.Shutdown(); Tracer.Info("Connection: Disposing of the Transport."); transport.Dispose(); } catch (Exception ex) { Tracer.ErrorFormat("Error during connection close: {0}", ex); } finally { if (executor != null) { executor.Shutdown(); } this.transport = null; this.closed.Value = true; this.connected.Value = false; this.closing.Value = false; } } }
public void Close() { if (!this.closed.Value && !transportFailed.Value) { this.Stop(); } lock (myLock) { if (this.closed.Value) { return; } try { Tracer.InfoFormat("Connection[{0}]: Closing Connection Now.", this.ConnectionId); this.closing.Value = true; if (this.advisoryConsumer != null) { this.advisoryConsumer.Dispose(); this.advisoryConsumer = null; } lock (sessions.SyncRoot) { foreach (Session session in sessions) { session.Shutdown(); } } sessions.Clear(); if (this.tempDests.Count > 0) { // Make a copy of the destinations to delete, because the act of deleting // them will modify the collection. ActiveMQTempDestination[] tempDestsToDelete = new ActiveMQTempDestination[this.tempDests.Count]; this.tempDests.Values.CopyTo(tempDestsToDelete, 0); foreach (ActiveMQTempDestination dest in tempDestsToDelete) { dest.Delete(); } } // Connected is true only when we've successfully sent our ConnectionInfo // to the broker, so if we haven't announced ourselves there's no need to // inform the broker of a remove, and if the transport is failed, why bother. if (connected.Value && !transportFailed.Value) { DisposeOf(ConnectionId); ShutdownInfo shutdowninfo = new ShutdownInfo(); transport.Oneway(shutdowninfo); } executor.Shutdown(); Tracer.DebugFormat("Connection[{0}]: Disposing of the Transport.", this.ConnectionId); transport.Stop(); transport.Dispose(); } catch (Exception ex) { Tracer.ErrorFormat("Connection[{0}]: Error during connection close: {1}", ConnectionId, ex); } finally { if (executor != null) { executor.Shutdown(); } this.transport = null; this.closed.Value = true; this.connected.Value = false; this.closing.Value = false; } } }
/// <summary> /// Check and ensure that the connection objcet is connected. If it is not /// connected or is closed, a ConnectionClosedException is thrown. /// </summary> internal void CheckConnected() { if(closed.Value) { throw new ConnectionClosedException(); } if(!connected.Value) { DateTime timeoutTime = DateTime.Now + this.RequestTimeout; int waitCount = 1; while(true) { if(Monitor.TryEnter(checkConnectedLock)) { try { if(!connected.Value) { if(!this.userSpecifiedClientID) { this.info.ClientId = this.clientIdGenerator.GenerateId(); } try { if(null != transport) { // Send the connection and see if an ack/nak is returned. Response response = transport.Request(this.info, this.RequestTimeout); if(!(response is ExceptionResponse)) { connected.Value = true; if(this.watchTopicAdviosires) { ConsumerId id = new ConsumerId( new SessionId(info.ConnectionId, -1), Interlocked.Increment(ref this.consumerIdCounter)); this.advisoryConsumer = new AdvisoryConsumer(this, id); } } } } catch { } } } finally { Monitor.Exit(checkConnectedLock); } } if(connected.Value || DateTime.Now > timeoutTime) { break; } // Back off from being overly aggressive. Having too many threads // aggressively trying to connect to a down broker pegs the CPU. Thread.Sleep(5 * (waitCount++)); } if(!connected.Value) { throw new ConnectionClosedException(); } } }
public void Close() { if(!this.closed.Value && !transportFailed.Value) { this.Stop(); } lock(myLock) { if(this.closed.Value) { return; } try { Tracer.InfoFormat("Connection[{0}]: Closing Connection Now.", this.ConnectionId); this.closing.Value = true; if(this.advisoryConsumer != null) { this.advisoryConsumer.Dispose(); this.advisoryConsumer = null; } lock(sessions.SyncRoot) { foreach(Session session in sessions) { session.Shutdown(); } } sessions.Clear(); if(this.tempDests.Count > 0) { // Make a copy of the destinations to delete, because the act of deleting // them will modify the collection. ActiveMQTempDestination[] tempDestsToDelete = new ActiveMQTempDestination[this.tempDests.Count]; this.tempDests.Values.CopyTo(tempDestsToDelete, 0); foreach(ActiveMQTempDestination dest in tempDestsToDelete) { dest.Delete(); } } // Connected is true only when we've successfully sent our ConnectionInfo // to the broker, so if we haven't announced ourselves there's no need to // inform the broker of a remove, and if the transport is failed, why bother. if(connected.Value && !transportFailed.Value) { DisposeOf(ConnectionId); ShutdownInfo shutdowninfo = new ShutdownInfo(); transport.Oneway(shutdowninfo); } executor.Shutdown(); Tracer.DebugFormat("Connection[{0}]: Disposing of the Transport.", this.ConnectionId); transport.Dispose(); } catch(Exception ex) { Tracer.ErrorFormat("Connection[{0}]: Error during connection close: {1}", ConnectionId, ex); } finally { if(executor != null) { executor.Shutdown(); } this.transport = null; this.closed.Value = true; this.connected.Value = false; this.closing.Value = false; } } }