Example #1
0
        private static async Task MainAsync()
        {
            Console.WriteLine("Registering EventProcessor...");

            var eventProcessorHost = new EventProcessorHost(Constants.GstnReqEventHub,
                                                            PartitionReceiver.DefaultConsumerGroupName,
                                                            Constants.GstnReqHubConnectionRead,
                                                            Constants.BlobStorageConnection,
                                                            "gstn-req-event-hub-container");
            //var eventProcessorHost = new EventProcessorHost(ConfigurationManager.AppSettings["GstnReqEventHub"].ToString(),
            //                                              PartitionReceiver.DefaultConsumerGroupName,
            //                                               ConfigurationManager.AppSettings["GstnReqHubConnectionRead"].ToString(),
            //                                              ConfigurationManager.AppSettings["BlobStorageConnection"].ToString(),
            //                                              ConfigurationManager.AppSettings["BlobContainer"].ToString());

            // registration fails and retry
            var options = new Microsoft.Azure.EventHubs.Processor.EventProcessorOptions()
            {
                MaxBatchSize   = 10,
                ReceiveTimeout = TimeSpan.FromSeconds(5),
                //InitialOffsetProvider = (partitionId) => "4312342208",
                //InitialOffsetProvider = (partitionId) => 98585,
            };
            await eventProcessorHost.RegisterEventProcessorAsync <EventHubProcessor>(options);

            Console.WriteLine("Receiving. Press ENTER to stop worker.");
            Console.ReadLine();

            // Disposes of the Event Processor Host
            //On Shutdown unregister the application from event Hub
            // Webhook call to shutdown
            // async health monitoring and log
            //
            await eventProcessorHost.UnregisterEventProcessorAsync();
        }
Example #2
0
 public MyThread()
 {
     this.options = new Microsoft.Azure.EventHubs.Processor.EventProcessorOptions()
     {
         MaxBatchSize   = 10,
         ReceiveTimeout = TimeSpan.FromSeconds(5),
     };
 }
        /// <summary>
        /// This registers <see cref="IEventProcessorFactory"/> implementation with the host which is used to create an instance of
        /// <see cref="IEventProcessor"/> when it takes ownership of a partition.  This also starts the host and causes it to start participating
        /// in the partition distribution process.
        /// </summary>
        /// <param name="factory">Instance of <see cref="IEventProcessorFactory"/> implementation.</param>
        /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pump created when ownership
        /// is acquired for a particular partition of EventHub.</param>
        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
        public async Task RegisterEventProcessorFactoryAsync(IEventProcessorFactory factory, EventProcessorOptions processorOptions)
        {
            if (factory == null || processorOptions == null)
            {
                throw new ArgumentNullException(factory == null ? nameof(factory) : nameof(processorOptions));
            }

            ProcessorEventSource.Log.EventProcessorHostOpenStart(this.Id, factory.GetType().ToString());
            try
            {
                // Override operation timeout by receive timeout?
                if (processorOptions.ReceiveTimeout > TimeSpan.MinValue)
                {
                    var cbs = new EventHubsConnectionStringBuilder(this.EventHubConnectionString)
                    {
                        OperationTimeout = processorOptions.ReceiveTimeout
                    };
                    this.EventHubConnectionString = cbs.ToString();
                }

                if (this.initializeLeaseManager)
                {
                    ((AzureStorageCheckpointLeaseManager)this.LeaseManager).Initialize(this);
                }

                this.ProcessorFactory      = factory;
                this.EventProcessorOptions = processorOptions;
                await this.PartitionManager.StartAsync().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                ProcessorEventSource.Log.EventProcessorHostOpenError(this.Id, e.ToString());
                throw;
            }
            finally
            {
                ProcessorEventSource.Log.EventProcessorHostOpenStop(this.Id);
            }
        }
        /// <summary>
        /// This registers <see cref="IEventProcessor"/> implementation with the host using <see cref="DefaultEventProcessorFactory{T}"/>.
        /// This also starts the host and causes it to start participating in the partition distribution process.
        /// </summary>
        /// <typeparam name="T">Implementation of your application specific <see cref="IEventProcessor"/>.</typeparam>
        /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pump created when ownership
        /// is acquired for a particular partition of EventHub.</param>
        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
        public Task RegisterEventProcessorAsync <T>(EventProcessorOptions processorOptions) where T : IEventProcessor, new()
        {
            IEventProcessorFactory f = new DefaultEventProcessorFactory <T>();

            return(RegisterEventProcessorFactoryAsync(f, processorOptions));
        }
Example #5
0
        /// <summary>
        /// This registers <see cref="IEventProcessorFactory"/> implementation with the host which is used to create an instance of
        /// <see cref="IEventProcessor"/> when it takes ownership of a partition.  This also starts the host and causes it to start participating
        /// in the partition distribution process.
        /// </summary>
        /// <param name="factory">Instance of <see cref="IEventProcessorFactory"/> implementation.</param>
        /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pump created when ownership
        /// is acquired for a particular partition of EventHub.</param>
        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
        public async Task RegisterEventProcessorFactoryAsync(IEventProcessorFactory factory, EventProcessorOptions processorOptions)
        {
            Guard.ArgumentNotNull(nameof(factory), factory);
            Guard.ArgumentNotNull(nameof(processorOptions), processorOptions);

            // Initialize partition manager options with default values if not already set by the client.
            if (this.PartitionManagerOptions == null)
            {
                // Assign partition manager with default options.
                this.PartitionManagerOptions = new PartitionManagerOptions();
            }

            ProcessorEventSource.Log.EventProcessorHostOpenStart(this.HostName, factory.GetType().ToString());

            try
            {
                // Override operation timeout by receive timeout?
                if (processorOptions.ReceiveTimeout > TimeSpan.MinValue)
                {
                    this.OperationTimeout = processorOptions.ReceiveTimeout;

                    if (this.eventHubConnectionString != null)
                    {
                        var cbs = new EventHubsConnectionStringBuilder(this.eventHubConnectionString)
                        {
                            OperationTimeout = processorOptions.ReceiveTimeout
                        };
                        this.eventHubConnectionString = cbs.ToString();
                    }
                }

                // Initialize lease manager if this is an AzureStorageCheckpointLeaseManager
                (this.LeaseManager as AzureStorageCheckpointLeaseManager)?.Initialize(this);

                this.ProcessorFactory      = factory;
                this.EventProcessorOptions = processorOptions;
                await this.PartitionManager.StartAsync().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                ProcessorEventSource.Log.EventProcessorHostOpenError(this.HostName, e.ToString());
                throw;
            }
            finally
            {
                ProcessorEventSource.Log.EventProcessorHostOpenStop(this.HostName);
            }
        }