コード例 #1
0
        public void Subscribe(string topicName)
        {
            _consumer.OnMessage += async(_, msg) =>
            {
                Interlocked.Increment(ref _inProcessingMessages);
                var businessMsg = new Business.Message(msg.Value, msg.Key, _consumerGroup, _consumer.MemberId, topicName, msg.Partition, msg.Offset.Value);
                await _handler.HandleAsync(businessMsg);

                Interlocked.Decrement(ref _inProcessingMessages);


                //await CommitMessageAsync();
                //CommitMessageAsync(_consumer).Wait();
            };

            _consumer.OnPartitionsAssigned += (_, partitions) =>
            {
                Console.WriteLine($"Assigned partitions: [{string.Join(", ", partitions)}]");
                _consumer.Assign(partitions);
            };

            _consumer.OnPartitionsRevoked += (_, partitions) =>
            {
                Console.WriteLine($"Revoked partitions: [{string.Join(", ", partitions)}]");
                _consumer.Unassign();
            };

            _consumer.Subscribe(topicName);
        }
コード例 #2
0
        public async Task ConsumeProcessAndCommitAsync()
        {
            if (!_consumer.Consume(out Message <string, string> msg, TimeSpan.FromMilliseconds(100)))
            {
                return;
            }

            var businessMsg = new Business.Message(msg.Value, msg.Key, _consumerGroup, _consumer.MemberId, msg.Topic, msg.Partition, msg.Offset.Value);
            await _handler.HandleAsync(businessMsg);

            await CommitMessageAsync(_consumer); // non-blocking :)
        }
コード例 #3
0
        private async Task <bool> ConsumeAndProcessOneMessageAsync()
        {
            _consumer.Consume(out Message <string, string> msg, TimeSpan.FromMilliseconds(100));
            if (msg == null)
            {
                return(false);
            }

            var businessMsg = new Business.Message(msg.Value, msg.Key, _consumerGroup, _consumer.MemberId, msg.Topic, msg.Partition, msg.Offset.Value);
            await _handler.HandleAsync(businessMsg);

            return(true);
        }
コード例 #4
0
        public void Subscribe(string topicName)
        {
            _consumer.OnMessage += async(_, msg) =>
            {
                var businessMsg = new Business.Message(msg.Value, msg.Key, _consumerGroup, _consumer.MemberId, topicName, msg.Partition, msg.Offset.Value);
                await _handler.HandleAsync(businessMsg);
            };

            _consumer.OnPartitionsAssigned += (_, partitions) =>
            {
                Console.WriteLine($"Assigned partitions: [{string.Join(", ", partitions)}]");
                _consumer.Assign(partitions);
            };

            _consumer.OnPartitionsRevoked += (_, partitions) =>
            {
                Console.WriteLine($"Revoked partitions: [{string.Join(", ", partitions)}]");
                _consumer.Unassign();
            };

            _consumer.Subscribe(topicName);
        }
コード例 #5
0
 private async Task ProcessMessageAsync(Message <string, string> msg)
 {
     var businessMsg = new Business.Message(msg.Value, msg.Key, _consumerGroup, _consumer.MemberId, msg.Topic, msg.Partition, msg.Offset.Value);
     await _handler.HandleAsync(businessMsg);
 }