/// <summary>
        /// This method recalculate the new poll chain for the listener and deadletter listener collection
        /// and swaps the new collection in atomically. This is done on a schedule to ensure that the collection priority
        /// does not become stale, and that the most active clients receive the most amount of attention.
        /// </summary>
        /// <param name="rebuild">This boolean property specifies whether the collection should be rebuilt or reordered. True rebuilds the collection.</param>
        private Task ListenersPriorityRecalculate(bool rebuild = true)
        {
            if (Status != ServiceStatus.Running)
            {
                return(Task.FromResult(0));
            }

            if (!rebuild && mClientCollection != null)
            {
                mClientCollection.Reprioritise();
            }

            try
            {
                //We do an atomic switch to add in a new priority list.
                var newColl = new ClientPriorityCollection(mListener
                                                           , mResourceTracker
                                                           , mPolicy.ListenerClientPollAlgorithm
                                                           , Interlocked.Increment(ref mListenersPriorityIteration));

                //Switch out the old collection for the new collection atomically
                var oldColl = Interlocked.Exchange(ref mClientCollection, newColl);

                //Close the old collection, note that it will be null the first time.
                oldColl?.Close();

                Collector?.LogMessage(LoggingLevel.Trace, $"ListenersPriorityRecalculate completed {mListenersPriorityIteration}.");
            }
            catch (Exception ex)
            {
                Collector?.LogException("ListenersPriorityCalculate failed. Using the old collection.", ex);
            }

            return(Task.FromResult(0));
        }
 /// <summary>
 /// This method starts the listener and prioritises the clients.
 /// </summary>
 public override void Start()
 {
     base.Start();
     Clients = new ClientPriorityCollection(new IListener[] { Service }.ToList(), null, PollAlgorithm, 0);
 }