private static void MicrosoftKafkaTest()
        {
            var brokerConfig = new BrokerConfiguration()
            {
                Host = "0.tcp.ngrok.io",
                Port = 15069
            };
            var config = new ProducerConfiguration(new List <BrokerConfiguration> {
                brokerConfig
            });
            var producer = new Kafka.Client.Producers.Producer(config);

            var payload = Encoding.ASCII.GetBytes(@"
            { 
                'method': 'POST', 
                'timeStamp': 1508164715, 
                'foo': 
                    { 
                        'id': 123 
                    } 
            }");
            var message = new Kafka.Client.Messages.Message(payload);
            var data    = new ProducerData <string, Kafka.Client.Messages.Message>("fun-topic", message);

            try
            {
                producer.Send(data);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #2
0
        public int ProduceMessages(string topic, IEnumerable <ProducerData <string, Message> > batchMessages, int timeToExecuteInMilliSeconds)
        {
            watch = Stopwatch.StartNew();
            var brokers = GetBrokerConfigurations();
            var producerConfiguration = new ProducerConfiguration(brokers);

            producerConfiguration.ClientId = "xyz-123-abc";
            while (watch.ElapsedMilliseconds <= timeToExecuteInMilliSeconds)
            {
                using (var kafkaProducer = new Kafka.Client.Producers.Producer(producerConfiguration))
                {
                    kafkaProducer.Send(batchMessages);
                    numberOfRecordsProcessed = numberOfRecordsProcessed + batchMessages.Count();
                }
            }
            watch.Stop();
            Console.WriteLine("{0} messages produced in {1} milliseconds. Thread ID: {2}", numberOfRecordsProcessed, watch.ElapsedMilliseconds, System.Threading.Thread.CurrentThread.ManagedThreadId);
            return(numberOfRecordsProcessed);
        }