Пример #1
0
        /// <summary>
        /// Submitts messages to kafka on a given topic and using the given bootstrap servers.
        /// This method will create a new producer for every message submitted.
        /// </summary>
        /// <param name="subject">The <c>EventClient</c> to extend</param>
        /// <param name="topic">The topic to produce messages to</param>
        /// <param name="bootstrapServers">the boostrap servers</param>
        /// <returns>the modified event client.</returns>
        public static EventClient UseKafka(this EventClient subject, string topic, string bootstrapServers)
        {
            var config = new ProducerConfig {
                BootstrapServers = bootstrapServers
            };
            var builder = new ProducerBuilder <string, string>(config);

            subject.UseHandle(
                async evnt =>
            {
                using (var producer = builder.Build())
                {
                    try
                    {
                        var kafkaEvent = new KafkaEvent {
                            Tags = evnt.Tags, Content = JObject.Parse(evnt.Content)
                        };
                        var strContent = await Task.Run(() => JsonConvert.SerializeObject(kafkaEvent));
                        await producer.ProduceAsync(topic, new Message <string, string> {
                            Key = evnt.Name, Value = strContent
                        });
                    }
                    catch (ProduceException <string, string> e)
                    {
                        Console.WriteLine($"Delivery to Kafka failed: {e.Error.Reason}");
                        throw e;
                    }
                }
            });

            return(subject);
        }
Пример #2
0
        /// <summary>
        /// Submitts messages to kafka on a given topic and using the given bootstrap servers.
        /// In contrast to <c>UseKafka(this EventClient subject, string , string)</c>, this extension gives controll over how a producer is created.
        /// This requires the lifetime and deallocation to be managed externally but enables reusing producers for as long as it's needed and thus have much higher throughput for production of messages.
        /// </summary>
        /// <param name="subject">The <c>EventClient</c> to extend</param>
        /// <param name="topic">The topic to produce messages to</param>
        /// <param name="producerFactory">A factory function that returnes a ready to use Kafka producer.</param>
        /// <returns></returns>
        public static EventClient UseKafka(this EventClient subject, string topic, Func <IProducer <string, string> > producerFactory)
        {
            subject.UseHandle(
                async evnt =>
            {
                try
                {
                    var producer   = producerFactory();
                    var kafkaEvent = new KafkaEvent {
                        Tags = evnt.Tags, Content = JObject.Parse(evnt.Content)
                    };
                    var strContent = await Task.Run(() => JsonConvert.SerializeObject(kafkaEvent));
                    await producer.ProduceAsync(topic, new Message <string, string> {
                        Key = evnt.Name, Value = strContent
                    });
                }
                catch (ProduceException <string, string> e)
                {
                    Console.WriteLine($"Delivery to Kafka failed: {e.Error.Reason}");
                    throw e;
                }
            });

            return(subject);
        }
Пример #3
0
        public Task <bool> CanConnectKafka(CancellationToken cancellationToken = default)
        {
            var config = new ProducerConfig
            {
                BootstrapServers = _kafkaOptions.BootstrapServers,
                ClientId         = $"{_kafkaOptions.ClientId} - {Dns.GetHostName()}",
            };

            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                try
                {
                    var kafkaEvent = KafkaEvent.HealthCheckEvent();
                    _logger.Debug("Sending Message ...");
                    producer.Produce("HealthCheck.Sent", new Message <Null, string> {
                        Value = JsonConvert.SerializeObject(kafkaEvent)
                    }, ProducerHandler);
                    producer.Flush();
                    _logger.Debug("... Message Produced");
                }
                catch (Exception ex)
                {
                    Task.FromResult(false);
                }
            }
            return(Task.FromResult(true));
        }
Пример #4
0
        public IActionResult Post(TopicMessageModel model)
        {
            var config = new ProducerConfig
            {
                BootstrapServers = _kafkaOptions.BootstrapServers,
                ClientId         = $"{_kafkaOptions.ClientId} - {Dns.GetHostName()}",
            };

            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                try
                {
                    var kafkaEvent = new KafkaEvent(model.Message);
                    _logger.Debug("Sending Message ...");
                    producer.Produce(model.Topic, new Message <Null, string> {
                        Value = JsonConvert.SerializeObject(kafkaEvent)
                    }, ProducerHandler);
                    producer.Flush();
                    _logger.Debug("... Message Produced");
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            return(Accepted());
        }
Пример #5
0
        private static Guid EvaluateSessionId(KafkaEvent <BinaryReferencedEvent> @event)
        {
            var binaryReferencedEvent = @event.Source;

            var fileKey = binaryReferencedEvent.FileKey;

            if (string.IsNullOrEmpty(fileKey))
            {
                throw new ArgumentException(
                          $"File key is not set for the object with id = '{binaryReferencedEvent.ObjectId}' and versionId = {binaryReferencedEvent.ObjectVersionId} " +
                          $"in the event of type {binaryReferencedEvent.GetType().Name} with offset '{@event.TopicPartitionOffset}'");
            }

            return(new Guid(fileKey.Substring(0, fileKey.IndexOf(SlashChar))));
        }
Пример #6
0
        /// <summary>
        /// Publishes specific event topic.
        /// </summary>
        /// <param name="kafkaEvent">The event to be published to kafka.</param>
        public void Publish <E>(KafkaEvent <E> kafkaEvent)
        {
            if (string.IsNullOrWhiteSpace(kafkaEvent.TopicKey))
            {
                throw new NullTopicException("No topic was set.");
            }
            if (!_serviceConfig.ProducedTopics.ContainsKey(kafkaEvent.TopicKey))
            {
                throw new NullTopicException("The topic does not exist in the topic dictionary. Update the appsettings file.");
            }
            _logger.LogInformation("Creating a new event {@Event}", kafkaEvent);

            var jsonString     = JsonConvert.SerializeObject(kafkaEvent);
            var deliveryReport = _producer.ProduceAsync(_serviceConfig.ProducedTopics[kafkaEvent.TopicKey], null, jsonString);

            _logger.LogInformation("Event has been processed");
        }
 public void Publish <E>(KafkaEvent <E> kafkaEvent)
 {
 }