/// <inheritdoc />
        public async Task StartAsync(IEnumerable <string> topics, HandleMessageAsync handleMessageObject, CancellationToken?cancellationToken = null)
        {
            this.currentIndexes      = topics.ToDictionary(x => x, x => - 1);
            this.handleMessageObject = handleMessageObject;

            while (cancellationToken.KeepRunning())
            {
                var updatedTopics = this.GetTopicsWithNewMessages();
                while (cancellationToken.KeepRunning() && !updatedTopics.Any())
                {
                    Thread.Sleep(100);
                    updatedTopics = this.GetTopicsWithNewMessages();
                }

                if (!cancellationToken.KeepRunning())
                {
                    break;
                }

                foreach (var topic in updatedTopics)
                {
                    await this.HandleMessageInTopic(topic);
                }
            }
        }
Example #2
0
 /// <summary>
 /// </summary>
 /// <param name="handleMessageDelegate">Handles messages</param>
 /// <param name="handleDisconnectionDelegate">Called when there is a problem with the reader. The stream will be closed before this is called.</param>
 /// <param name="handleKeepAliveDelegate">Useful if you need to keep writing data every xxx seconds</param>
 /// <param name="numReaders">How many tasks to spawn to read from the channel</param>
 /// <param name="handleMessagesAsynchronously">
 /// Set this to true if you are using a bounded reader channel and you call WriteRequestAsync inside of your handleMessageDelegate.
 /// If you leave it false, you can run into a situation where your reader channel is being throttled, so the responses for WriteRequest will never come back,
 /// which will cause even more blocking on the whole read pipeline.
 /// </param>
 /// <param name="keepAliveTimeSpan">How long until we wait until we invoke the keep alive delegate. Default is 30 seconds.</param>
 public EventMessageStream(
     IMessageDeserializer <T> deserializer,
     IMessageSerializer <T> serializer,
     IDuplexMessageStream duplexMessageStream,
     HandleMessageAsync handleMessageDelegate,
     HandleKeepAliveAsync handleKeepAliveDelegate,
     UnexpectedCloseDelegateAsync unexpectedCloseDelegate,
     EventMessageStreamOptions options             = null,
     RequestResponseKeyResolver <T> rpcKeyResolver = null)
     : base(deserializer, serializer, duplexMessageStream, unexpectedCloseDelegate, options ?? new EventMessageStreamOptions(), rpcKeyResolver)
 {
     this.eventOptions            = this.options as EventMessageStreamOptions;
     this.handleMessageDelegate   = handleMessageDelegate;
     this.handleKeepAliveDelegate = handleKeepAliveDelegate;
 }
Example #3
0
        public async Task StartAsync(
            IEnumerable <string> topics,
            HandleMessageAsync handleMessageObject,
            CancellationToken?cancellationToken = null)
        {
            this.handleMessageObject = handleMessageObject;

            Console.WriteLine("Listening to Kafka messages");

            this.consumer.OnError += (_, error) => Console.WriteLine($"Error: {error}");

            if (this.consumeFromOffset != null)
            {
                var topicPartitionOffsets = this.consumeFromOffset.Select(x => new TopicPartitionOffset(
                                                                              new TopicPartition(x.Key, 0),
                                                                              new Offset(x.Value)));
                this.consumer.Assign(topicPartitionOffsets);
            }

            var topicsWithoutOffsets = topics.Except(this.consumeFromOffset.Keys);

            this.consumer.Subscribe(topicsWithoutOffsets);

            while (cancellationToken.KeepRunning())
            {
                var result = this.consumer.Consume(TimeSpan.FromMilliseconds(100));
                if (result == null)
                {
                    continue;
                }

                Console.WriteLine($"Topic: {result.Topic} Partition: {result.Partition} Offset: {result.Offset} {result.Value}");

                await this.HandleMessage(result);

                Console.WriteLine($"Committing offset");
                var committedOffsets = this.consumer.Commit(result);
                Console.WriteLine($"Committed offset: {committedOffsets}");
            }
        }