Exemplo n.º 1
0
        /// <summary>
        /// Esse método apenas redireciona uma mensagem para outro tópico.
        /// </summary>
        public static KafkaBuilder AddConsumerWithRedirectMessage <TConsumerClient, TKey, TValue>(
            this KafkaBuilder kafkaBuilder,
            Action <IServiceProvider, ConsumerConnectionBuilder <TKey, TValue> > optionsAction
            )
            where TConsumerClient : class, IConsumerClient <TKey, TValue>
        {
            var services = kafkaBuilder.Services;

            services.AddSingleton <TConsumerClient>();

            services.AddTransient <IHostedService>((s) =>
            {
                var logger = s.GetService <IServiceBusLogger>();
                var loggerServiceHosted = s.GetService <ILogger <DefaultConsumerHostedService <TKey, TValue> > >();
                var client = s.GetRequiredService <TConsumerClient>();
                var config = s.GetService <IOptions <KafkaConfig> >();
                var cb     = new ConsumerConnectionBuilder <TKey, TValue>(config.Value.CertificatePath);

                optionsAction(s, cb);

                var topicConsumer = TopicFactory.GetTopicConsumerWithRedirectMessage(client, cb, logger);
                return(new DefaultConsumerHostedService <TKey, TValue>(topicConsumer, loggerServiceHosted));
            });

            return(kafkaBuilder);
        }
Exemplo n.º 2
0
        public IActionResult Subscribe(
            string appName,
            bool simulateError,
            [FromBody] ConsumerOptions settings,
            [FromServices] IServiceBusLogger loggerServiceBus,
            [FromServices] ILogger <SignalRConsumerClient> loggerSignalR
            )
        {
            if (!subscribersCancels.ContainsKey(appName))
            {
                TopicConsumer <Guid, EventMessage> topicConsumer;
                var cancelSource = new CancellationTokenSource();
                var builder      = new ConsumerConnectionBuilder <Guid, EventMessage>(settings.IgnoreSsl ? null : this._appConfiguration.Kafka.CertificatePath);
                builder.WithBrokers(settings.BootstrapServers);
                builder.WithTopic(settings.Topic);
                builder.WithGroupId(settings.GroupId);
                builder.WithRetry(settings.RetryTopic, settings.Delay);
                builder.AutoOffSetReset(settings.AutoOffSetReset);
                builder.EnableAutoCommit(settings.EnableAutoCommit);
                builder.EnablePartitionEof(settings.EnablePartitionEof);
                //builder.MaxPollIntervalMs(settings.MaxPollIntervalMs);
                builder.WithJsonSerializer();

                var appInfo = new AppInfo
                {
                    AppName           = appName,
                    CancellationToken = cancelSource,
                    SimulateError     = simulateError,
                    Settings          = settings
                };

                var client = new SignalRConsumerClient(appInfo, _kafkaHubContext, loggerSignalR);

                switch (settings.RetryStrategy)
                {
                case "retry":
                    topicConsumer = TopicFactory.GetTopicConsumerWithRetry(client, builder, loggerServiceBus);
                    break;

                case "redirect":
                    topicConsumer = TopicFactory.GetTopicConsumerWithRedirectMessage(client, builder, loggerServiceBus);
                    break;

                default:     // Only consume
                    topicConsumer = TopicFactory.GetTopicConsumer(client, builder, loggerServiceBus);
                    break;
                }

                var task = topicConsumer.Run(cancelSource.Token);

                appInfo.TaskId = task.Id;
                subscribersCancels.TryAdd(appName, appInfo);
                return(Ok(appInfo));
            }

            return(Ok(subscribersCancels[appName]));
        }