public async Task ConsumeAsync_DeadLetterMesssageIsProvided_OriginalMessageIsSentToSubscriberQueue()
        {
            //prepare
            var    publisherMock       = new Mock <IPublisher>();
            string subscriberQueueName = "subscriber";

            IntegrationEvent <DeadLetterEventDescriptor <JObject> > deadLetterIntegrationEvent =
                JsonConvert.DeserializeObject <IntegrationEvent <DeadLetterEventDescriptor <JObject> > >(_serializedMessage);

            IntegrationEvent <JObject> originalMessage = null;

            publisherMock.Setup(x => x.PublishDirectEventAsync(
                                    It.IsAny <IntegrationEvent <JObject> >(), subscriberQueueName, true))
            .Returns <IntegrationEvent <JObject>, string, bool>(
                (integrationEvent, subscriberName, persistant) =>
            {
                originalMessage = integrationEvent;
                return(Task.CompletedTask);
            });

            var deadLetterReloader =
                new DeadLetterReloader <JObject>(publisherMock.Object, subscriberQueueName);
            //act
            await deadLetterReloader.ConsumeAsync(deadLetterIntegrationEvent);

            //check
            originalMessage.ShouldBeEquivalentTo(deadLetterIntegrationEvent.Content.Original);
        }
Esempio n. 2
0
        public async Task Subscribe_DeadLetterMessageIsSentToDeadLetter_DeadLetterIsReadByDeadLetterReloaderAndSentToTheOriginalQueue()
        {
            //prepare
            IConfiguration configuration = ConfigurationHelper.ProvideConfiguration();
            var            exchangeName  =
                "Subscribe_DeadLetterMessageIsSentToDeadLetter_DeadLetterIsReadByDeadLetterReloaderAndSentToTheOriginalQueue.exchangename";

            configuration["rabbitmq:exchangename"]     = exchangeName;
            configuration["rabbitmq:waitexchangename"] = exchangeName.Replace("exchangename", "waitexchangename");

            IContainer container = ConfigurationHelper.ConfigureContainer(configuration);

            //Let's create AccountBookingTypedConsumer Subscriber

            var         maxretrycount            = 0;
            var         accountcreatedbindingkey = "*.entity.create.account";
            var         bookingcreatedbindingkey = "*.entity.create.booking";
            var         accountBookingCreatedSubscriberFactory    = container.Resolve <ISubscriberFactory>();
            var         accountBookingTypedConsumerSubscriberName = "Subscribe_DeadLetterMessageIsSentToDeadLetter_DeadLetterIsReadByDeadLetterReloaderAndSentToTheOriginalQueue";
            ISubscriber typedSubscriber =
                await accountBookingCreatedSubscriberFactory
                .CreateSubscriberAsync(accountBookingTypedConsumerSubscriberName
                                       , new List <string> {
                accountcreatedbindingkey, bookingcreatedbindingkey
            }, maxretrycount);

            var accountCreatedEventType = "accountcreated";
            var bookingCreatedEventType = "bookingcreated";

            AccountBookingTypedConsumer typedConsumer = new AccountBookingTypedConsumer(1, 1);

            typedSubscriber.Subscribe(SubscriptionBuilder.Create()
                                      .AddSubscription <AccountCreated>(accountCreatedEventType,
                                                                        () => typedConsumer)
                                      .AddDefaultSubscription <BookingCreated>(() => typedConsumer).Build());

            //create Publisher

            var publisher = container.Resolve <IPublisher>();

            string accountcreatedroutingKey = "changetracker.entity.create.account";
            string bookingcreatedroutingKey = "changetracker.entity.create.booking";

            //Let's create AccountBookingTypedConsumer dead letter queue Subscriber and use DeadLetterReloader
            var deadLetterSubscriberFactory = container.Resolve <IDeadLetterSubscriberFactory>();

            DeadLetterReloader <AccountCreated> deadLetterreloader = new DeadLetterReloader <AccountCreated>(publisher, accountBookingTypedConsumerSubscriberName);


            ISubscriber deadLetterSubscriber =
                await deadLetterSubscriberFactory.CreateSubscriberAsync(accountBookingTypedConsumerSubscriberName, 1);

            deadLetterSubscriber.Subscribe(SubscriptionBuilder.Create()
                                           .AddDefaultSubscription(() => deadLetterreloader).Build());

            //act
            AccountCreated accountCreated =
                new AccountCreated()
            {
                AccountLevel = 10
            };

            var accountCreatedIntegrationEvent = new IntegrationEvent <AccountCreated>()
            {
                Content       = accountCreated,
                EventType     = accountCreatedEventType,
                CorrelationId = Guid.NewGuid()
            };
            await publisher.PublishEventAsync(accountCreatedIntegrationEvent, accountcreatedroutingKey);

            //The event must be sent to the deadletter queue.
            await Task.Delay(2000);

            //Let's check that dead letter reloader processed the message with Type = AccountCreated and sent it to the original queue.
            //AccountBookingTypedConsumer should process the original message at the second attempt.
            IntegrationEvent <AccountCreated> accountCreatedMessage =
                typedConsumer.ProcessedAccountCreatedIntegrationEvent;

            accountCreatedMessage.CorrelationId.Should().Be(accountCreatedIntegrationEvent.CorrelationId);
            accountCreatedMessage.Content.ShouldBeEquivalentTo(accountCreatedIntegrationEvent.Content);
            typedConsumer.CountOfAttemptsForAccountCreated.Should().Be(2);
            typedConsumer.CountOfAttemptsForBookingCreated.Should().Be(0);
        }