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); }
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 :) }
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); }
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); }
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); }