Constructs a request containing multiple producer requests to send to Kafka.
Inheritance: AbstractRequest, IWritable
        public void WriteToValidFormat()
        {
            List<ProducerRequest> requests = new List<ProducerRequest>
            {
                new ProducerRequest("topic a", 0, new List<Message> { new Message(new byte[10]) }),
                new ProducerRequest("topic a", 0, new List<Message> { new Message(new byte[10]) }),
                new ProducerRequest("topic b", 0, new List<Message> { new Message(new byte[10]) }),
                new ProducerRequest("topic c", 0, new List<Message> { new Message(new byte[10]) })
            };

            MultiProducerRequest request = new MultiProducerRequest(requests);

            // format = len(request) + requesttype + requestcount + requestpackage
            // total byte count = 4 + (2 + 2 + 144)
            MemoryStream ms = new MemoryStream();
            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);
            Assert.AreEqual(156, bytes.Length);

            // first 4 bytes = the length of the request
            Assert.AreEqual(152, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray<byte>()), 0));

            // next 2 bytes = the RequestType which in this case should be Produce
            Assert.AreEqual((short)RequestTypes.MultiProduce, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray<byte>()), 0));

            // next 2 bytes = the number of messages
            Assert.AreEqual((short)4, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray<byte>()), 0));
        }
Exemple #2
0
        /// <summary>
        /// Sends the data to a multiple topics on Kafka server synchronously
        /// </summary>
        /// <param name="requests">
        /// The requests.
        /// </param>
        public void MultiSend(IEnumerable<ProducerRequest> requests)
        {
            Guard.Assert<ArgumentNullException>(() => requests != null);
            Guard.Assert<ArgumentNullException>(
                () => requests.All(
                    x => x != null && x.MessageSet != null && x.MessageSet.Messages != null));
            Guard.Assert<ArgumentNullException>(
                () => requests.All(
                    x => x.MessageSet.Messages.All(
                        y => y != null && y.PayloadSize <= this.Config.MaxMessageSize)));

            var multiRequest = new MultiProducerRequest(requests);
            using (var conn = new KafkaConnection(this.config.Host, this.config.Port))
            {
                conn.Write(multiRequest);
            }
        }
Exemple #3
0
 /// <summary>
 /// Sends the data to a multiple topics on Kafka server synchronously
 /// </summary>
 /// <param name="requests">
 /// The requests.
 /// </param>
 public void MultiSend(IEnumerable<ProducerRequest> requests)
 {
     Guard.NotNull(requests, "requests");
     Guard.Assert<ArgumentNullException>(
         () => requests.All(
             x => x != null && x.MessageSet != null && x.MessageSet.Messages != null));
     Guard.Assert<ArgumentNullException>(
         () => requests.All(
             x => x.MessageSet.Messages.All(
                 y => y != null && y.PayloadSize <= this.Config.MaxMessageSize)));
     this.EnsuresNotDisposed();
     var multiRequest = new MultiProducerRequest(requests);
     this.connection.Write(multiRequest);
 }
Exemple #4
0
 /// <summary>
 /// Writes a multi-producer request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="MultiProducerRequest"/> to send to the server.</param>
 public void Write(MultiProducerRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.NotNull(request, "request");
     this.Write(request.RequestBuffer.GetBuffer());
 }
Exemple #5
0
 /// <summary>
 /// Writes a multi-producer request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="MultiProducerRequest"/> to send to the server.</param>
 public void Write(MultiProducerRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.Assert<ArgumentNullException>(() => request != null);
     this.Write(request.RequestBuffer.GetBuffer(), Timeout.Infinite);
 }