Exemplo n.º 1
0
        static void Main(string[] args)
        {
            using var channel = RabbitMqHelpers.GetRabbitMqChannel();

            while (true)
            {
                Console.WriteLine("Enter a new message:");
                var input = Console.ReadLine();

                var body = GetSerializedMessage(input);

                channel.BasicPublish(exchange: "", routingKey: "hello", body: Encoding.UTF8.GetBytes(body));
                Console.WriteLine(" [x] Sent {0}", body);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            _keyRetries = new Dictionary <Guid, int>();

            using var channel = RabbitMqHelpers.GetRabbitMqChannel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, eventArgs) =>
            {
                Console.WriteLine($"Received message");

                var message = GetMessageFromEvent(eventArgs);
                try
                {
                    MessageService.ProcessMessage(message);
                    channel.BasicAck(deliveryTag: eventArgs.DeliveryTag, multiple: false);
                }
                catch (Exception ex)
                {
                    if (ShouldRetry(message.Key))
                    {
                        if (!_keyRetries.ContainsKey(message.Key))
                        {
                            _keyRetries.Add(message.Key, 1);
                        }
                        _keyRetries[message.Key]++;
                        Console.WriteLine($"FAILED. Number of attempts now {_keyRetries[message.Key]}\nException: {ex.Message}");
                        channel.BasicNack(deliveryTag: eventArgs.DeliveryTag, multiple: false, requeue: true);
                    }
                    else
                    {
                        Console.WriteLine("Exceeded maximum retries - ABORTING");
                        channel.BasicAck(deliveryTag: eventArgs.DeliveryTag, multiple: false);
                    }
                }
            };

            channel.BasicConsume(queue: "hello", autoAck: false, consumer: consumer);

            Console.ReadKey();
        }