public async Task Subscribe_DeadLetterIsSibscribedThenDisposed_DeadLetterMessagesAreDeliveredCorrectlyToSecondSubscriber() { //prepare IConfiguration configuration = ConfigurationHelper.ProvideConfiguration(); var exchangeName = "Subscribe_DeadLetterIsSibscribedThenUnsibcribed_DeadLetterMessagesAreDeliveredCorrectly.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 accountCreatedSubscriberFactory = container.Resolve <ISubscriberFactory>(); var accountTypedConsumerSubscriberName = "Subscribe_DeadLetterIsSibscribedThenUnsibcribed_DeadLetterMessagesAreDeliveredCorrectly"; ISubscriber typedSubscriber = await accountCreatedSubscriberFactory .CreateSubscriberAsync(accountTypedConsumerSubscriberName , new List <string> { accountcreatedbindingkey }, maxretrycount); var accountCreatedEventType = "accountcreated"; AccountBookingTypedConsumer typedConsumer = new AccountBookingTypedConsumer(10, 0); typedSubscriber.Subscribe(SubscriptionBuilder.Create() .AddSubscription <AccountCreated>(accountCreatedEventType, () => typedConsumer).Build()); //Let's create AccountTypedConsumer for dead letter queue Subscriber var deadLetterSubscriberFactory = container.Resolve <IDeadLetterSubscriberFactory>(); DeadLetterConsumer deadLetterConsumer = new DeadLetterConsumer(0, 0); ISubscriber deadLetterSubscriber = await deadLetterSubscriberFactory.CreateSubscriberAsync(accountTypedConsumerSubscriberName, 1); deadLetterSubscriber.Subscribe(SubscriptionBuilder.Create() .AddDefaultSubscription <DeadLetterEventDescriptor <AccountCreated> >(() => deadLetterConsumer).Build()); //create Publisher var publisher = container.Resolve <IPublisher>(); string accountcreatedroutingKey = "changetracker.entity.create.account"; //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(1000); //Let's check that dead letter consumer processed the message with Type = AccountCreated on the first try. IntegrationEvent <DeadLetterEventDescriptor <AccountCreated> > deadLetterAccountCreatedMessage = deadLetterConsumer.ProcessedAccountCreatedIntegrationEvents.First(); deadLetterConsumer.ProcessedAccountCreatedIntegrationEvents.Count().Should().Be(1); deadLetterAccountCreatedMessage.CorrelationId.Should().Be(accountCreatedIntegrationEvent.CorrelationId); deadLetterAccountCreatedMessage.Content.Original.ShouldBeEquivalentTo(accountCreatedIntegrationEvent); deadLetterConsumer.CountOfAttemptsForAccountCreated.Should().Be(1); //Let's dispose deadLetterSubscriber.Dispose(); //Let's publish the new message that must be sent to the dead letter queue AccountCreated uptickAccountCreated = new AccountCreated() { AccountLevel = 20 }; var uptickAccountCreatedIntegrationEvent = new IntegrationEvent <AccountCreated>() { Content = uptickAccountCreated, EventType = accountCreatedEventType, CorrelationId = Guid.NewGuid() }; await publisher.PublishEventAsync(uptickAccountCreatedIntegrationEvent, accountcreatedroutingKey); //Let's create the new Dead Letter queue Subscriber DeadLetterConsumer secondDeadLetterConsumer = new DeadLetterConsumer(0, 0); ISubscriber secondDeadLetterSubscriber = await deadLetterSubscriberFactory.CreateSubscriberAsync(accountTypedConsumerSubscriberName, 1); secondDeadLetterSubscriber.Subscribe(SubscriptionBuilder.Create() .AddDefaultSubscription <DeadLetterEventDescriptor <AccountCreated> >(() => secondDeadLetterConsumer).Build()); await Task.Delay(1000); //check that the second dead letter qeuee message was sent to the new second consumer. The first should not have recieved new message. deadLetterConsumer.ProcessedAccountCreatedIntegrationEvents.Count().Should().Be(1); deadLetterAccountCreatedMessage.CorrelationId.Should().Be(accountCreatedIntegrationEvent.CorrelationId); deadLetterAccountCreatedMessage.Content.Original.ShouldBeEquivalentTo(accountCreatedIntegrationEvent); deadLetterConsumer.CountOfAttemptsForAccountCreated.Should().Be(1); IntegrationEvent <DeadLetterEventDescriptor <AccountCreated> > uptickAccountDeadLetterAccountCreatedMessage = secondDeadLetterConsumer.ProcessedAccountCreatedIntegrationEvents.First(); secondDeadLetterConsumer.ProcessedAccountCreatedIntegrationEvents.Count().Should().Be(1); uptickAccountDeadLetterAccountCreatedMessage.CorrelationId.Should().Be(uptickAccountCreatedIntegrationEvent.CorrelationId); uptickAccountDeadLetterAccountCreatedMessage.Content.Original.ShouldBeEquivalentTo(uptickAccountCreatedIntegrationEvent); secondDeadLetterConsumer.CountOfAttemptsForAccountCreated.Should().Be(1); }
public async Task Subscribe_DeadLetterMessageIsSentToDeadLetter_DeadLetterIsReadBySubscriber() { //prepare IConfiguration configuration = ConfigurationHelper.ProvideConfiguration(); var exchangeName = "Subscribe_DeadLetterMessageIsSentToDeadLetter_DeadLetterIsReadBySubscriber.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_DeadLetterMessagesAreSentToDeadLetter_DeadLettersAreReadByAppropriateConsumers"; ISubscriber typedSubscriber = await accountBookingCreatedSubscriberFactory .CreateSubscriberAsync(accountBookingTypedConsumerSubscriberName , new List <string> { accountcreatedbindingkey, bookingcreatedbindingkey }, maxretrycount); var accountCreatedEventType = "accountcreated"; var bookingCreatedEventType = "bookingcreated"; AccountBookingTypedConsumer typedConsumer = new AccountBookingTypedConsumer(10, 10); typedSubscriber.Subscribe(SubscriptionBuilder.Create() .AddSubscription <AccountCreated>(accountCreatedEventType, () => typedConsumer) .AddDefaultSubscription <BookingCreated>(() => typedConsumer).Build()); //Let's create AccountBookingTypedConsumer dead letter queue Subscriber var deadLetterSubscriberFactory = container.Resolve <IDeadLetterSubscriberFactory>(); DeadLetterConsumer deadLetterConsumer = new DeadLetterConsumer(0, 0); ISubscriber deadLetterSubscriber = await deadLetterSubscriberFactory.CreateSubscriberAsync(accountBookingTypedConsumerSubscriberName, 1); deadLetterSubscriber.Subscribe(SubscriptionBuilder.Create() .AddSubscription <DeadLetterEventDescriptor <BookingCreated> >(bookingCreatedEventType, () => deadLetterConsumer) .AddDefaultSubscription <DeadLetterEventDescriptor <AccountCreated> >(() => deadLetterConsumer).Build()); //create Publisher var publisher = container.Resolve <IPublisher>(); string accountcreatedroutingKey = "changetracker.entity.create.account"; string bookingcreatedroutingKey = "changetracker.entity.create.booking"; //act AccountCreated accountCreated = new AccountCreated() { AccountLevel = 10 }; var accountCreatedIntegrationEvent = new IntegrationEvent <AccountCreated>() { Content = accountCreated, EventType = accountCreatedEventType, CorrelationId = Guid.NewGuid() }; await publisher.PublishEventAsync(accountCreatedIntegrationEvent, accountcreatedroutingKey); BookingCreated bookingCreated = new BookingCreated() { BookingName = "bookingName" }; var bookingCreatedIntegrationEvent = new IntegrationEvent <BookingCreated>() { Content = bookingCreated, EventType = bookingCreatedEventType, CorrelationId = Guid.NewGuid() }; await publisher.PublishEventAsync(bookingCreatedIntegrationEvent, bookingcreatedroutingKey); //The event must be sent to the deadletter queue. await Task.Delay(500); //Let's check that dead letter consumer got the message. IntegrationEvent <DeadLetterEventDescriptor <AccountCreated> > deadLetterAccountCreatedMessage = deadLetterConsumer.ProcessedAccountCreatedIntegrationEvents.First(); deadLetterConsumer.ProcessedAccountCreatedIntegrationEvents.Count().Should().Be(1); deadLetterAccountCreatedMessage.CorrelationId.Should().Be(accountCreatedIntegrationEvent.CorrelationId); deadLetterAccountCreatedMessage.Content.Original.ShouldBeEquivalentTo(accountCreatedIntegrationEvent); IntegrationEvent <DeadLetterEventDescriptor <BookingCreated> > deadLetterBookingCreatedMessage = deadLetterConsumer.ProcessedBookingCreatedIntegrationEvents.First(); deadLetterConsumer.ProcessedBookingCreatedIntegrationEvents.Count().Should().Be(1); deadLetterBookingCreatedMessage.CorrelationId.Should().Be(bookingCreatedIntegrationEvent.CorrelationId); deadLetterBookingCreatedMessage.Content.Original.ShouldBeEquivalentTo(bookingCreatedIntegrationEvent); }