public void ShouldUseDeadLetterQueueOnException() { _subscriber = new RabbitMqSubscriber <string>( EmptyLogFactory.Instance, _settings, new DeadQueueErrorHandlingStrategy(EmptyLogFactory.Instance, _settings)) .CreateDefaultBinding() .SetMessageDeserializer(new DefaultStringDeserializer()); const string expected = "GetDefaultHost message"; SetupNormalQueue(); PublishToQueue(expected); var completeLock = new ManualResetEventSlim(false); var handler = new Func <string, Task>(s => { completeLock.Set(); throw new Exception(); }); _subscriber.Subscribe(handler); _subscriber.Start(); completeLock.Wait(); var result = ReadFromQueue(PoisonQueueName); Assert.That(result, Is.EqualTo(expected)); }
public void ShouldUseDeadLetterQueueOnException() { _subscriber = new RabbitMqSubscriber <string>( new NullLogger <RabbitMqSubscriber <string> >(), _settings) .UseMiddleware(new ExceptionSwallowMiddleware <string>(new NullLogger <ExceptionSwallowMiddleware <string> >())) .CreateDefaultBinding() .SetMessageDeserializer(new DefaultStringDeserializer()); const string expected = "GetDefaultHost message"; SetupNormalQueue(); PublishToQueue(expected); var completeLock = new ManualResetEventSlim(false); var handler = new Func <string, Task>(s => { completeLock.Set(); throw new Exception(); }); _subscriber.Subscribe(handler); _subscriber.Start(); completeLock.Wait(); var result = ReadFromQueue(PoisonQueueName); Assert.That(result, Is.EqualTo(expected)); }
public void ReceivingMessagesWhenOneRabbitIsNotReachable() { // arrange var settings = new RabbitMqSubscriptionSettings { ConnectionString = InvalidConnectionString, ExchangeName = ExchangeName, IsDurable = true, QueueName = QueueName }; _subscriber = new RabbitMqSubscriber <string>( EmptyLogFactory.Instance, settings, new DefaultErrorHandlingStrategy(EmptyLogFactory.Instance, settings)) .SetMessageDeserializer(new DefaultStringDeserializer()) .SetMessageReadStrategy(new MessageReadQueueStrategy()) .SetAlternativeExchange(AlternativeConnectionString) .SetDeduplicator(new InMemoryDeduplcator()) .CreateDefaultBinding(); var handler = new Func <string, Task>(s => { _messagesCount++; return(Task.CompletedTask); }); _subscriber.Subscribe(handler); _subscriber.Start(); // act { var factory = new ConnectionFactory { Uri = new Uri(AlternativeConnectionString) }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("1")); channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("3")); channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("4")); } } Thread.Sleep(TimeSpan.FromSeconds(2)); // assert Assert.AreEqual(3, _messagesCount); }
public async Task StartAsync(CancellationToken cancellationToken) { if (!await _featureManager.IsEnabledAsync(BrokerFeature.ProductComplexityWarning)) { _log.LogInformation("{Component} not started because product complexity warning feature is disabled for broker", nameof(OrderHistoryListener)); return; } if (_settings.ComplexityWarningsCount <= 0) { throw new InvalidOperationException($"Broker {_settings.BrokerId} feature {BrokerFeature.ProductComplexityWarning} is enabled, " + $"but {nameof(_settings.ComplexityWarningsCount)} = {_settings.ComplexityWarningsCount} is not valid "); } _subscriber .Subscribe(this.Handle) .Start(); }
public void ShouldExecuteHelloWorldCommand() { using (var receiver = new RabbitMqSubscriber <RabbitMqMessage <HelloWorldCommand> >(new RabbitMqConfig())) { receiver.Subscribe(AssertReceivedMessage); var message = new RabbitMqMessage <HelloWorldCommand> { Payload = new HelloWorldCommand { Input = "Hola!" } }; var dispatcher = container.Resolve <ICommandDispatcher>(); dispatcher.SubmitRabbitMqCommand(message); while (!messageReceived) { log.Info("Waiting for a message receiver"); } } }
public TradingSignalsHandler(IEnumerable <Exchange> exchanges, ILog logger, IHandler <ExecutionReport> acknowledHandler, IHandler <ExecutionReport> tradeHandler, TranslatedSignalsRepository translatedSignalsRepository, TimeSpan apiTimeout, RabbitMqSubscriber <TradingSignal> messageProducer, bool enabled) { this.exchanges = exchanges.ToDictionary(k => k.Name); this.logger = logger; _acknowledHandler = acknowledHandler; _tradeHandler = tradeHandler; this.translatedSignalsRepository = translatedSignalsRepository; this.apiTimeout = apiTimeout; _messageProducer = messageProducer; _enabled = enabled; if (enabled) { messageProducer.Subscribe(Handle); } }
public void SuccessfulPath() { const string expected = "GetDefaultHost message"; string result = null; SetupNormalQueue(); var completeLock = new ManualResetEventSlim(false); var handler = new Func <string, Task>(s => { result = s; completeLock.Set(); return(Task.CompletedTask); }); _subscriber.Subscribe(handler); _subscriber.Start(); PublishToQueue(expected); completeLock.Wait(); Assert.That(result, Is.EqualTo(expected)); }
public void SubscribeMessageHandler(Func <T, Task> callback) { _connector.Subscribe(callback); }
public void ReceivingAndDeduplicationMessagesFromBothExchanges() { // arrange var settings = new RabbitMqSubscriptionSettings { ConnectionString = ConnectionString, ExchangeName = ExchangeName, IsDurable = true, QueueName = QueueName }; _subscriber = new RabbitMqSubscriber <string>( new NullLogger <RabbitMqSubscriber <string> >(), settings) .UseMiddleware(new InMemoryDeduplicationMiddleware <string>()) .UseMiddleware(new ExceptionSwallowMiddleware <string>(new NullLogger <ExceptionSwallowMiddleware <string> >())) .SetMessageDeserializer(new DefaultStringDeserializer()) .SetMessageReadStrategy(new MessageReadQueueStrategy()) .SetAlternativeExchange(AlternativeConnectionString) .CreateDefaultBinding(); var handler = new Func <string, Task>(s => { _messagesCount++; return(Task.CompletedTask); }); _subscriber.Subscribe(handler); _subscriber.Start(); // act { var factory = new ConnectionFactory { Uri = new Uri(ConnectionString, UriKind.Absolute) }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("1")); channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("2")); channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("4")); } } { var factory = new ConnectionFactory { Uri = new Uri(AlternativeConnectionString, UriKind.Absolute) }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("1")); channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("3")); channel.BasicPublish(ExchangeName, "", body: Encoding.UTF8.GetBytes("4")); } } Thread.Sleep(TimeSpan.FromSeconds(2)); // assert Assert.AreEqual(4, _messagesCount); }