Sends messages encapsulated in request to Kafka server asynchronously
Inheritance: IAsyncProducer
Exemple #1
0
        /// <summary>
        /// Add a new asynchronous producer to the pool.
        /// </summary>
        /// <param name="broker">The broker informations.</param>
        public override void AddProducer(Broker broker)
        {
            this.EnsuresNotDisposed();
            Guard.NotNull(broker, "broker");
            var asyncConfig = new AsyncProducerConfiguration(this.Config, broker.Id, broker.Host, broker.Port)
            {
                SerializerClass = this.Config.SerializerClass
            };
            var asyncProducer = new AsyncProducer(asyncConfig, this.CallbackHandler);

            Logger.InfoFormat(
                CultureInfo.CurrentCulture,
                "Creating async producer for broker id = {0} at {1}:{2}",
                broker.Id,
                broker.Host,
                broker.Port);
            this.asyncProducers.Add(broker.Id, asyncProducer);
        }
        public void AsyncProducerSendsAndConsumerReceivesSingleSimpleMessage()
        {
            Message sourceMessage = new Message(Encoding.UTF8.GetBytes("test message"));

            var config = new AsyncProducerConfig(clientConfig);
            var producer = new AsyncProducer(config);
            var producerRequest = new ProducerRequest(CurrentTestTopic, 0, new List<Message>() { sourceMessage });

            long currentOffset = TestHelper.GetCurrentKafkaOffset(CurrentTestTopic, clientConfig);

            producer.Send(producerRequest);

            ConsumerConfig consumerConfig = new ConsumerConfig(clientConfig);
            IConsumer consumer = new Consumers.Consumer(consumerConfig);
            FetchRequest request = new FetchRequest(CurrentTestTopic, 0, currentOffset);

            BufferedMessageSet response;
            int totalWaitTimeInMiliseconds = 0;
            int waitSingle = 100;
            while (true)
            {
                Thread.Sleep(waitSingle);
                response = consumer.Fetch(request);
                if (response != null && response.Messages.Count() > 0)
                {
                    break;
                }
                else
                {
                    totalWaitTimeInMiliseconds += waitSingle;
                    if (totalWaitTimeInMiliseconds >= MaxTestWaitTimeInMiliseconds)
                    {
                        break;
                    }
                }
            }

            Assert.NotNull(response);
            Assert.AreEqual(1, response.Messages.Count());
            Message resultMessage = response.Messages.First();
            Assert.AreEqual(sourceMessage.ToString(), resultMessage.ToString());
        }
        public void AsyncProducerSendsManyLongRandomMessages()
        {
            List<Message> messages = GenerateRandomTextMessages(50);

            var config = new AsyncProducerConfig(clientConfig);

            var producer = new AsyncProducer(config);
            producer.Send(CurrentTestTopic, 0, messages);
        }
 public void AsyncProducerSendsMessageWithCallbackClass()
 {
     List<Message> messages = new List<Message>()
                                  {
                                      new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                  };
     var config = new AsyncProducerConfig(clientConfig);
     TestCallbackHandler myHandler = new TestCallbackHandler();
     var producer = new AsyncProducer(config, myHandler);
     producer.Send(CurrentTestTopic, 0, messages);
     Thread.Sleep(1000);
     Assert.IsTrue(myHandler.WasRun);
 }
        public void AsyncProducerSendsFewShortFixedMessages()
        {
            List<Message> messages = new List<Message>()
                                         {
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 2")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 3")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 4"))
                                         };

            var config = new AsyncProducerConfig(clientConfig);

            var producer = new AsyncProducer(config);
            producer.Send(CurrentTestTopic, 0, messages);
        }
Exemple #6
0
        public void AsyncProducerSendsFewShortFixedMessages()
        {
            var prodConfig = this.AsyncProducerConfig1;

            var messages = new List<Message>
                                         {
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 2")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 3")),
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 4"))
                                         };

            using (var producer = new AsyncProducer(prodConfig))
            {
                producer.Send(CurrentTestTopic, 0, messages);
            }
        }
Exemple #7
0
        public void AsyncProducerSendsMessageWithCallbackClass()
        {
            var prodConfig = this.AsyncProducerConfig1;

            var messages = new List<Message>
                                         {
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                         };
            var myHandler = new TestCallbackHandler();
            using (var producer = new AsyncProducer(prodConfig, myHandler))
            {
                producer.Send(CurrentTestTopic, 0, messages);
            }

            Thread.Sleep(1000);
            Assert.IsTrue(myHandler.WasRun);
        }
Exemple #8
0
 public void AsyncProducerSendsManyLongRandomMessages()
 {
     var prodConfig = this.AsyncProducerConfig1;
     List<Message> messages = GenerateRandomTextMessages(50);
     using (var producer = new AsyncProducer(prodConfig))
     {
         producer.Send(CurrentTestTopic, 0, messages);
     }
 }
Exemple #9
0
        public void AsyncProducerSendsFewShortFixedMessagesInSeparateSendActions()
        {
            var prodConfig = this.AsyncProducerConfig1;

            using (var producer = new AsyncProducer(prodConfig))
            {
                var req1 = new ProducerRequest(
                    CurrentTestTopic,
                    0,
                    new List<Message> { new Message(Encoding.UTF8.GetBytes("Async Test Message 1")) });
                producer.Send(req1);

                var req2 = new ProducerRequest(
                    CurrentTestTopic,
                    0,
                    new List<Message> { new Message(Encoding.UTF8.GetBytes("Async Test Message 2")) });
                producer.Send(req2);

                var req3 = new ProducerRequest(
                    CurrentTestTopic,
                    0,
                    new List<Message> { new Message(Encoding.UTF8.GetBytes("Async Test Message 3")) });
                producer.Send(req3);
            }
        }