private void ProcessMessage(IList <BrokerMessage> messages, CancellationToken token) { var handle = messages.FirstOrDefault().Handle; Console.WriteLine($"processing handle={handle}..."); using (var broker = new BrokerRepository(QueueName)) { foreach (var message in messages) { if (message.MsgType != "DEFAULT") { continue; } Console.WriteLine($"Received message: {message.Message}"); Thread.Sleep(5000); // simulate a heavy task message.Message += ". Done!"; broker.SendMessageAsync(message, token).Wait(); } broker.EndConversationAsync(messages, token).Wait(); } Console.WriteLine($"processing handle={handle}... finished!"); }
public async Task ConsumeAsync(CancellationToken token) { using (var brokerRepository = new BrokerRepository(QueueName)) { while (!token.IsCancellationRequested) { var messages = await brokerRepository.NextMessagesAsync(token); foreach (var message in messages) { if (message.MsgType != "DEFAULT") { continue; } await Task.Delay(1000); // simulate a heavy task message.Message += ". Done!"; await brokerRepository.SendMessageAsync(message, token); Console.WriteLine($"Received message: {message.Message}"); } await brokerRepository.EndConversationAsync(messages, token); Console.WriteLine("end conversation"); } } }
public async Task ConsumeAsync(CancellationToken token) { using (var broker = new BrokerRepository(QueueName)) { while (!token.IsCancellationRequested) { var messages = await broker.NextMessagesAsync(token); Task.Run(() => ProcessMessage(messages, token), token); } } }