コード例 #1
0
        public async Task CanCreateQueue_WithMaxLengthAndOverflowModeDropHead()
        {
            var options = new CreateQueueOptionsBuilder(QueueType.Classic)
                          .WithMaxLengthByte(16)
                          .WithOverflowBehaviour(OverflowBehaviour.DropHead)
                          .Build();

            await _serviceClient.CreateQueueAsync(_queueName, options);

            await _serviceClient.EnqueueMessageAsync(_queueName, "some-message-1");

            await Task.Delay(TimeSpan.FromMilliseconds(100));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(1);
            await _serviceClient.EnqueueMessageAsync(_queueName, "some-message-2");

            await Task.Delay(TimeSpan.FromMilliseconds(100));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(1);

            using var channelContainer = await _rabbitMqConnectionManager.AcquireChannel(ChannelType.Consumer);

            var message = Encoding.UTF8.GetString(channelContainer.Channel.BasicGet(_queueName, true).Body.Span);

            message.Should().Be("\"some-message-2\"");
        }
コード例 #2
0
        public static async Task Main()
        {
            var          rabbitMqConnectionManager = new RabbitMqConnectionManager(new Uri("amqp://localhost"), "test", TimeSpan.FromSeconds(30));
            const string queueName = "some-queue-name";
            var          options   = new RabbitMqServiceOptionsBuilder()
                                     .WithRetry(TimeSpan.FromSeconds(15), null, TimeSpan.FromSeconds(1))
                                     .WithDeliveryMode(DeliveryMode.Persistent)
                                     .WithConnectionManager(rabbitMqConnectionManager)
                                     .WithDefaultSerializer(message => Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)), "application/json")
                                     .Build();
            var serviceClient = new RabbitMqServiceClient(options);

            // Create a queue
            await serviceClient.CreateQueueAsync(queueName + "-Error", true);

            // Create a queue with options builder
            var createQueueOption = new CreateQueueOptionsBuilder(QueueType.Classic)
                                    .Durable()
                                    .WithDeadLetterExchange(RabbitMqConstants.DefaultExchangeName)
                                    .WithDeadLetterRoutingKey(queueName + "-Error")
                                    .Build();
            await serviceClient.CreateQueueAsync(queueName, createQueueOption);

            // Listen to queue (Auto reconnect is enabled)
            var consumerOptions = new ConsumerOptionsBuilder <string>()
                                  .WithDefaultDeSerializer(message => JsonConvert.DeserializeObject <string>(Encoding.UTF8.GetString(message.Span)))
                                  .WithSimpleMessageAck()
                                  .WithCustomPipe(async(context, next) =>
            {
                Console.WriteLine("Some logging message before processing");
                await next();
                Console.WriteLine("Some logging message after processing");
            })
                                  .Build();
            var activeConsumer = await serviceClient.StartListeningQueueAsync(queueName, consumerOptions, (message, items, ct) =>
            {
                Console.WriteLine(message);
                return(Task.CompletedTask);
            });

            // Enqueue a message
            await serviceClient.EnqueueMessageAsync(queueName, "some-message");

            await Task.Delay(100);

            // Enqueue using EnqueueQueueClient
            var queueClient = serviceClient.CreateQueueClient(queueName);
            await queueClient.EnqueueMessageAsync("some-other-message");

            // Cancel listening
            activeConsumer.Cancel();

            // Purge the queue
            await serviceClient.PurgeQueueAsync(queueName);

            // Delete a queue
            await serviceClient.DeleteQueueAsync(queueName, false, false);

            await serviceClient.DeleteQueueAsync(queueName + "-Error", false, false);
        }
コード例 #3
0
        public async Task CanCreateQueue_WithLazyMode()
        {
            var options = new CreateQueueOptionsBuilder(QueueType.Classic)
                          .WithLazyMode()
                          .Build();

            await _serviceClient.CreateQueueAsync(_queueName, options);

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(0);
            // FIXME: Is there a way to test this ?
        }
コード例 #4
0
        public async Task CanCreateWithExpires()
        {
            var options = new CreateQueueOptionsBuilder(QueueType.Classic)
                          .WithExpire(100)
                          .Build();

            await _serviceClient.CreateQueueAsync(_queueName, options);

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(0);
            await Task.Delay(TimeSpan.FromMilliseconds(300));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(-1);
        }
コード例 #5
0
        public async Task CanCreateAutoDeleteQueue()
        {
            var options = new CreateQueueOptionsBuilder(QueueType.Classic)
                          .AutoDelete()
                          .Build();

            await _serviceClient.CreateQueueAsync(_queueName, options);

            var activeConsumer = await _serviceClient.StartListeningQueueAsync(_queueName, _consumerOptions, (message, items, ct) => Task.CompletedTask);

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(0);
            activeConsumer.Cancel();
            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(-1);
        }
コード例 #6
0
        public async Task CanCreateQueue_WithTtl()
        {
            var options = new CreateQueueOptionsBuilder(QueueType.Classic)
                          .WithMessageTtl(100)
                          .Build();

            await _serviceClient.CreateQueueAsync(_queueName, options);

            await _serviceClient.EnqueueMessageAsync(_queueName, "some-message");

            await Task.Delay(TimeSpan.FromMilliseconds(50));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(1);
            await Task.Delay(TimeSpan.FromMilliseconds(200));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(0);
        }
コード例 #7
0
        public async Task CanCreateQueue_WithDeadLetterQueue()
        {
            var options = new CreateQueueOptionsBuilder(QueueType.Classic)
                          .WithDeadLetterExchange(RabbitMqConstants.DefaultExchangeName)
                          .WithDeadLetterRoutingKey(_errorQueueName)
                          .Build();

            await _serviceClient.CreateQueueAsync(_errorQueueName, false);

            await _serviceClient.CreateQueueAsync(_queueName, options);

            var activeConsumer = await _serviceClient.StartListeningQueueAsync(_queueName, _consumerOptions, (message, items, ct) => Task.FromException(new Exception("Test")));

            (await _serviceClient.GetMessageCountInQueueAsync(_errorQueueName)).Should().Be(0);
            await _serviceClient.EnqueueMessageAsync(_queueName, "some-message");

            await Task.Delay(TimeSpan.FromMilliseconds(100));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(0);
            (await _serviceClient.GetMessageCountInQueueAsync(_errorQueueName)).Should().Be(1);
            activeConsumer.Cancel();
        }
コード例 #8
0
        public async Task CanCreateQueue_WithSingleActiveConsumer()
        {
            var usedConsumerIds = new List <int>();
            var options         = new CreateQueueOptionsBuilder(QueueType.Classic)
                                  .WithSingleActiveConsumer()
                                  .Build();
            await _serviceClient.CreateQueueAsync(_queueName, options);

            var activeConsumer1 = await _serviceClient.StartListeningQueueAsync(_queueName, _consumerOptions, (message, items, ct) =>
            {
                usedConsumerIds.Add(1);
                return(Task.CompletedTask);
            });

            var activeConsumer2 = await _serviceClient.StartListeningQueueAsync(_queueName, _consumerOptions, (message, items, ct) =>
            {
                usedConsumerIds.Add(2);
                return(Task.CompletedTask);
            });

            for (var i = 0; i < 1_000; i++)
            {
                await _serviceClient.EnqueueMessageAsync(_queueName, "some-message");
            }

            var sw = Stopwatch.StartNew();

            while (usedConsumerIds.Count < 1_000 && sw.ElapsedMilliseconds < 10_000)
            {
                await Task.Delay(TimeSpan.FromMilliseconds(10));
            }

            activeConsumer1.Cancel();
            activeConsumer2.Cancel();
            usedConsumerIds.Should().AllBeEquivalentTo(usedConsumerIds.First());
            usedConsumerIds.Should().HaveCount(1_000);
        }
コード例 #9
0
        public async Task CanCreateQueue_WithMaxLengthAndOverflowModeRejectPublishDlx()
        {
            var options = new CreateQueueOptionsBuilder(QueueType.Classic)
                          .WithDeadLetterExchange(RabbitMqConstants.DefaultExchangeName)
                          .WithDeadLetterRoutingKey(_errorQueueName)
                          .WithMaxLength(1)
                          .WithOverflowBehaviour(OverflowBehaviour.RejectPublishDlx)
                          .Build();

            await _serviceClient.CreateQueueAsync(_errorQueueName, false);

            await _serviceClient.CreateQueueAsync(_queueName, options);

            await _serviceClient.EnqueueMessageAsync(_queueName, "some-message-1");

            await Task.Delay(TimeSpan.FromMilliseconds(100));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(1);
            (await _serviceClient.GetMessageCountInQueueAsync(_errorQueueName)).Should().Be(0);

            // This message should be rejected and enqueue in the errorQueue
            await _serviceClient.EnqueueMessageAsync(_queueName, "some-message-2");

            await Task.Delay(TimeSpan.FromMilliseconds(100));

            (await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(1);
            (await _serviceClient.GetMessageCountInQueueAsync(_errorQueueName)).Should().Be(1);

            using var channelContainer = await _rabbitMqConnectionManager.AcquireChannel(ChannelType.Consumer);

            var message = Encoding.UTF8.GetString(channelContainer.Channel.BasicGet(_queueName, true).Body.Span);

            message.Should().Be("\"some-message-1\"");
            var messageError = Encoding.UTF8.GetString(channelContainer.Channel.BasicGet(_errorQueueName, true).Body.Span);

            messageError.Should().Be("\"some-message-2\"");
        }