Example #1
0
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            cancellationToken.Register(() => _logger.LogInformation($"{nameof(RunAsync)} is being cancelled"));

            try
            {
                await Execution
                .ExecuteAsync(cancellationToken,
                              _logger, _serviceEventSource,
                              nameof(ReceiverService), Context.PartitionId.ToString(),
                              async ct =>
                {
                    await _switch(cancellationToken);

                    cancellationToken.ThrowIfCancellationRequested();

                    var options = new EventProcessorOptions
                    {
                        OnShutdown              = OnShutdown,
                        MaxBatchSize            = MaxMessageCount,
                        PrefetchCount           = MaxMessageCount,
                        InitialPositionProvider = s =>
                        {
                            _logger.LogInformation("Using InitialPositionProvider for {s}", s);
                            return(_initialPositionProvider(s));
                        }
                    };

                    _logger.LogInformation("Create ServiceFabricProcessor with {ConsumerGroup}", _options.ConsumerGroup);

                    var processorService = new ServiceFabricProcessor(
                        Context.ServiceName,
                        Context.PartitionId,
                        StateManager,
                        Partition,
                        CreateProcessor(_options, _telemetryClient, _logger, _serviceEventSource, _eventHandlerCreator),
                        _options.ConnectionString,
                        _options.ConsumerGroup,
                        options);

                    await processorService.RunAsync(cancellationToken);
                });
            }
            catch (FabricTransientException e)
            {
                _logger.LogError(e, nameof(ReceiverService) + "Exception .RunAsync for {PartitionId}", Context.PartitionId);
            }
        }
Example #2
0
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            EventProcessorOptions options = new EventProcessorOptions();

            options.OnShutdown = OnShutdown;
            string eventHubConnectionString         = "------------------------------EVENT HUB CONNECTION STRING GOES HERE ----------------------------";
            ServiceFabricProcessor processorService = new ServiceFabricProcessor(this.Context.ServiceName, this.Context.PartitionId, this.StateManager, this.Partition,
                                                                                 new SampleEventProcessor(), eventHubConnectionString, "$Default", options);

            Task processing = processorService.RunAsync(cancellationToken);

            // If there is nothing else to do, application can simply await on the task here instead of polling keepgoing
            while (this.keepgoing)
            {
                // Do other stuff here
                Thread.Sleep(1000);
            }
            await processing;
            // The await may throw if there was an error.
        }