コード例 #1
0
 internal void CommitCallback(IntPtr rk,
                              ErrorCode err,
                              /* rd_kafka_topic_partition_list_t * */ IntPtr offsets,
                              IntPtr opaque)
 {
     OnOffsetCommit?.Invoke(this, new OffsetCommitArgs()
     {
         Error   = err,
         Offsets = SafeKafkaHandle.GetTopicPartitionOffsetList(offsets)
     });
 }
コード例 #2
0
        public Consumer(
            IEnumerable <KeyValuePair <string, object> > config,
            IDeserializer <TKey> keyDeserializer,
            IDeserializer <TValue> valueDeserializer)
        {
            KeyDeserializer   = keyDeserializer;
            ValueDeserializer = valueDeserializer;

            // TODO: allow deserializers to be set in the producer config IEnumerable<KeyValuePair<string, object>>.

            if (KeyDeserializer == null)
            {
                if (typeof(TKey) != typeof(Null))
                {
                    throw new ArgumentNullException("Key deserializer must be specified.");
                }
                // TKey == Null -> cast is always valid.
                KeyDeserializer = (IDeserializer <TKey>) new NullDeserializer();
            }

            if (ValueDeserializer == null)
            {
                if (typeof(TValue) != typeof(Null))
                {
                    throw new ArgumentNullException("Value deserializer must be specified.");
                }
                // TValue == Null -> cast is always valid.
                ValueDeserializer = (IDeserializer <TValue>) new NullDeserializer();
            }

            consumer                       = new Consumer(config);
            consumer.OnLog                += (sender, e) => OnLog?.Invoke(sender, e);
            consumer.OnError              += (sender, e) => OnError?.Invoke(sender, e);
            consumer.OnStatistics         += (sender, e) => OnStatistics?.Invoke(sender, e);
            consumer.OnPartitionsAssigned += (sender, e) => OnPartitionsAssigned?.Invoke(sender, e);
            consumer.OnPartitionsRevoked  += (sender, e) => OnPartitionsRevoked?.Invoke(sender, e);
            consumer.OnOffsetCommit       += (sender, e) => OnOffsetCommit?.Invoke(sender, e);
            // TODO: bypass this.consumer for this event to optimize perf.
            consumer.OnMessage += (sender, e) => OnMessage?.Invoke(sender,
                                                                   new Message <TKey, TValue> (
                                                                       e.Topic,
                                                                       e.Partition,
                                                                       e.Offset,
                                                                       KeyDeserializer.Deserialize(e.Key),
                                                                       ValueDeserializer.Deserialize(e.Value),
                                                                       e.Timestamp,
                                                                       e.Error
                                                                       )
                                                                   );
            consumer.OnPartitionEOF += (sender, e) => OnPartitionEOF?.Invoke(sender, e);
        }