Example #1
0
        public override Task <OperateResult> PublishAsync(string keyName, string content)
        {
            try
            {
                var config = _kafkaOptions.AsRdkafkaConfig();
                using (var producer = new Producer <Null, string>(config, null, new StringSerializer(Encoding.UTF8)))
                {
                    producer.ProduceAsync(keyName, null, content);
                    producer.Flush();
                }

                _logger.LogDebug($"kafka topic message [{keyName}] has been published.");

                return(Task.FromResult(OperateResult.Success));
            }
            catch (Exception ex)
            {
                _logger.LogError($"kafka topic message [{keyName}] has benn raised an exception of sending. the exception is: {ex.Message}");

                return(Task.FromResult(OperateResult.Failed(ex,
                                                            new OperateError()
                {
                    Code = ex.HResult.ToString(),
                    Description = ex.Message
                })));
            }
        }
Example #2
0
        private void InitKafkaClient()
        {
            _kafkaOptions.MainConfig.Add("group.id", _groupId);

            var config = _kafkaOptions.AsRdkafkaConfig();

            _consumerClient = new Consumer <Null, string>(config, null, StringDeserializer);

            _consumerClient.OnMessage += ConsumerClient_OnMessage;
        }
        private Task ExecuteJobAsync(string topic, string content)
        {
            var config = _kafkaOptions.AsRdkafkaConfig();

            using (var producer = new Producer <Null, string>(config, null, new StringSerializer(Encoding.UTF8)))
            {
                producer.ProduceAsync(topic, null, content);
                producer.Flush();
            }
            return(Task.CompletedTask);
        }