/// <summary>
 /// Adds the just saved message queue request to the response manager for watching.
 /// </summary>
 /// <param name="entities">The entities.</param>
 /// <param name="state">The state passed between the before save and after save callbacks.</param>
 public void OnAfterSave(IEnumerable <IEntity> entities, IDictionary <string, object> state)
 {
     foreach (var entity in entities)
     {
         MessageQueueResponseManager.Add(entity.As <MessageQueueRequest>());
     }
 }
        /// <summary>
        /// Removes the about to be deleted message queue request from the watch list of the response manager.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <param name="state">The state passed between the before delete and after delete callbacks.</param>
        /// <returns>True to cancel the delete operation; false otherwise.</returns>
        public bool OnBeforeDelete(IEnumerable <IEntity> entities, IDictionary <string, object> state)
        {
            foreach (var entity in entities)
            {
                MessageQueueResponseManager.Remove(entity.As <MessageQueueRequest>());
            }

            return(false);
        }
        /// <summary>
        /// Starts the global message queue response manager listening for the responses to any registered requests.
        /// </summary>
        public static void Start()
        {
            try
            {
                if (_instance != null)
                {
                    return;
                }

                var config = ConfigurationSettings.GetRabbitMqConfigurationSection();
                if (config == null)
                {
                    return;
                }

                var settings = config.RabbitMq;
                if (settings == null)
                {
                    return;
                }

                if (string.IsNullOrEmpty(settings.HostName))
                {
                    return;
                }

                lock (Sync)
                {
                    if (_instance == null)
                    {
                        _instance = new MessageQueueResponseManager();
                        _instance.StartInternal();
                    }
                }

                var maxWait = TimeSpan.FromMinutes(1);
                var sw      = Stopwatch.StartNew();
                while (!_instance.IsListening && sw.Elapsed < maxWait)
                {
                    // wait... but only so long
                    Thread.Sleep(100);
                }

                if (!_instance.IsListening)
                {
                    EventLog.Application.WriteError("Message Queue Response Manager: Thread did not start in time. Exiting.");
                }

                using (new AdministratorContext())
                {
                    var requests = Entity.GetInstancesOfType <MessageQueueRequest>();
                    foreach (var request in requests)
                    {
                        //
                        // TODO: ? Maybe if the persisted entity is too old, it should just be deleted?
                        //

                        Add(request);
                    }
                }
            }
            catch (Exception e)
            {
                EventLog.Application.WriteError("Unexpected failure starting the message queue response manager. {0}", e);
            }
        }