public void Abort()
            {
                lock (connectionFactory)
                {
                    aborting = true;

                    consumer = null;

                    model?.Abort();
                    model = null;

                    connection?.Abort();
                    connection = null;
                }
            }
Ejemplo n.º 2
0
 private void AbortIfErrors(IModel model, object userData)
 {
     if (model.HasErrors())
     {
         model.Abort();
     }
 }
Ejemplo n.º 3
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _cancellationSource.Cancel();
                _cancellationSource.Dispose();

                if (_model != null)
                {
                    if (_model.IsOpen)
                    {
                        _model.Abort();
                    }
                    _model.Dispose();
                }

                if (_connection != null)
                {
                    if (_connection.IsOpen)
                    {
                        _connection.Close();
                    }
                    _connection.Dispose();
                }
            }

            _disposed = true;
        }
        private void DisconnectFromRabbitMQ()
        {
            _isConsuming = false;
            try
            {
                if (_rabbitMQSubscription != null)
                {
                    _rabbitMQSubscription.Close();
                }
            }
            catch { }

            try
            {
                if (_rabbitMQConnection != null)
                {
                    _rabbitMQConnection.Close();
                }
            }
            catch { }

            try
            {
                if (_rabbitMQModel != null)
                {
                    _rabbitMQModel.Abort();
                }
            }
            catch { }

            _rabbitMQQueueName = null;
        }
Ejemplo n.º 5
0
        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;
            }
        }
Ejemplo n.º 6
0
        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;
            }
        }
Ejemplo n.º 7
0
        private void CreateChannel()
        {
            lock (ConsumerLock)
            {
                if (_channel != null && _channel.IsOpen && _connection.IsOpen)
                {
                    return;
                }

                if (_channel != null && _channel.IsOpen)
                {
                    _channel.Abort();
                }
                if (!_connection.IsOpen)
                {
                    _connection = _factory.CreateConnection();
                }

                _channel = _connection.CreateModel();
                _channel.BasicQos(0, 1, false);

                var properties = _channel.CreateBasicProperties();
                properties.SetPersistent(true);

                // QueueDeclare is idempotent
                foreach (var queue in _queues)
                {
                    _channel.QueueDeclare(queue, true, false, false, null);
                }
            }
        }
Ejemplo n.º 8
0
 public static void stop()
 {
     channel.Close(200, "Goodbye");
     channel.Abort();
     connection.Close();
     connection.Abort();
 }
Ejemplo n.º 9
0
        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);
            }
        }
Ejemplo n.º 10
0
        private void ShutdownAmqp(object sender, ShutdownEventArgs reason)
        {
            // I can't make this NOT hang when RMQ goes down
            // and then a log message is sent...
            var connection = sender as IConnection;

            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)
                {
                    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, {0}", e);
            }
        }
Ejemplo n.º 11
0
        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;
            }
        }
Ejemplo n.º 12
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 13
0
 public void Dispose()
 {
     lock (_lock)
         if (_model != null)
         {
             _model.Abort();
         }
 }
Ejemplo n.º 14
0
        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);
            }
        }
Ejemplo n.º 15
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         Channel?.Abort();
         Channel?.Dispose();
         Channel = null;
     }
 }
        private void Close()
        {
            isStopped = true;
            publishModel?.Abort();
            connection?.Abort();

            connection   = null;
            publishModel = null;
        }
Ejemplo n.º 17
0
 /// <inheritdoc />
 public void EndConsume()
 {
     if (_channel != null)
     {
         _channel.Abort();
         _channel.Dispose();
     }
     _connection?.Dispose();
 }
Ejemplo n.º 18
0
        public TResponse CallRemoteProcedure <TResponse>(byte[] requestBytes, string channelName)
        {
            if (requestBytes == null)
            {
                throw new ArgumentNullException("Request bytes can't be null!");
            }

            using (IModel channel = Connection.CreateModel())
            {
                string replyQueueName           = channel.QueueDeclare().QueueName;
                EventingBasicConsumer _consumer = new EventingBasicConsumer(channel);

                string correlationId = Guid.NewGuid().ToString();

                IBasicProperties props = channel.CreateBasicProperties();
                props.CorrelationId = correlationId;
                props.ReplyTo       = replyQueueName;

                BlockingCollection <byte[]> respQueue = new BlockingCollection <byte[]>();

                _consumer.Received += (model, ea) =>
                {
                    if (ea.BasicProperties.CorrelationId == correlationId)
                    {
                        respQueue.Add(ea.Body.ToArray());
                    }
                };


                channel.BasicPublish("", channelName, props, requestBytes);
                channel.BasicConsume(_consumer, replyQueueName, true);

                byte[] data = new byte[0];

                if (Timeout == 0)
                {
                    data = respQueue.Take();
                }
                else
                {
                    DateTime started = DateTime.Now;
                    DateTime expired = started.AddMilliseconds(Timeout);

                    while (!respQueue.TryTake(out data))
                    {
                        if (DateTime.Now > expired)
                        {
                            channel.Abort();
                            channel.Close();
                            throw new MqRpcResponseTimeout();
                        }
                    }
                }
                return(data.ToObject <TResponse>());
            }
        }
 public virtual void Dispose()
 {
     if (disposed)
     {
         return;
     }
     channel.Abort();
     channel.Dispose();
     disposed = true;
 }
Ejemplo n.º 20
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                Channel?.Abort();
                Channel?.Dispose();
                Channel = null;

                new RMQMessageGatewayConnectionPool(Connection.Name, Connection.Heartbeat).RemoveConnection(_connectionFactory);
            }
        }
Ejemplo n.º 21
0
 public void Dispose()
 {
     if (Connection != null)
     {
         Connection.Close();
     }
     if (Model != null)
     {
         Model.Abort();
     }
 }
Ejemplo n.º 22
0
 public void Dispose()
 {
     if (Connection != null && Connection.IsOpen)
     {
         Connection.Close();
     }
     if (Channel != null)
     {
         Channel.Abort();
     }
 }
Ejemplo n.º 23
0
 public void Dispose()
 {
     if ((_connection != null) && _connection.IsOpen)
     {
         _connection.Close();
     }
     if (_model != null)
     {
         _model.Abort();
     }
 }
Ejemplo n.º 24
0
 public void Dispose()
 {
     isConsuming = false;
     if (Connection != null)
     {
         Connection.Close();
     }
     if (Model != null)
     {
         Model.Abort();
     }
 }
Ejemplo n.º 25
0
 public void Dispose()
 {
     if (connection != null && connection.IsOpen)
     {
         connection.Close();
     }
     connection.Abort();
     if (channel != null && channel.IsOpen)
     {
         channel.Close();
     }
     channel.Abort();
 }
Ejemplo n.º 26
0
    public void CallToChildThread()
    {
        Console.WriteLine("Enter 'end' to stop");
        string input = "";

        while (input != "end")
        {
            input = Console.ReadLine();
        }
        _kinectSensor.Close();
        _channel.Abort();
        connection.Close();
    }
Ejemplo n.º 27
0
 public void Dispose()
 {
     if (_consumer != null)
     {
         _consumer.Received -= ConsumerOnReceived;
         _consumer           = null;
     }
     if (_model != null)
     {
         _model.Abort();
         _model = null;
     }
 }
Ejemplo n.º 28
0
        //
        // Channels
        //

        internal void WithTemporaryModel(Action <IModel> action)
        {
            IModel model = _conn.CreateModel();

            try
            {
                action(model);
            }
            finally
            {
                model.Abort();
            }
        }
Ejemplo n.º 29
0
        protected void WithTemporaryModel(Action <IModel> action)
        {
            IModel model = Conn.CreateModel();

            try
            {
                action(model);
            }
            finally
            {
                model.Abort();
            }
        }
Ejemplo n.º 30
0
        public void StopAndDispose()
        {
            if (_connection != null)
            {
                _connection.Close();
            }

            if (_channel != null && _channel.IsOpen)
            {
                _channel.Abort();
            }
            GC.SuppressFinalize(this);
        }
 /// <summary>
 /// Called when [message].
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="channel">The channel.</param>
 /// <remarks></remarks>
 public void OnMessage(Message message, IModel channel)
 {
     var value = Encoding.UTF8.GetString(message.Body);
     logger.Debug("Receiving: " + value);
     if (this.failed.CompareAndSet(false, true))
     {
         // intentional error (causes exception on connection thread):
         channel.Abort();
     }
     else
     {
         this.latch.Signal();
     }
 }
 /// <summary>Called when [message].</summary>
 /// <param name="message">The message.</param>
 /// <param name="channel">The channel.</param>
 public void OnMessage(Message message, IModel channel)
 {
     var value = Encoding.UTF8.GetString(message.Body);
     Logger.Debug(m => m("Receiving: {0}", value));
     if (this.failed.CompareAndSet(false, true))
     {
         // intentional error (causes exception on connection thread):
         channel.Abort();
     }
     else
     {
         if (this.latch.CurrentCount > 0)
         {
             this.latch.Signal();
             Logger.Debug(m => m("Latch Count: {0}", this.latch.CurrentCount));
         }
     }
 }
Ejemplo n.º 33
0
		protected virtual void Close(IModel channel, ConnectionState state, Exception exception = null)
		{
			this.CurrentState = ConnectionState.Closing;

			if (channel != null)
			{
				Log.Debug("Aborting operations on temporary channel.");
				channel.Abort();
			}

			if (this.connection != null)
			{
				Log.Debug("Blocking up to {0} ms before forcing the existing connection to close.", this.shutdownTimeout);

				// calling connection.TryDispose() can thrown while connection.Abort() closes without throwing
				this.connection.Abort(this.shutdownTimeout);
			}

			this.connection = null;
			this.CurrentState = state;

			if (exception != null)
				throw new ChannelConnectionException(exception.Message, exception);
		}
Ejemplo n.º 34
0
		protected virtual void Close(IModel channel, ConnectionState state, Exception exception = null)
		{
			this.CurrentState = ConnectionState.Closing;

			if (channel != null)
			{
				Log.Debug("Aborting operations on temporary channel.");
				channel.Abort();
			}

			this.TryAbortConnection();
			this.connection = null;
			this.CurrentState = state;

			if (exception != null)
				throw new ChannelConnectionException(exception.Message, exception);
		}