KafkaNet.Producer.SendMessageAsync C# (CSharp) Method

SendMessageAsync() public method

Send an enumerable of message objects to a given topic.
public SendMessageAsync ( string topic, IEnumerable messages, Int16 acks = 1, System.TimeSpan timeout = null, MessageCodec codec = MessageCodec.CodecNone, int partition = null ) : Task
topic string The name of the kafka topic to send the messages to.
messages IEnumerable The enumerable of messages that will be sent to the given topic.
acks System.Int16 The required level of acknowlegment from the kafka server. 0=none, 1=writen to leader, 2+=writen to replicas, -1=writen to all replicas.
timeout System.TimeSpan Interal kafka timeout to wait for the requested level of ack to occur before returning. Defaults to 1000ms.
codec MessageCodec The codec to apply to the message collection. Defaults to none.
partition int
return Task
        public Task<ProduceResponse[]> SendMessageAsync(string topic, IEnumerable<Message> messages, Int16 acks = 1,
            TimeSpan? timeout = null, MessageCodec codec = MessageCodec.CodecNone, int? partition = null)
        {
            if (_stopToken.IsCancellationRequested)
                throw new ObjectDisposedException("Cannot send new documents as producer is disposing.");
            if (timeout == null) timeout = TimeSpan.FromMilliseconds(DefaultAckTimeoutMS);

            var batch = messages.Select(message => new TopicMessage
            {
                Acks = acks,
                Codec = codec,
                Timeout = timeout.Value,
                Topic = topic,
                Message = message,
                Partition = partition
            }).ToArray();

            _asyncCollection.AddRange(batch);

            return Task.WhenAll(batch.Select(x => x.Tcs.Task));
        }

Same methods

Producer::SendMessageAsync ( Message messages, string topic, int partition, Int16 acks = 1 ) : Task
Producer::SendMessageAsync ( string topic ) : Task
Producer::SendMessageAsync ( string topic, int partition ) : Task

Usage Example

 public async Task ProducerAckLevel()
 {
     using (var router = new BrokerRouter(new KafkaOptions(IntegrationConfig.IntegrationUri) { Log = IntegrationConfig.NoDebugLog }))
     using (var producer = new Producer(router))
     {
         var responseAckLevel0 = await producer.SendMessageAsync(new Message("Ack Level 0"), IntegrationConfig.IntegrationTopic, acks: 0, partition: 0);
         Assert.AreEqual(responseAckLevel0.Offset, -1);
         var responseAckLevel1 = await producer.SendMessageAsync(new Message("Ack Level 1"), IntegrationConfig.IntegrationTopic, acks: 1, partition: 0);
         Assert.That(responseAckLevel1.Offset, Is.GreaterThan(-1));
     }
 }
All Usage Examples Of KafkaNet.Producer::SendMessageAsync