コード例 #1
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;
        }
コード例 #2
0
ファイル: Topic.cs プロジェクト: zhijia1122/rdkafka-dotnet
        internal Topic(SafeKafkaHandle kafkaHandle, Producer producer, string topic, TopicConfig config,
                       out LibRdKafka.PartitionerCallback partitionerDelegate)
        {
            // PartitionerDelegate is an out parameter as its reference must be kept outside of Topic
            // it may be called after topic is GC, and it may be different for different topics
            // so we can't simply make it static here
            this.producer = producer;

            config = config ?? new TopicConfig();
            config["produce.offset.report"] = "true";
            IntPtr configPtr = config.handle.Dup();

            if (config.CustomPartitioner != null)
            {
                partitionerDelegate = (IntPtr rkt, IntPtr keydata, UIntPtr keylen, int partition_cnt,
                                       IntPtr rkt_opaque, IntPtr msg_opaque) =>
                {
                    byte[] key = null;
                    if (keydata != IntPtr.Zero)
                    {
                        key = new byte[(int)keylen];
                        Marshal.Copy(keydata, key, 0, (int)keylen);
                    }
                    return(config.CustomPartitioner(this, key, partition_cnt));
                };
                LibRdKafka.topic_conf_set_partitioner_cb(configPtr, partitionerDelegate);
            }
            else
            {
                partitionerDelegate = null;
            }

            handle = kafkaHandle.Topic(topic, configPtr);
        }
コード例 #3
0
ファイル: Topic.cs プロジェクト: liuhd1992/rdkafka-dotnet
        internal Topic(SafeKafkaHandle kafkaHandle, Producer producer, string topic, TopicConfig config)
        {
            this.producer = producer;

            config = config ?? new TopicConfig();
            config["produce.offset.report"] = "true";
            IntPtr configPtr = config.handle.Dup();

            if (config.CustomPartitioner != null)
            {
                PartitionerDelegate = (IntPtr rkt, IntPtr keydata, UIntPtr keylen, int partition_cnt,
                                       IntPtr rkt_opaque, IntPtr msg_opaque) =>
                {
                    byte[] key = null;
                    if (keydata != IntPtr.Zero)
                    {
                        key = new byte[(int)keylen];
                        Marshal.Copy(keydata, key, 0, (int)keylen);
                    }
                    return(config.CustomPartitioner(this, key, partition_cnt));
                };
                LibRdKafka.topic_conf_set_partitioner_cb(configPtr, PartitionerDelegate);
            }

            handle = kafkaHandle.Topic(topic, configPtr);
        }