예제 #1
0
        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!");
        }
예제 #2
0
        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");
                }
            }
        }
예제 #3
0
        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);
                }
            }
        }