示例#1
0
        public Task StartAsync(CancellationToken stopCancellationToken = default)
        {
            this.cancellationTokenSource =
                CancellationTokenSource.CreateLinkedTokenSource(stopCancellationToken);

            this.backgroundTask = Task.Factory.StartNew(
                async() =>
            {
                while (!this.cancellationTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        var message = await this.messagesBuffer.Reader
                                      .ReadAsync(this.cancellationTokenSource.Token)
                                      .ConfigureAwait(false);

                        var context = new ConsumerMessageContext(
                            new MessageContextConsumer(
                                this.consumer,
                                this.configuration.ConsumerName,
                                this.offsetManager,
                                message),
                            message,
                            this.Id,
                            this.configuration.GroupId);

                        try
                        {
                            await this.middlewareExecutor
                            .Execute(context, con => Task.CompletedTask)
                            .ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            this.logHandler.Error(
                                "Error executing consumer",
                                ex,
                                context);
                        }
                        finally
                        {
                            if (this.configuration.AutoStoreOffsets)
                            {
                                this.offsetManager.StoreOffset(message.TopicPartitionOffset);
                            }

                            this.onMessageFinishedHandler?.Invoke();
                        }
                    }
                    catch (OperationCanceledException)
                    {
                    }
                }
            },
                CancellationToken.None,
                TaskCreationOptions.LongRunning,
                TaskScheduler.Default);

            return(Task.CompletedTask);
        }
示例#2
0
        public Task StartAsync()
        {
            this.stopCancellationTokenSource = new CancellationTokenSource();

            this.backgroundTask = Task.Run(
                async() =>
            {
                while (!this.stopCancellationTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        var message = await this.messagesBuffer.Reader
                                      .ReadAsync(this.stopCancellationTokenSource.Token)
                                      .ConfigureAwait(false);

                        var context = new ConsumerMessageContext(
                            new MessageContextConsumer(
                                this.consumer,
                                this.offsetManager,
                                message,
                                this.stopCancellationTokenSource.Token),
                            message,
                            this.Id,
                            this.consumer.Configuration.GroupId);

                        try
                        {
                            await this.middlewareExecutor
                            .Execute(context, _ => Task.CompletedTask)
                            .ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            this.logHandler.Error(
                                "Error executing consumer",
                                ex,
                                new
                            {
                                context.Message,
                                context.Topic,
                                context.PartitionKey,
                                ConsumerName = context.Consumer.Name
                            });
                        }
                        finally
                        {
                            if (this.consumer.Configuration.AutoStoreOffsets && context.Consumer.ShouldStoreOffset)
                            {
                                this.offsetManager.StoreOffset(message.TopicPartitionOffset);
                            }

                            this.onMessageFinishedHandler?.Invoke();
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        // Ignores the exception
                    }
                }
            });

            return(Task.CompletedTask);
        }