Exemplo n.º 1
0
        /// <summary>
        /// 异步发送消息
        /// </summary>
        /// <param name="kafkaMessage"></param>
        /// <returns></returns>
        public async Task <DeliveryResult> PublishAsync(KafkaMessage kafkaMessage)
        {
            var topic = string.IsNullOrEmpty(kafkaMessage.Topic) ? DefaultTopic : kafkaMessage.Topic;

            if (string.IsNullOrEmpty(topic))
            {
                throw new ArgumentException("topic can not be empty", nameof(kafkaMessage.Topic));
            }
            var key = string.IsNullOrEmpty(kafkaMessage.Key) ? DefaultKey : kafkaMessage.Key;

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("key can not be empty", nameof(kafkaMessage.Key));
            }

            var producer = RentProducer();
            DeliveryResult <string, object> deliveryResult;

            if (kafkaMessage.Partition == null)
            {
                deliveryResult = await producer.ProduceAsync(topic, new Message <string, object>() { Key = key, Value = kafkaMessage.Message });
            }
            else
            {
                var topicPartition = new TopicPartition(topic, new Partition(kafkaMessage.Partition.Value));
                deliveryResult = await producer.ProduceAsync(topicPartition, new Message <string, object>() { Key = key, Value = kafkaMessage.Message });
            }

            producer.Flush(new TimeSpan(0, 0, 0, 0, FlushTimeOut));

            ReturnProducer(producer);

            return(new DeliveryResult(deliveryResult));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="kafkaMessage"></param>
        /// <param name="callback"></param>
        public void Publish(KafkaMessage kafkaMessage, Action <DeliveryResult> callback = null)
        {
            var topic = string.IsNullOrEmpty(kafkaMessage.Topic) ? DefaultTopic : kafkaMessage.Topic;

            if (string.IsNullOrEmpty(topic))
            {
                throw new ArgumentException("topic can not be empty", nameof(kafkaMessage.Topic));
            }
            var key = string.IsNullOrEmpty(kafkaMessage.Key) ? DefaultKey : kafkaMessage.Key;

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("key can not be empty", nameof(kafkaMessage.Key));
            }

            var producer = RentProducer();

            if (kafkaMessage.Partition == null)
            {
                producer.Produce(topic, new Message <string, object>()
                {
                    Key = key, Value = kafkaMessage.Message
                }, dr => callback?.Invoke(new DeliveryResult(dr)));
            }
            else
            {
                var topicPartition = new TopicPartition(topic, new Partition(kafkaMessage.Partition.Value));
                producer.Produce(topicPartition, new Message <string, object>()
                {
                    Key = key, Value = kafkaMessage.Message
                }, dr => callback?.Invoke(new DeliveryResult(dr)));
            }

            producer.Flush(TimeSpan.FromMilliseconds(FlushTimeOut));

            ReturnProducer(producer);
        }