Example #1
0
        private AzureQueuePicker InstantiateQueueMonitor()
        {
            if (this.OptionalQueueMonitorInjector != null)
            {
                AzureQueuePicker qmon = this.OptionalQueueMonitorInjector(this);
                qmon.Subscribe(this.RelayMessages);
                return(qmon);
            }

            return(this.SourceQueue.Queue.Subscribe(
                       this.RelayMessages,
                       this.SourceQueue.MessageInivisibilityTimeMillisec,
                       this.SourceQueue.MaxDelayBetweenDequeueAttemptsSeconds,
                       useAopProxyWhenAccessingQueue: false
                       ));
        }
        /// <summary>
        ///     Enable pub/sub pattern for regular Azure queues instead of polling by
        ///     launching polling loop that invokes message-processing callback when messages arrived.
        ///     This is an alternative to using blocking WaitForMessage() method.
        ///     Uses smart polling with delays between attempts to dequeue messages, ensuring lows CPU utilization and not leaking
        ///     money for Azure storage transactions.
        /// </summary>
        /// <param name="queue">Azure queue to dequeue messages from.</param>
        /// <param name="messageProcessCallback">
        ///     Optional message-processing delegate. If null, Process() method must be overridden
        ///     in a subclass.
        /// </param>
        /// <param name="messageInvisibilityTimeMillisec">
        ///     Time for queue element to be processed. If not deleted from queue within
        ///     this time, message is automatically placed back in the queue.
        /// </param>
        /// <param name="maxCheckDelaySeconds">Maximum delay, in seconds, between attempts to dequeue messages.</param>
        /// <param name="useAopProxyWhenAccessingQueue">
        ///     Set to true to use Aspectacular AOP proxy with process-wide set of aspects,
        ///     to call queue access functions. Set to false to call queue operations directly.
        /// </param>
        /// <returns>
        ///     IDisposable queue wrapper object to be used later for calling Stop() or Dispose() methods to terminate queue
        ///     polling.
        /// </returns>
        /// <remarks>
        ///     This method starts polling thread, on which both polling function and message processing functions are called.
        ///     This means that next attempt to dequeue messages won't occur until message processing callback function is done.
        ///     Payload processing callback may start its own thread(s) to process messages asynchronously and quickly return
        ///     control to the polling thread.
        /// </remarks>
        public static AzureQueuePicker Subscribe(this CloudQueue queue, Action <CloudQueue, IList <CloudQueueMessage> > messageProcessCallback,
                                                 int messageInvisibilityTimeMillisec, int maxCheckDelaySeconds = 60, bool useAopProxyWhenAccessingQueue = true)
        {
            if (queue == null)
            {
                throw new ArgumentNullException("queue");
            }
            if (messageProcessCallback == null)
            {
                throw new ArgumentNullException("messageProcessCallback");
            }

            var qmon = new AzureQueuePicker(queue, messageInvisibilityTimeMillisec, maxCheckDelaySeconds, useAopProxyWhenAccessingQueue);

            qmon.Subscribe(messageProcessCallback);
            return(qmon);
        }