protected void EnsureConnectionExists()
        {
            lock (_connectionLock)
            {
                if (Connection != null)
                {
                    return;
                }

                Logger.LogDebug("Connecting...");

                try
                {
                    Connection = _connectionFactory.CreateConnection(_rabbitMqSettings.HostNames, $"MassiveJobs.NET/{GetEntryFileName()}");

                    Connection.CallbackException   += ConnectionOnCallbackException;
                    Connection.ConnectionBlocked   += ConnectionOnConnectionBlocked;
                    Connection.ConnectionUnblocked += ConnectionOnConnectionUnblocked;
                    Connection.ConnectionShutdown  += ConnectionOnConnectionShutdown;

                    ModelPool = new ModelPool(Connection, 2, Logger);

                    var model = ModelPool.Get();
                    try
                    {
                        DeclareTopology(model.Model);
                    }
                    finally
                    {
                        ModelPool.Return(model);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Failed connection create");
                    throw;
                }
            }

            Logger.LogWarning("Connected");
        }
        public void Publish(string routingKey, IEnumerable <RawMessage> messages, TimeSpan timeout)
        {
            EnsureConnectionExists();

            var poolEntry = ModelPool.Get();

            try
            {
                foreach (var msg in messages)
                {
                    poolEntry.BasicProperties.Type       = msg.TypeTag;
                    poolEntry.BasicProperties.Persistent = msg.IsPersistent;
                    poolEntry.Model.BasicPublish(_rmqSettings.ExchangeName, routingKey, poolEntry.BasicProperties, msg.Body);
                }

                poolEntry.Model.WaitForConfirmsOrDie(timeout);
            }
            finally
            {
                ModelPool.Return(poolEntry);
            }
        }
        protected void CloseConnection()
        {
            lock (_connectionLock)
            {
                if (Connection == null)
                {
                    return;
                }

                ModelPool.SafeDispose(Logger);
                ModelPool = null;

                Connection.CallbackException   -= ConnectionOnCallbackException;
                Connection.ConnectionBlocked   -= ConnectionOnConnectionBlocked;
                Connection.ConnectionUnblocked -= ConnectionOnConnectionUnblocked;
                Connection.ConnectionShutdown  -= ConnectionOnConnectionShutdown;

                Connection.SafeClose(Logger);
                Connection = null;

                Logger.LogWarning("Connection closed and set to null");
            }
        }