예제 #1
0
        public KafkaProducer(Dictionary <string, object> config, string topicName, int partition, EventHandler <IError> dlgtOnError)
        {
            this.config      = config;
            this.topicName   = topicName;
            this.partition   = partition;
            this.dlgtOnError = dlgtOnError;

            var keySerializer   = Factory.CreateSerializer <string>();
            var valueSerializer = Factory.CreateSerializer <T>();

            AssignSerializersIfDefault(ref keySerializer, ref valueSerializer);

            producer = new Producer <string, T>(config, keySerializer, valueSerializer);

            threadProcessing = new ThreadProcessing(() =>
            {
                if (ToBeContinued)
                {
                    KeyValuePair <string, T> pair;
                    while (GetNextDataPair(out pair))
                    {
                        producer.ProduceAsync(topicName, pair.Key, pair.Value, partition, blockIfQueueFull: true);
                    }
                }

                // Tasks are not waited on synchronously (ContinueWith is not synchronous),
                // so it's possible they may still in progress here.
                //producer.Flush(TimeSpan.FromSeconds(10));
            },
                                                    e =>
            {
                dlgtOnError(null, new Error((int)ErrorCode.Unknown, e.Message));
                LogToScreen.Exception(e);
            });
        }
        public KafkaConsumer(Dictionary <string, object> config, string topicName, int partition, int offset,
                             MessageProcessingDelegate <T> dlgtMessageProcessing, int consumerTimeoutMs,
                             EventHandler <IError> dlgtOnError)
        {
            if (dlgtMessageProcessing == null)
            {
                return;
            }

            var keyDeserializer   = Factory.CreateDeserializer <string>();
            var valueDeserializer = Factory.CreateDeserializer <T>();

            AssignDeserializersIfDefault(ref keyDeserializer, ref valueDeserializer);

            consumer = GetConsumer(config, topicName, partition, offset, (sender, e) => dlgtOnError(sender, (Error)e),
                                   keyDeserializer, valueDeserializer);

            consumer.OnMessage += (sender, msg) =>
            {
                Interlocked.Exchange(ref lastOffset, msg.Offset.Value);

                Task.Run(() =>
                {
                    try
                    {
                        dlgtMessageProcessing(new Message <T>(msg));
                    }
                    catch (Exception e)
                    {
                        dlgtOnError(null, new Error((int)ErrorCode.Unknown, e.Message));
                        LogToScreen.Exception(e);
                    }
                });
            };

            threadProcessing = new ThreadProcessing(() =>
            {
                while (ToBeContinued)
                {
                    consumer.Poll(consumerTimeoutMs);
                }
            },
                                                    e =>
            {
                dlgtOnError(null, new Error((int)ErrorCode.Unknown, e.Message));
                LogToScreen.Exception(e);
            });
        }