Example #1
0
            public void HandleDeliveryReport(DeliveryReport deliveryReport)
            {
                if (deliveryReport == null)
                {
                    return;
                }

                var dr = new DeliveryReport
                {
                    TopicPartitionOffsetError = deliveryReport.TopicPartitionOffsetError,
                    Message = new Message
                    {
                        Key       = Key,
                        Value     = Value,
                        Timestamp = deliveryReport.Message == null
                            ? new Timestamp(0, TimestampType.NotAvailable)
                            : deliveryReport.Message.Timestamp,
                        Headers = deliveryReport.Message?.Headers
                    }
                };

                // topic is cached in this object, not set in the deliveryReport to avoid the
                // cost of marshalling it.
                dr.Topic = Topic;

                if (Handler != null)
                {
                    Handler(dr);
                }
            }
            public void HandleDeliveryReport(DeliveryReport <Null, Null> deliveryReport)
            {
                if (CancellationTokenRegistration != null)
                {
                    CancellationTokenRegistration.Dispose();
                }

                if (deliveryReport == null)
                {
#if NET45
                    System.Threading.Tasks.Task.Run(() => TrySetResult(null));
#else
                    TrySetResult(null);
#endif
                    return;
                }

                var dr = new DeliveryResult <TKey, TValue>
                {
                    TopicPartitionOffset = deliveryReport.TopicPartitionOffset,
                    Status  = deliveryReport.Status,
                    Message = new Message <TKey, TValue>
                    {
                        Key       = Key,
                        Value     = Value,
                        Timestamp = deliveryReport.Message.Timestamp,
                        Headers   = deliveryReport.Message.Headers
                    }
                };
                // topic is cached in this object, not set in the deliveryReport to avoid the
                // cost of marshalling it.
                dr.Topic = Topic;

#if NET45
                if (deliveryReport.Error.IsError)
                {
                    System.Threading.Tasks.Task.Run(() => SetException(new ProduceException <TKey, TValue>(deliveryReport.Error, dr)));
                }
                else
                {
                    System.Threading.Tasks.Task.Run(() => TrySetResult(dr));
                }
#else
                if (deliveryReport.Error.IsError)
                {
                    TrySetException(new ProduceException <TKey, TValue>(deliveryReport.Error, dr));
                }
                else
                {
                    TrySetResult(dr);
                }
#endif
            }
Example #3
0
            public void HandleDeliveryReport(DeliveryReport deliveryReport)
            {
                if (deliveryReport == null)
                {
#if NET45
                    System.Threading.Tasks.Task.Run(() => TrySetResult(null));
#else
                    TrySetResult(null);
#endif
                    return;
                }

                var dr = new DeliveryResult
                {
                    TopicPartitionOffset = deliveryReport.TopicPartitionOffset,
                    Message = new Message
                    {
                        Key       = Key,
                        Value     = Value,
                        Timestamp = deliveryReport.Message.Timestamp,
                        Headers   = deliveryReport.Message.Headers
                    }
                };
                // topic is cached in this object, not set in the deliveryReport to avoid the
                // cost of marshalling it.
                dr.Topic = Topic;

#if NET45
                if (deliveryReport.Error.IsError)
                {
                    System.Threading.Tasks.Task.Run(() => SetException(new ProduceException(deliveryReport.Error, dr)));
                }
                else
                {
                    System.Threading.Tasks.Task.Run(() => TrySetResult(dr));
                }
#else
                if (deliveryReport.Error.IsError)
                {
                    TrySetException(new ProduceException(deliveryReport.Error, dr));
                }
                else
                {
                    TrySetResult(dr);
                }
#endif
            }
        /// <summary>
        ///     Asynchronously send a single message to a Kafka topic.
        ///     The partition the message is sent to is determined using
        ///     the partitioner defined using the 'partitioner'
        ///     configuration property.
        /// </summary>
        /// <param name="topic">
        ///     The topic to produce the message to.
        /// </param>
        /// <param name="message">
        ///     The message to produce.
        /// </param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used to abort this request.
        /// </param>
        /// <returns>
        ///     A Task which will complete with a delivery report corresponding to
        ///     the produce request, or an exception if an error occured.
        /// </returns>
        public Task <DeliveryReport <TKey, TValue> > ProduceAsync(string topic, Message <TKey, TValue> message, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (this.producer.enableDeliveryReports)
            {
                var handler = new TypedTaskDeliveryHandlerShim(topic,
                                                               producer.enableDeliveryReportKey ? message.Key : default(TKey),
                                                               producer.enableDeliveryReportValue ? message.Value : default(TValue));

                using (cancellationToken.Register(() => handler.TrySetException(new TaskCanceledException())))
                {
                    var keyBytes = keySerializer(topic, message.Key);
                    var valBytes = valueSerializer(topic, message.Value);

                    producer.ProduceImpl(
                        topic,
                        valBytes, 0, valBytes == null ? 0 : valBytes.Length,
                        keyBytes, 0, keyBytes == null ? 0 : keyBytes.Length,
                        message.Timestamp, Partition.Any, message.Headers,
                        handler);

                    return(handler.Task);
                }
            }
            else
            {
                var keyBytes = keySerializer(topic, message.Key);
                var valBytes = valueSerializer(topic, message.Value);

                producer.ProduceImpl(
                    topic,
                    valBytes, 0, valBytes == null ? 0 : valBytes.Length,
                    keyBytes, 0, keyBytes == null ? 0 : keyBytes.Length,
                    message.Timestamp, Partition.Any, message.Headers,
                    null);

                var result = new DeliveryReport <TKey, TValue>
                {
                    TopicPartitionOffset = new TopicPartitionOffset(new TopicPartition(topic, Partition.Any), Offset.Invalid),
                    Message = message
                };

                return(Task.FromResult(result));
            }
        }
 /// <summary>
 ///     Initialize a new instance of ProduceException based on
 ///     an existing Error value.
 /// </summary>
 /// <param name="error">
 ///     The error associated with the delivery report.
 /// </param>
 /// <param name="deliveryReport">
 ///     The delivery report associated with the produce request.
 /// </param>
 public ProduceException(Error error, DeliveryReport <TKey, TValue> deliveryReport)
     : base(error)
 {
     DeliveryReport = deliveryReport;
 }