/// <summary>
        /// Initializes the subscriber asynchronously.
        /// </summary>
        /// <param name="description">
        /// The <see cref="SubscriberDescription">description</see>.
        /// </param>
        /// <param name="handler">
        /// The handler callback method.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the initialization operation.
        /// </returns>
        public async Task InitializeAsync(SubscriberDescription description, Func <IMessage, Task> handler)
        {
            using (ActivityMonitor.Instance.SubscriberInitialize(this, description))
            {
                try
                {
                    await this.EnsureEventHubExistsAsync(description);

                    await this.EnsureConsumerGroupExistsAsync(description);

                    this.Callback = handler;

                    var name  = Guid.NewGuid().ToString("N");
                    var path  = description.Entity;
                    var hcs   = description.ConnectionString;
                    var scs   = description.StorageConnectionString;
                    var group = description.Name;

                    this.Host = new EventProcessorHost(name, path, group, hcs, scs);

                    var options = new EventProcessorOptions();
                    options.ExceptionReceived += this.OnProcessorExceptionReceived;

                    await this.Host.RegisterEventProcessorFactoryAsync(this, options);
                }
                catch (Exception e)
                {
                    ActivityMonitor.Instance.ReportSubscriberException(this, e, false);
                    throw new AzureEventHubException(e.Message, e);
                }
            }
        }
        /// <summary>
        /// Initializes the subscriber asynchronously.
        /// </summary>
        /// <param name="description">
        /// The <see cref="SubscriberDescription">description</see>.
        /// </param>
        /// <param name="handler">
        /// The handler callback method.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the initialization operation.
        /// </returns>
        public async Task InitializeAsync(SubscriberDescription description, Func<IMessage, Task> handler)
        {
            using (ActivityMonitor.Instance.SubscriberInitialize(this, description))
            {
                try
                {
                    await this.EnsureEventHubExistsAsync(description);
                    await this.EnsureConsumerGroupExistsAsync(description);
                    this.Callback = handler;

                    var name = Guid.NewGuid().ToString("N");
                    var path = description.Entity;
                    var hcs = description.ConnectionString;
                    var scs = description.StorageConnectionString;
                    var group = description.Name;

                    this.Host = new EventProcessorHost(name, path, group, hcs, scs);

                    var options = new EventProcessorOptions();
                    options.ExceptionReceived += this.OnProcessorExceptionReceived;

                    await this.Host.RegisterEventProcessorFactoryAsync(this, options);
                }
                catch (Exception e)
                {
                    ActivityMonitor.Instance.ReportSubscriberException(this, e, false);
                    throw new AzureEventHubException(e.Message, e);
                }
            }
        }
        /// <summary>
        /// Ensures the subscription exists.
        /// </summary>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        private async Task EnsureSubscriptionExistsAsync(SubscriberDescription description)
        {
            await this.EnsureTopicExistsAsync(description);

            var connectionString = description.ConnectionString;
            var path             = description.Entity;
            var name             = description.Name;

            this.Manager = NamespaceManager.CreateFromConnectionString(connectionString);

            if (!await this.Manager.SubscriptionExistsAsync(path, name))
            {
                await
                this.Manager.CreateSubscriptionAsync(
                    new SubscriptionDescription(path, name)
                {
                    EnableBatchedOperations = true,
                    EnableDeadLetteringOnFilterEvaluationExceptions = false,
                    EnableDeadLetteringOnMessageExpiration          = false
                });
            }

            var timer = new Timer {
                Interval = 60000, Enabled = true
            };

            timer.Elapsed += this.ReportSubscriberTopicQueueDepth;
        }
        /// <summary>
        /// Initializes the subscriber asynchronously.
        /// </summary>
        /// <param name="description">
        /// The <see cref="SubscriberDescription">description</see>.
        /// </param>
        /// <param name="handler">
        /// The handler callback method.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the initialization operation.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Occurs when either argument is null.
        /// </exception>
        public async Task InitializeAsync(SubscriberDescription description, Func <IMessage, Task> handler)
        {
            using (ActivityMonitor.Instance.SubscriberInitialize(this, description))
            {
                if (description == null)
                {
                    var exception = new ArgumentNullException("description");
                    ActivityMonitor.Instance.ReportSubscriberException(this, exception, false);
                    throw exception;
                }

                if (handler == null)
                {
                    var exception = new ArgumentNullException("handler");
                    ActivityMonitor.Instance.ReportSubscriberException(this, exception, false);
                    throw exception;
                }

                try
                {
                    await this.EnsureSubscriptionExistsAsync(description);

                    this.InitializeClient(description, handler);
                }
                catch (Exception e)
                {
                    ActivityMonitor.Instance.ReportSubscriberException(this, e, false);
                    throw new AzureServiceBusException(e.Message, e);
                }
            }
        }
        /// <summary>
        /// Ensures the consumer group exists asynchronously.
        /// </summary>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        /// <exception cref="AzureEventHubException">
        /// Occurs if there is an error creating the event hub.
        /// </exception>
        protected async Task EnsureConsumerGroupExistsAsync(SubscriberDescription description)
        {
            var cs = description.ConnectionString;
            var hub = description.Entity;

            var manager = NamespaceManager.CreateFromConnectionString(cs);
            var eventhubdescription = await manager.CreateEventHubIfNotExistsAsync(hub);
            await manager.CreateConsumerGroupIfNotExistsAsync(eventhubdescription.Path, description.Name);
        }
        /// <summary>
        /// Ensures the consumer group exists asynchronously.
        /// </summary>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        /// <exception cref="AzureEventHubException">
        /// Occurs if there is an error creating the event hub.
        /// </exception>
        protected async Task EnsureConsumerGroupExistsAsync(SubscriberDescription description)
        {
            var cs  = description.ConnectionString;
            var hub = description.Entity;

            var manager             = NamespaceManager.CreateFromConnectionString(cs);
            var eventhubdescription = await manager.CreateEventHubIfNotExistsAsync(hub);

            await manager.CreateConsumerGroupIfNotExistsAsync(eventhubdescription.Path, description.Name);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes the subscriber asynchronously.
 /// </summary>
 /// <param name="description">
 /// The <see cref="SubscriberDescription">description</see>.
 /// </param>
 /// <param name="handler">
 /// The handler callback method.
 /// </param>
 /// <returns>
 /// A <see cref="Task"/> representing the initialization operation.
 /// </returns>
 public Task InitializeAsync(SubscriberDescription description, Func <IMessage, Task> handler)
 {
     return(Task.Run(
                () =>
     {
         Assert.IsFalse(this.IsClosed, "The subscriber must not be closed before it is initialized.");
         this.Description = description;
         this.Handler = handler;
         this.IsInitialized = true;
     }));
 }
 /// <summary>
 /// Initializes the subscriber asynchronously.
 /// </summary>
 /// <param name="description">
 /// The <see cref="SubscriberDescription">description</see>.
 /// </param>
 /// <param name="handler">
 /// The handler callback method.
 /// </param>
 /// <returns>
 /// A <see cref="Task"/> representing the initialization operation.
 /// </returns>
 public Task InitializeAsync(SubscriberDescription description, Func<IMessage, Task> handler)
 {
     return Task.Run(
         () =>
             {
                 Assert.IsFalse(this.IsClosed, "The subscriber must not be closed before it is initialized.");
                 this.Description = description;
                 this.Handler = handler;
                 this.IsInitialized = true;
             });
 }
        /// <summary>
        /// Initializes the subscription client.
        /// </summary>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <param name="callback">
        /// The callback.
        /// </param>
        private void InitializeClient(SubscriberDescription description, Func <IMessage, Task> callback)
        {
            this.SubscriberDescription = description;
            var connectionString = this.SubscriberDescription.ConnectionString;
            var path             = this.SubscriberDescription.Entity;
            var name             = this.SubscriberDescription.Name;

            this.Callback = callback;

            this.Client = SubscriptionClient.CreateFromConnectionString(
                connectionString,
                path,
                name,
                ReceiveMode.ReceiveAndDelete);

            this.ReceiveMessages();
        }
Exemplo n.º 10
0
 /// <summary>
 /// The subscriber initialize.
 /// </summary>
 /// <param name="publisher">
 /// The publisher.
 /// </param>
 /// <param name="description">
 /// The description.
 /// </param>
 /// <returns>
 /// The <see cref="IDisposable"/>.
 /// </returns>
 public IDisposable SubscriberInitialize(ISubscriber publisher, SubscriberDescription description)
 {
     return(GetActivityTracker(
                () =>
     {
         var args = new SubscriberInitializingEventArgs {
             Description = description
         };
         this.OnSubscriberInitializing(publisher, args);
     },
                t =>
     {
         var args = new SubscriberInitializedEventArgs {
             Elapsed = t, Description = description
         };
         this.OnSubscriberInitialized(publisher, args);
     }));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Reports the subscription message count.
 /// </summary>
 /// <param name="subscriber">The subscriber.</param>
 /// <param name="args">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param>
 /// <param name="totalMessageCount">The total message count.</param>
 /// <param name="activeMessageCount">The active message count.</param>
 /// <param name="deadLetterMessageCount">The dead letter message count.</param>
 /// <param name="description">The description.</param>
 public void ReportSubscriptionMessageCount(
     ISubscriber subscriber,
     ElapsedEventArgs args,
     long totalMessageCount,
     long activeMessageCount,
     long deadLetterMessageCount,
     SubscriberDescription description)
 {
     if (args != null)
     {
         this.OnSubscriberMessageCount(
             subscriber,
             new SubscriberMessageCountEventArgs
         {
             SignalTime             = args.SignalTime,
             TotalMessageCount      = totalMessageCount,
             ActiveMessageCount     = activeMessageCount,
             DeadLetterMessageCount = deadLetterMessageCount,
             SubscriberDescription  = description
         });
     }
 }
        /// <summary>
        /// Initializes the subscriber asynchronously.
        /// </summary>
        /// <param name="description">
        /// The <see cref="SubscriberDescription">description</see>.
        /// </param>
        /// <param name="handler">
        /// The handler callback method.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the initialization operation.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Occurs when either argument is null.
        /// </exception>
        public async Task InitializeAsync(SubscriberDescription description, Func<IMessage, Task> handler)
        {
            using (ActivityMonitor.Instance.SubscriberInitialize(this, description))
            {
                if (description == null)
                {
                    var exception = new ArgumentNullException("description");
                    ActivityMonitor.Instance.ReportSubscriberException(this, exception, false);
                    throw exception;
                }

                if (handler == null)
                {
                    var exception = new ArgumentNullException("handler");
                    ActivityMonitor.Instance.ReportSubscriberException(this, exception, false);
                    throw exception;
                }

                try
                {
                    await this.EnsureSubscriptionExistsAsync(description);
                    this.InitializeClient(description, handler);
                }
                catch (Exception e)
                {
                    ActivityMonitor.Instance.ReportSubscriberException(this, e, false);
                    throw new AzureServiceBusException(e.Message, e);
                }
            }
        }
        /// <summary>
        /// Initializes the subscription client.
        /// </summary>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <param name="callback">
        /// The callback.
        /// </param>
        private void InitializeClient(SubscriberDescription description, Func<IMessage, Task> callback)
        {
            this.SubscriberDescription = description;
            var connectionString = this.SubscriberDescription.ConnectionString;
            var path = this.SubscriberDescription.Entity;
            var name = this.SubscriberDescription.Name;

            this.Callback = callback;

            this.Client = SubscriptionClient.CreateFromConnectionString(
                connectionString,
                path,
                name,
                ReceiveMode.ReceiveAndDelete);

            this.ReceiveMessages();
        }
        /// <summary>
        /// Ensures the subscription exists.
        /// </summary>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        private async Task EnsureSubscriptionExistsAsync(SubscriberDescription description)
        {
            await this.EnsureTopicExistsAsync(description);

            var connectionString = description.ConnectionString;
            var path = description.Entity;
            var name = description.Name;

            this.Manager = NamespaceManager.CreateFromConnectionString(connectionString);

            if (!await this.Manager.SubscriptionExistsAsync(path, name))
            {
                await
                    this.Manager.CreateSubscriptionAsync(
                        new SubscriptionDescription(path, name)
                            {
                                EnableBatchedOperations = true,
                                EnableDeadLetteringOnFilterEvaluationExceptions = false,
                                EnableDeadLetteringOnMessageExpiration = false
                            });
            }

            var timer = new Timer { Interval = 60000, Enabled = true };
            timer.Elapsed += this.ReportSubscriberTopicQueueDepth;
        }