public Task <DeliveryResult <T, TV> > SendMessage(T key, TV value)
        {
            var result = KafkaHandler.DeliverMessage(key, value, ProducerId);
            var date   = result.Result.Timestamp.UtcDateTime;

            Devon4NetLogger.Information($"Message delivered. Key: {result.Result.Key} | Value : {result.Result.Value} | Topic: {result.Result.Topic} | UTC TimeStamp : {date.ToShortDateString()}-{date.ToLongTimeString()} | Status: {result.Result.Status}");
            return(result);
        }
Esempio n. 2
0
        public async Task <bool> DeleteTopic(string adminId, List <string> topicsName)
        {
            if (topicsName == null || !topicsName.Any())
            {
                throw new ArgumentException("The topics list can not be null or empty");
            }

            try
            {
                return(await KafkaHandler.DeleteTopic(adminId, topicsName));
            }
            catch (Exception ex)
            {
                Devon4NetLogger.Error($"An error occured creating topic list {string.Join(",", topicsName)}");
                Devon4NetLogger.Error(ex);
                throw;
            }
        }
Esempio n. 3
0
        public async Task <bool> CreateTopic(string adminId, string topicName, short replicationFactor = 1, int numPartitions = 1)
        {
            if (string.IsNullOrEmpty(topicName))
            {
                throw new ArgumentException("The topic name can not be null or empty");
            }

            try
            {
                return(await KafkaHandler.CreateTopic(adminId, topicName, replicationFactor, numPartitions));
            }
            catch (Exception ex)
            {
                Devon4NetLogger.Error($"An error occured creating topic {topicName}");
                Devon4NetLogger.Error(ex);
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// If commit is set to true, the Commit method sends a "commit offsets" request to the Kafka cluster and synchronously waits for the response.
        /// This is very slow compared to the rate at which the consumer is capable of consuming messages.
        /// A high performance application will typically commit offsets relatively infrequently and be designed handle duplicate messages in the event of failure.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TV"></typeparam>
        /// <param name="commit"></param>
        /// <param name="commitPeriod"></param>
        private void Consume(bool commit, int commitPeriod)
        {
            var cancellationToken = new CancellationTokenSource();

            Task.Run(() =>
            {
                try
                {
                    using var consumer = KafkaHandler.GetConsumerBuilder <T, TV>(ConsumerId);
                    while (EnableConsumerFlag)
                    {
                        var consumeResult = consumer?.Consume(cancellationToken.Token);
                        if (consumeResult?.Message == null)
                        {
                            continue;
                        }

                        HandleCommand(consumeResult.Message.Key, consumeResult.Message.Value);

                        if (consumeResult.IsPartitionEOF)
                        {
                            Devon4NetLogger.Information($"Reached end of topic {consumeResult.Topic}, partition {consumeResult.Partition}, offset {consumeResult.Offset}.");
                            continue;
                        }

                        Devon4NetLogger.Debug($"Received message at {consumeResult.TopicPartitionOffset}: {consumeResult.Message.Value}");

                        if (!commit || consumeResult.Offset % commitPeriod != 0)
                        {
                            continue;
                        }

                        consumer?.Commit(consumeResult);
                        consumer?.Close();
                    }
                }
                catch (Exception ex)
                {
                    Devon4NetLogger.Error(ex);
                }
            }, cancellationToken.Token);
        }