Esempio n. 1
0
        public async Task SendAsync(KafkaJob job)
        {
            if (!string.IsNullOrWhiteSpace(job.Schema))
            {
                var value = CreateAvroRecord(job.MessageValue, job.Schema);

                var message = new Message <string, GenericRecord> {
                    Value = value
                };

                await ProduceAsync(avroProducer, message, job);
            }
            else
            {
                var message = new Message <string, string> {
                    Value = job.MessageValue
                };

                await ProduceAsync(textProducer, message, job);
            }
        }
Esempio n. 2
0
        private async Task ProduceAsync <T>(IProducer <string, T> producer, Message <string, T> message, KafkaJob job)
        {
            message.Key = job.MessageKey;

            if (job.Headers?.Count > 0)
            {
                message.Headers = new Headers();

                foreach (var header in job.Headers)
                {
                    message.Headers.Add(header.Key, Encoding.UTF8.GetBytes(header.Value));
                }
            }

            if (!string.IsNullOrWhiteSpace(job.PartitionKey) && job.PartitionCount > 0)
            {
                var partition = Math.Abs(job.PartitionKey.GetHashCode()) % job.PartitionCount;

                await producer.ProduceAsync(new TopicPartition(job.TopicName, partition), message);
            }
            else
            {
                await producer.ProduceAsync(job.TopicName, message);
            }
        }