internal void Abort(Exception e) { lock (ThisLock) { if (isClosed) { return; } isClosed = true; } try { connection.Abort(); } finally { if (closedCallback != null) { closedCallback(this); } if (connectionDequeuedCallback != null) { connectionDequeuedCallback(); } } }
protected override bool PublishInternal(CronusMessage message) { try { if (ReferenceEquals(null, connection) || connection.IsOpen == false) { lock (connectionFactory) { if (ReferenceEquals(null, connection) || connection.IsOpen == false) { connection?.Abort(); connection = connectionFactory.CreateConnection(); connection.AutoClose = false; } } } if (publishModel == null || publishModel.IsClosed) { publishModel = connection.CreateModel(); } IBasicProperties props = publishModel.CreateBasicProperties(); props.Headers = new Dictionary <string, object>() { { message.Payload.GetType().GetContractId(), string.Empty } }; props.Persistent = true; props.Priority = 9; byte[] body = this.serializer.SerializeToBytes(message); var publishDelayInMiliseconds = message.GetPublishDelay(); if (publishDelayInMiliseconds < 1000) { var exchangeName = RabbitMqNamer.GetExchangeName(message.Payload.GetType()); publishModel.BasicPublish(exchangeName, string.Empty, false, props, body); } else { var exchangeName = RabbitMqNamer.GetExchangeName(message.Payload.GetType()) + ".Scheduler"; props.Headers.Add("x-delay", message.GetPublishDelay()); publishModel.BasicPublish(exchangeName, string.Empty, false, props, body); } return(true); } catch (Exception ex) { publishModel?.Abort(); connection?.Abort(); connection = null; publishModel = null; return(false); } }
public void Abort() { lock (connectionFactory) { model?.Abort(); connection?.Abort(); connection = null; model = null; consumer = null; } }
private void SnapshotConnection(IConnection upgradedConnection, IConnection rawConnection, bool isConnectionFromPool) { lock (ThisLock) { if (_closed) { upgradedConnection.Abort(); // cleanup our pool if necessary if (isConnectionFromPool) { _connectionPool.ReturnConnection(_connectionKey, rawConnection, false, TimeSpan.Zero); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CommunicationObjectAbortedException( string.Format(SRServiceModel.OperationAbortedDuringConnectionEstablishment, _via))); } else { _upgradedConnection = upgradedConnection; _rawConnection = rawConnection; _isConnectionFromPool = isConnectionFromPool; } } }
void SnapshotConnection(IConnection upgradedConnection, IConnection rawConnection, bool isConnectionFromPool) { lock (ThisLock) { if (closed) { upgradedConnection.Abort(); // cleanup our pool if necessary if (isConnectionFromPool) { this.connectionPool.ReturnConnection(this.connectionKey, rawConnection, false, TimeSpan.Zero); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CommunicationObjectAbortedException( SR.GetString(SR.OperationAbortedDuringConnectionEstablishment, this.via))); } else { this.upgradedConnection = upgradedConnection; this.rawConnection = rawConnection; this.isConnectionFromPool = isConnectionFromPool; } } }
internal static void CloseNoThrow(IConnection connection, TimeSpan timeout) { bool flag = false; try { connection.Close(timeout, false); flag = true; } catch (TimeoutException exception) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information); } } catch (CommunicationException exception2) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information); } } finally { if (!flag) { connection.Abort(); } } }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (consumer != null && channel != null) { channel.BasicCancel(consumer.ConsumerTags.FirstOrDefault()); consumer = null; } if (channel != null) { channel.Abort(); channel.Dispose(); channel = null; } if (connection != null) { connection.Abort(); connection.Dispose(); connection = null; } } disposedValue = true; } }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (consumer != null && channel != null) { foreach (var tag in consumer.ConsumerTags) { channel.BasicCancel(tag); } consumer = null; } if (channel != null) { channel.Abort(); channel.Dispose(); channel = null; } if (connection != null) { connection.Abort(); connection.Dispose(); connection = null; } } disposedValue = true; } }
public void Close() { m_closed.Set(); lock (this) { if (m_connection != null) m_connection.Abort(); } OnStateChange(ConnectorState.Disconnected); }
public static void stop() { channel.Close(200, "Goodbye"); channel.Abort(); connection.Close(); connection.Abort(); }
private void ShutdownAmqp(IConnection connection, ShutdownEventArgs reason) { // I can't make this NOT hang when RMQ goes down // and then a log message is sent... //try //{ // if (_Model != null && _Model.IsOpen) // _Model.Abort(); //_Model.Close(); //} //catch (Exception e) //{ // InternalLogger.Error("could not close model", e); //} try { if (connection != null && connection.IsOpen) { connection.ConnectionShutdown -= ShutdownAmqp; connection.Close(reason.ReplyCode, reason.ReplyText, 1000); connection.Abort(1000); // you get 2 seconds to shut down! } } catch (Exception e) { InternalLogger.Error("could not close connection", e); } }
protected override void ReturnConnectionIfNecessary(bool abort, CancellationToken token) { var timeout = TimeoutHelper.GetOriginalTimeout(token); IConnection localConnection = null; if (sessionReader != null) { lock (ThisLock) { localConnection = sessionReader.GetRawConnection(); } } if (localConnection != null) { if (abort) { localConnection.Abort(); } else { connectionDemuxer.ReuseConnection(localConnection, timeout); } connectionDemuxer = null; } }
void CleanUpVirtualHost(IRabbitMqHost host) { try { _nameFormatter = new RabbitMqMessageNameFormatter(); ConnectionFactory connectionFactory = host.Settings.GetConnectionFactory(); using (IConnection connection = connectionFactory.CreateConnection()) using (IModel model = connection.CreateModel()) { model.ExchangeDelete("input_queue"); model.QueueDelete("input_queue"); model.ExchangeDelete("input_queue_skipped"); model.QueueDelete("input_queue_skipped"); model.ExchangeDelete("input_queue_error"); model.QueueDelete("input_queue_error"); model.ExchangeDelete("input_queue_delay"); model.QueueDelete("input_queue_delay"); OnCleanupVirtualHost(model); model.Abort(200, "Cleanup complete"); connection.Abort(200, "Cleanup complete"); } } catch (Exception exception) { Console.WriteLine(exception); } }
internal static void CloseNoThrow(IConnection connection, TimeSpan timeout) { bool flag = false; try { try { connection.Close(timeout); flag = true; } catch (TimeoutException timeoutException) { Fx.Exception.TraceHandled(timeoutException, "ConnectionUtiltities.CloseNoThrow", connection.Activity); } catch (CommunicationException communicationException) { Fx.Exception.TraceHandled(communicationException, "ConnectionUtiltities.CloseNoThrow", connection.Activity); } } finally { if (!flag) { connection.Abort(); } } }
public void Dispose() { isConsuming = false; if (consumer != null) { consumer = null; } if (Connection != null) { Connection.Close(); } if (channel != null) { channel.Abort(); } if (connectionFactory != null) { connectionFactory = null; } if (Connection != null) { Connection.Abort(); } if (e != null) { e = null; } }
internal static void CloseNoThrow(IConnection connection, TimeSpan timeout) { bool success = false; try { // TODO: Change IConnection.Close to async and switch to async here connection.Close(timeout, false); success = true; } catch (TimeoutException e) { DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); } catch (CommunicationException e) { DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); } finally { if (!success) { connection.Abort(); } } }
private void ShutdownAmqp(IConnection connection, ShutdownEventArgs reason) { // I can't make this NOT hang when RMQ goes down // and then a log message is sent... try { if (_Model != null && _Model.IsOpen && reason.ReplyCode != Constants.ChannelError && reason.ReplyCode != Constants.ConnectionForced) { _Model.Abort(); //_Model.Close(); } } catch (Exception e) { InternalLogger.Error("could not close model, {0}", e); } try { if (connection != null && connection.IsOpen) { AddConnectionShutdownDelegate(connection); connection.Close(reason.ReplyCode, reason.ReplyText, 1000); connection.Abort(1000); // you get 2 seconds to shut down! } } catch (Exception e) { InternalLogger.Error("could not close connection, {0}", e); } }
internal static void CloseNoThrow(IConnection connection, TimeSpan timeout) { bool success = false; try { connection.Close(timeout, false); success = true; } catch (TimeoutException e) { if (TD.CloseTimeoutIsEnabled()) { TD.CloseTimeout(e.Message); } DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); } catch (CommunicationException e) { DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); } finally { if (!success) { connection.Abort(); } } }
private void ShutdownAmqp(IConnection connection, ShutdownEventArgs reason) { if (reason.ReplyCode != 200 /* Constants.ReplySuccess*/) { InternalLogger.Warn("RabbitMQTarget(Name={0}): Connection shutdown. ReplyCode={1}, ReplyText={2}", Name, reason.ReplyCode, reason.ReplyText); } else { InternalLogger.Info("RabbitMQTarget(Name={0}): Connection shutdown. ReplyCode={1}, ReplyText={2}", Name, reason.ReplyCode, reason.ReplyText); } lock (_sync) { if (connection != null) { IModel model = null; if (ReferenceEquals(connection, _Connection)) { model = _Model; _Connection = null; _Model = null; } try { if (reason.ReplyCode == 200 /* Constants.ReplySuccess*/ && connection.IsOpen) { model?.Close(); } else { model?.Abort(); // Abort is close without throwing Exceptions } } catch (Exception e) { InternalLogger.Error(e, "RabbitMQTarget(Name={0}): Could not close model: {1}", Name, e.Message); } try { // you get 1.5 seconds to shut down! if (reason.ReplyCode == 200 /* Constants.ReplySuccess*/ && connection.IsOpen) { connection.Close(reason.ReplyCode, reason.ReplyText, TimeSpan.FromMilliseconds(1500)); } else { connection.Abort(reason.ReplyCode, reason.ReplyText, TimeSpan.FromMilliseconds(1500)); } } catch (Exception e) { InternalLogger.Error(e, "RabbitMQTarget(Name={0}): Could not close connection: {1}", Name, e.Message); } } } }
public void Disconnect() { if (connection.IsOpen) { connection.Abort(); Log.Info("Connection aborted."); } }
public void Dispose() { if (_connection == null) { return; } _connection.Abort(500); _connection = null; }
[TearDown] public void Disconnect() { try { Channel.ExchangeDelete(exchangeName); } catch (OperationInterruptedException) {} Connection.Abort(); }
public void Abort() { if (_connection == null) { return; } lock (_syncRoot) { if (_connection == null) { return; } _connection.Abort(); _diagnosticService.Emit(new RabbitMQEventBuilder(this, RabbitMQEventType.RabbitMQConnectionAborted) { Detail = "Managed connection ID " + ManagedConnectionId + " aborted" }.Build()); } }
void ICredentialsAcces.Logout() { _connection.Abort(); _connection = null; _channel = null; _userName = null; }
private void ReleaseConnection(bool abort, TimeSpan timeout) { string connectionKey; IConnection upgradedConnection; IConnection rawConnection; lock (this.ThisLock) { this.closed = true; connectionKey = this.connectionKey; upgradedConnection = this.upgradedConnection; rawConnection = this.rawConnection; this.upgradedConnection = null; this.rawConnection = null; } if (upgradedConnection != null) { try { if (this.isConnectionFromPool) { this.connectionPool.ReturnConnection(connectionKey, rawConnection, !abort, timeout); } else if (abort) { upgradedConnection.Abort(); } else { this.connectionPool.AddConnection(connectionKey, rawConnection, timeout); } } catch (CommunicationException exception) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information); } upgradedConnection.Abort(); } } }
/// <summary> /// The connect. /// </summary> /// <param name="token"> /// The token. /// </param> private void Connect(CancellationToken token) { this.logger.InfoFormat("Connecting to RabbitMQ using [{0}].", this.Configuration.ConnectionString); var clientProperties = new Dictionary <string, object> { { "Endpoint", this.Endpoint.Address }, { "Machine", Environment.MachineName }, { "Location", Path.GetDirectoryName( Assembly.GetExecutingAssembly(). CodeBase) } }; var connectionFactory = new ConnectionFactory { Uri = this.Configuration.ConnectionString, ClientProperties = clientProperties, RequestedConnectionTimeout = 3000, // 3 s // RequestedHeartbeat = 5 }; int retryCount = 0; while (!token.IsCancellationRequested) { IConnection newConnection = null; try { newConnection = connectionFactory.CreateConnection(); newConnection.ConnectionShutdown += this.DisconnectEventHandler; this.Connection = newConnection; this.OnConnected(); return; } catch (Exception ex) { var secondsToRetry = Math.Min(10, retryCount); this.logger.WarnFormat("Unable to connect to RabbitMQ. Retrying in {0} seconds...", ex, secondsToRetry); if (newConnection != null) { newConnection.ConnectionShutdown -= this.DisconnectEventHandler; newConnection.Abort(500); } Thread.Sleep(TimeSpan.FromSeconds(secondsToRetry)); retryCount++; } } }
public void Start() { int oldValue = Interlocked.CompareExchange(ref _volatileRunFlag, Running, Stopped); if (Running == oldValue) { throw new InvalidOperationException("Can not call start twice concurrently on the same session"); } do { try { _streamConnection = _httpInvoker.Connect(_baseUri, new StreamRequest(), _sessionId); _eventStreamHandler.ParseEventStream(_streamConnection.GetTextReader()); } catch (UnexpectedHttpStatusCodeException e) { if (Running == Thread.VolatileRead(ref _volatileRunFlag) && HttpStatusCode.Forbidden == e.StatusCode) { if (null != EventStreamSessionDisconnected) { EventStreamSessionDisconnected(); } Interlocked.CompareExchange(ref _volatileRunFlag, Stopped, Running); } else if (Running == Thread.VolatileRead(ref _volatileRunFlag) && null != EventStreamFailed) { EventStreamFailed(e); } } catch (Exception e) { if (Running == Thread.VolatileRead(ref _volatileRunFlag) && null != EventStreamFailed) { if (null != EventStreamSessionDisconnected) { EventStreamSessionDisconnected(); } EventStreamFailed(e); } } finally { if (null != _streamConnection) { _streamConnection.Abort(); _streamConnection = null; } } } while (_restartStreamOnFailure && Running == Thread.VolatileRead(ref _volatileRunFlag)); }
public static void Stop() { foreach (var model in models) { model.Value.Abort(); model.Value.Close(); } models = new Dictionary <string, IModel>(); connection.Abort(); connection.Close(); factory = null; }
public void Dispose() { if (connection != null && connection.IsOpen) { connection.Close(); } connection.Abort(); if (channel != null && channel.IsOpen) { channel.Close(); } channel.Abort(); }
private bool HandleOnVia(DuplicateContext duplicateContext) { if (this.onDuplicatedViaCallback != null) { lock (this.ThisLock) { if (this.onDuplicatedViaCallback == null) { return(true); } if (this.onDuplicatedViaCallback != null) { try { int num = this.onDuplicatedViaCallback(duplicateContext.Via); this.connectionBufferSize = num; this.onDuplicatedViaCallback = null; } catch (Exception exception) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information); } string faultCode = null; if (exception is ServiceActivationException) { faultCode = "http://schemas.microsoft.com/ws/2006/05/framing/faults/ServiceActivationFailed"; } else if (exception is EndpointNotFoundException) { faultCode = "http://schemas.microsoft.com/ws/2006/05/framing/faults/EndpointNotFound"; } IConnection connection = this.BuildConnectionFromData(duplicateContext, 0x2000); if (faultCode != null) { this.SendFault(connection, faultCode); return(false); } connection.Abort(); if (!(exception is CommunicationObjectAbortedException)) { throw; } return(false); } } } } return(true); }
private void SnapshotConnection(IConnection upgradedConnection, IConnection rawConnection, bool isConnectionFromPool) { lock (this.ThisLock) { if (this.closed) { upgradedConnection.Abort(); if (isConnectionFromPool) { this.connectionPool.ReturnConnection(this.connectionKey, rawConnection, false, TimeSpan.Zero); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationObjectAbortedException(System.ServiceModel.SR.GetString("OperationAbortedDuringConnectionEstablishment", new object[] { this.via }))); } this.upgradedConnection = upgradedConnection; this.rawConnection = rawConnection; this.isConnectionFromPool = isConnectionFromPool; } }
internal static void CloseNoThrow(IConnection connection, TimeSpan timeout) { bool success = false; try { connection.Close(timeout, false); success = true; } catch (TimeoutException) { } catch (CommunicationException) { } finally { if (!success) { connection.Abort(); } } }
internal static void SendFault(IConnection connection, string faultString, byte[] drainBuffer, TimeSpan sendTimeout, int maxRead) { if (TD.ConnectionReaderSendFaultIsEnabled()) { TD.ConnectionReaderSendFault(faultString); } EncodedFault encodedFault = new EncodedFault(faultString); TimeoutHelper timeoutHelper = new TimeoutHelper(sendTimeout); try { connection.Write(encodedFault.EncodedBytes, 0, encodedFault.EncodedBytes.Length, true, timeoutHelper.RemainingTime()); connection.Shutdown(timeoutHelper.RemainingTime()); } catch (CommunicationException e) { DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } catch (TimeoutException e) { if (TD.SendTimeoutIsEnabled()) { TD.SendTimeout(e.Message); } DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } // make sure we read until EOF or a quota is hit int read = 0; int readTotal = 0; for (;;) { try { read = connection.Read(drainBuffer, 0, drainBuffer.Length, timeoutHelper.RemainingTime()); } catch (CommunicationException e) { DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } catch (TimeoutException e) { if (TD.SendTimeoutIsEnabled()) { TD.SendTimeout(e.Message); } DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); connection.Abort(); return; } if (read == 0) break; readTotal += read; if (readTotal > maxRead || timeoutHelper.RemainingTime() <= TimeSpan.Zero) { connection.Abort(); return; } } ConnectionUtilities.CloseNoThrow(connection, timeoutHelper.RemainingTime()); }
private void SnapshotConnection(IConnection upgradedConnection, IConnection rawConnection, bool isConnectionFromPool) { lock (ThisLock) { if (_closed) { upgradedConnection.Abort(); // cleanup our pool if necessary if (isConnectionFromPool) { _connectionPool.ReturnConnection(_connectionKey, rawConnection, false, TimeSpan.Zero); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CommunicationObjectAbortedException( SR.Format(SR.OperationAbortedDuringConnectionEstablishment, _via))); } else { _upgradedConnection = upgradedConnection; _rawConnection = rawConnection; _isConnectionFromPool = isConnectionFromPool; } } }
private void ShutdownAmqp(IConnection connection, ShutdownEventArgs reason) { // I can't make this NOT hang when RMQ goes down // and then a log message is sent... try { if (_Model != null && _Model.IsOpen && reason.ReplyCode != Constants.ChannelError && reason.ReplyCode != Constants.ConnectionForced) _Model.Abort(); //_Model.Close(); } catch (Exception e) { InternalLogger.Error("could not close model", e); } try { if (connection != null && connection.IsOpen) { connection.ConnectionShutdown -= ShutdownAmqp; connection.Close(reason.ReplyCode, reason.ReplyText, 1000); connection.Abort(1000); // you get 2 seconds to shut down! } } catch (Exception e) { InternalLogger.Error("could not close connection", e); } }
public void Start() { Int32 oldValue = Interlocked.CompareExchange(ref _runFlag, Running, Stopped); if (Running == oldValue) { throw new InvalidOperationException("Can not call start twice concurrently on the same session"); } do { try { _streamConnection = _httpInvoker.Connect(_baseUri, new StreamRequest(), _sessionId); _eventStreamHandler.ParseEventStream(_streamConnection.GetTextReader()); } catch (UnexpectedHttpStatusCodeException e) { if (Running == _runFlag && HttpStatusCode.Forbidden == e.StatusCode) { if (null != EventStreamSessionDisconnected) { EventStreamSessionDisconnected(); } Interlocked.CompareExchange(ref _runFlag, Stopped, Running); } else if (Running == _runFlag && null != EventStreamFailed) { EventStreamFailed(e); } } catch (Exception e) { if (Running == _runFlag && null != EventStreamFailed) { EventStreamFailed(e); } } finally { if (null != _streamConnection) { _streamConnection.Abort(); _streamConnection = null; } } } while (_restartStreamOnFailure && Running == _runFlag); }