public async Task ProduceAsync(Message message) { var config = new ProducerConfig { BootstrapServers = _serverAddress }; using (var producer = new ProducerBuilder <string, string>(config).Build()) { try { // Note: Awaiting the asynchronous produce request below prevents flow of execution // from proceeding until the acknowledgement from the broker is received (at the // expense of low throughput). var deliveryReport = await producer.ProduceAsync( _produceTopic, KafkaMessage.ToKafka(message) ); _logger.LogInformation($"delivered to: {deliveryReport.TopicPartitionOffset}"); } catch (ProduceException <string, string> e) { _logger.LogError($"failed to deliver message: {e.Message} [{e.Error.Code}]"); } } }
public async Task ProduceAsync(string channelName, Message message) { var config = new ProducerConfig { BootstrapServers = _serverAddress }; var producerBuilder = new ProducerBuilder <string, string>(config); producerBuilder.SetErrorHandler( (producer, error) => { _logger.LogError(error.Reason); } ); using (var producer = producerBuilder.Build()) { try { // Note: Awaiting the asynchronous produce request below prevents flow of execution // from proceeding until the acknowledgement from the broker is received (at the // expense of low throughput). var deliveryReport = await producer.ProduceAsync( channelName, KafkaMessage.ToKafka(message) ); //producer.Produce( // channelName, KafkaMessage.FromMessageBrokerMessage(message) //); //_logger.LogInformation($"delivered to: {deliveryReport.TopicPartitionOffset}"); } catch (ProduceException <string, string> e) { _logger.LogError($"failed to deliver message: {e.Message} [{e.Error.Code}]"); } } }