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; }
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); }
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); }