예제 #1
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _executingTask = Task.Factory.StartNew(async() =>
            {
                using (var consumer = _consumerFactory.Create())
                {
                    const string topic = "build.workshop.something";
                    consumer.Subscribe(topic);
                    Console.WriteLine($"KafkaConsumer started. Listening to topic: {topic}");

                    // Consume loop
                    while (!_cancellationTokenSource.IsCancellationRequested)
                    {
                        ConsumeResult <string, string> msg;
                        try
                        {
                            msg = consumer.Consume(cancellationToken);
                            using (var scope = _serviceProvider.CreateScope())
                            {
                                var message           = MessagingHelper.MessageEnvelopeToMessage(msg.Value);
                                var eventType         = _eventRegistry.GetInstanceTypeFor(message.EventName);
                                dynamic eventInstance = Activator.CreateInstance(eventType, message);
                                dynamic handlersList  = _eventHandlerFactory.GetEventHandlersFor(eventInstance, scope);

                                foreach (var handler in handlersList)
                                {
                                    await handler.HandleAsync(eventInstance);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Consumption of message failed, reason: {ex}");
                            continue;
                        }

                        try
                        {
                            await Task.Run(() => consumer.Commit(msg));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Error consuming message: Exception message: {ex.Message}. Raw message: '{msg.Value}'");
                        }
                    }
                }
            }, _cancellationTokenSource.Token,
                                                   TaskCreationOptions.LongRunning, TaskScheduler.Default)
                             .ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("Event loop crashed");
                }
            }, cancellationToken);

            return(Task.CompletedTask);
        }
예제 #2
0
        private async Task DoWork(CancellationToken stoppingToken)
        {
            const string topic       = "build.workshop.something";
            var          newWorkshop = new WorkshopCreated
            {
                Id    = Guid.NewGuid().ToString(),
                Title = "Kafka - The basics",
                Date  = DateTime.Now.ToString("f")
            };

            using (var producer = _producerFactory.Create())
            {
                await producer.ProduceAsync(topic : topic, message : new Message <string, string>()
                {
                    Key   = Guid.NewGuid().ToString(),
                    Value = MessagingHelper.MessageToEnvelope(newWorkshop, "workshop_created", Guid.NewGuid().ToString())
                });
            }
        }