Example #1
0
        public Task <DeliveryReport> Produce(byte[] payload, byte[] key = null, Int32 partition = RD_KAFKA_PARTITION_UA)
        {
            // Passes the TaskCompletionSource to the delivery report callback
            // via the msg_opaque pointer
            var deliveryCompletionSource = new TaskCompletionSource <DeliveryReport>();
            var gch = GCHandle.Alloc(deliveryCompletionSource);

            while (true)
            {
                if (handle.Produce(payload, key, partition, GCHandle.ToIntPtr(gch)) == 0)
                {
                    // Successfully enqueued produce request
                    break;
                }

                ErrorCode err = LibRdKafka.last_error();
                if (err == ErrorCode._QUEUE_FULL)
                {
                    // Wait and retry
                    Task.Delay(TimeSpan.FromMilliseconds(50)).Wait();
                }
                else
                {
                    gch.Free();
                    throw RdKafkaException.FromErr(err, "Could not produce message");
                }
            }

            return(deliveryCompletionSource.Task);
        }
Example #2
0
        private void Produce(
            string topic,
            byte[] val, int valOffset, int valLength,
            byte[] key, int keyOffset, int keyLength,
            long?timestamp,
            Int32 partition, bool blockIfQueueFull,
            IDeliveryHandler deliveryHandler)
        {
            SafeTopicHandle topicHandle = getKafkaTopicHandle(topic);

            if (!this.disableDeliveryReports && deliveryHandler != null)
            {
                // Passes the TaskCompletionSource to the delivery report callback via the msg_opaque pointer
                var deliveryCompletionSource = deliveryHandler;
                var gch = GCHandle.Alloc(deliveryCompletionSource);
                var ptr = GCHandle.ToIntPtr(gch);

                if (topicHandle.Produce(val, valOffset, valLength, key, keyOffset, keyLength, partition, timestamp, ptr, blockIfQueueFull) != 0)
                {
                    var err = LibRdKafka.last_error();
                    gch.Free();
                    throw new KafkaException(err, "Could not produce message");
                }

                return;
            }

            if (topicHandle.Produce(val, valOffset, valLength, key, keyOffset, keyLength, partition, timestamp, IntPtr.Zero, blockIfQueueFull) != 0)
            {
                var err = LibRdKafka.last_error();
                throw new KafkaException(err, "Could not produce message");
            }

            return;
        }
Example #3
0
        private void Produce(byte[] payload, byte[] key, Int32 partition, object deliveryHandler)
        {
            var gch = GCHandle.Alloc(deliveryHandler);
            var ptr = GCHandle.ToIntPtr(gch);

            while (true)
            {
                if (handle.Produce(payload, key, partition, ptr) == 0)
                {
                    // Successfully enqueued produce request
                    break;
                }

                var err = LibRdKafka.last_error();
                if (err == ErrorCode._QUEUE_FULL)
                {
                    // Wait and retry
                    Task.Delay(TimeSpan.FromMilliseconds(50)).Wait();
                }
                else
                {
                    gch.Free();
                    throw RdKafkaException.FromErr(err, "Could not produce message");
                }
            }
        }
Example #4
0
        private void Produce(byte[] payload, int payloadCount, byte[] key, int keyCount, Int32 partition, object deliveryHandler, bool blockIfQueueFull)
        {
            var gch = GCHandle.Alloc(deliveryHandler);
            var ptr = GCHandle.ToIntPtr(gch);

            if (handle.Produce(payload, payloadCount, key, keyCount, partition, ptr, blockIfQueueFull) != 0)
            {
                var err = LibRdKafka.last_error();
                gch.Free();
                throw RdKafkaException.FromErr(err, "Could not produce message");
            }
        }