public async Task PublishAsync_Should_Sent_To_Service_Exchange() { try { var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom); networkInfos.ServiceExchangeDescriptions.Add(new RabbitExchangeDescription("pub1_exchange")); var config = new RabbitPublisherConfiguration { ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos }; RabbitCommonTools.DeclareExchangesAndQueueForPublisher(channel, config); channel.QueueDeclare("CQELight"); channel.QueueBind("CQELight", "pub1_exchange", ""); var publisher = new RabbitPublisher( loggerFactory, config); await publisher.PublishEventAsync(new TestEvent()); var result = channel.BasicGet("CQELight", true); result.Should().NotBeNull(); Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestCommand>().Should().NotBeNull(); } finally { DeleteData(); } }
public async Task DispatchAsync_Should_Sent_To_FirstNamespacePart_By_Convention() { try { var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom); networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("CQELight")); var config = new RabbitPublisherConfiguration { ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos }; var publisher = new RabbitPublisher( loggerFactory, config); await publisher.DispatchAsync(new TestCommand()); var result = channel.BasicGet("CQELight", true); result.Should().NotBeNull(); Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestCommand>().Should().NotBeNull(); } finally { DeleteData(); } }
static void Main(string[] args) { Console.WriteLine("Trying to connect to RabbitMQ instance @locahost with guest:guest"); var loggerFactory = new LoggerFactory(); var network = RabbitNetworkInfos.GetConfigurationFor("client", RabbitMQExchangeStrategy.SingleExchange); //This network will produce a client_queue queue, bound to cqelight_global_exchange new Bootstrapper() .UseRabbitMQ( ConnectionInfosHelper.GetConnectionInfos("client"), network ) .UseAutofacAsIoC(c => c.Register(_ => loggerFactory).AsImplementedInterfaces().ExternallyOwned()) .Bootstrapp(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfuly connected to RabbitMQ"); Console.ResetColor(); Console.WriteLine("Enter your message and press enter to send to server, or enter 'quit' to exit process"); var data = Console.ReadLine(); while (data != "quit") { CoreDispatcher.PublishEventAsync(new NewMessage { Payload = data }); data = Console.ReadLine(); } }
public async Task OneExchangePerService_NetworkConfiguration_AsExpected_Event() { try { bool eventReceived = false; var networkInfos = RabbitNetworkInfos.GetConfigurationFor("sub1", RabbitMQExchangeStrategy.ExchangePerService); networkInfos.DistantExchangeDescriptions.Add( new RabbitExchangeDescription(firstProducerEventExchangeName) ); var serviceQueue = networkInfos.ServiceQueueDescriptions[0]; serviceQueue.Bindings.Add(new RabbitQueueBindingDescription(firstProducerEventExchangeName)); var config = new RabbitSubscriberConfiguration { UseDeadLetterQueue = false, ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos, DispatchInMemory = false }; config.EventCustomCallback = (e) => eventReceived = e is RabbitEvent; var subscriber = new RabbitSubscriber( _loggerFactory, config, scopeFactory); subscriber.Start(); var enveloppeWithFirstEvent = GetEnveloppeDataForEvent(publisher: "pub1", content: "data"); _channel.BasicPublish( exchange: firstProducerEventExchangeName, routingKey: "", basicProperties: null, body: enveloppeWithFirstEvent); int awaitedTime = 0; while (awaitedTime <= 2000) { if (eventReceived) { break; } await Task.Delay(10); awaitedTime += 10; } eventReceived.Should().BeTrue(); } finally { DeleteData(); } }
public async Task RabbitMQSubscriber_Should_Consider_AckStrategy_Ack_On_Receive_Fail_Should_Remove_MessageFromQueue_CallbackExc() { try { var networkInfos = RabbitNetworkInfos.GetConfigurationFor("sub1", RabbitMQExchangeStrategy.SingleExchange); var config = new RabbitSubscriberConfiguration { UseDeadLetterQueue = true, ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos, AckStrategy = AckStrategy.AckOnReceive }; config.EventCustomCallback += (_) => throw new InvalidOperationException(); var subscriber = new RabbitSubscriber( _loggerFactory, config); subscriber.Start(); var evt = new ExceptionEvent(); _channel.BasicPublish( Consts.CONST_CQE_EXCHANGE_NAME, "", body: Encoding.UTF8.GetBytes( JsonConvert.SerializeObject( new Enveloppe( JsonConvert.SerializeObject(evt), typeof(ExceptionEvent), publisher1Name)))); await Task.Delay(250); int awaitedTime = 0; BasicGetResult result = null; while (awaitedTime <= 750) { result = _channel.BasicGet(Consts.CONST_DEAD_LETTER_QUEUE_NAME, true); if (result != null) { break; } await Task.Delay(10); awaitedTime += 10; } result.Should().BeNull(); } finally { DeleteData(); } }
public async Task RabbitSubscriber_Should_Consider_AckStrategy_Ack_On_Success_CallbackExc() { try { bool eventReceived = false; var messages = new List <object>(); var networkInfos = RabbitNetworkInfos.GetConfigurationFor("sub1", RabbitMQExchangeStrategy.SingleExchange); var config = new RabbitSubscriberConfiguration { UseDeadLetterQueue = true, ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos, DispatchInMemory = false }; config.EventCustomCallback = (e) => { messages.Add(e); eventReceived = true; }; var subscriber = new RabbitSubscriber( _loggerFactory, config); subscriber.Start(); var evt = new AutoAckEvent(); _channel.BasicPublish( Consts.CONST_CQE_EXCHANGE_NAME, "", body: Encoding.UTF8.GetBytes( JsonConvert.SerializeObject( new Enveloppe( JsonConvert.SerializeObject(evt), typeof(AutoAckEvent), publisher1Name)))); int awaitedTime = 0; while (awaitedTime <= 2000) { if (eventReceived) { break; } await Task.Delay(10); awaitedTime += 10; } eventReceived.Should().BeTrue(); var result = _channel.BasicGet(Consts.CONST_DEAD_LETTER_QUEUE_NAME, true); result.Should().BeNull(); } finally { DeleteData(); } }
public async Task Command_Should_Be_Send_AsDirect() { try { bool commandReceived = false; var networkInfos = RabbitNetworkInfos.GetConfigurationFor("sub1", RabbitMQExchangeStrategy.SingleExchange); var serviceQueue = networkInfos.ServiceQueueDescriptions[0]; var config = new RabbitSubscriberConfiguration { UseDeadLetterQueue = false, ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos, DispatchInMemory = false }; config.CommandCustomCallback = (c) => commandReceived = c is RabbitCommand; var subscriber = new RabbitSubscriber( _loggerFactory, config, scopeFactory); subscriber.Start(); var enveloppeWithCommand = GetEnveloppeDataForCommand(publisher: "pub1", content: "data"); _channel.BasicPublish( exchange: "", routingKey: serviceQueue.QueueName, basicProperties: null, body: enveloppeWithCommand); int awaitedTime = 0; while (awaitedTime <= 2000) { if (commandReceived) { break; } await Task.Delay(10); awaitedTime += 10; } commandReceived.Should().BeTrue(); } finally { DeleteData(); } }
public async Task PublishAsync_RoutingKey_Topic_Should_BeConsidered() { try { var fakeRoutingKeyFactory = new Mock <IRoutingKeyFactory>(); fakeRoutingKeyFactory .Setup(m => m.GetRoutingKeyForEvent(It.IsAny <object>())) .Returns("cqelight.events.testevent"); var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom); networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("CQELight")); networkInfos.ServiceExchangeDescriptions.Add(new RabbitExchangeDescription("MyCustomExchange") { ExchangeType = ExchangeType.Topic }); var queue = new RabbitQueueDescription("MyCustomQueue"); queue.Bindings.Add(new RabbitQueueBindingDescription("MyCustomExchange") { RoutingKeys = new List <string> { "*.events.*" } }); networkInfos.ServiceQueueDescriptions.Add(queue); var config = new RabbitPublisherConfiguration { ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos, RoutingKeyFactory = fakeRoutingKeyFactory.Object }; var publisher = new RabbitPublisher( loggerFactory, config); await publisher.PublishEventAsync(new TestEvent()); channel.BasicGet("CQELight", true).Should().BeNull(); var result = channel.BasicGet("MyCustomQueue", true); result.Should().NotBeNull(); Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestEvent>().Should().NotBeNull(); } finally { DeleteData(); channel.QueueDelete("MyCustomQueue"); channel.ExchangeDelete("MyCustomExchange"); } }
public async Task OneExchange_Network_Configuration_AsExpected_Event() { try { bool eventReceived = false; var networkInfos = RabbitNetworkInfos.GetConfigurationFor("sub1", RabbitMQExchangeStrategy.SingleExchange); var config = new RabbitSubscriberConfiguration { UseDeadLetterQueue = false, ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos, DispatchInMemory = false }; config.EventCustomCallback = (e) => eventReceived = e is RabbitEvent; var subscriber = new RabbitSubscriber( _loggerFactory, config, scopeFactory); subscriber.Start(); var enveloppeWithFirstEvent = GetEnveloppeDataForEvent(publisher: "pub1", content: "data"); _channel.BasicPublish( exchange: Consts.CONST_CQE_EXCHANGE_NAME, routingKey: "", basicProperties: null, body: enveloppeWithFirstEvent); int awaitedTime = 0; while (awaitedTime <= 2000) { if (eventReceived) { break; } await Task.Delay(10); awaitedTime += 10; } eventReceived.Should().BeTrue(); } finally { DeleteData(); } }
public async Task DispatchAsync_Default_Routing_Key_Factory_Should_Be_Considered() { try { var fakeRoutingKeyFactory = new Mock <IRoutingKeyFactory>(); fakeRoutingKeyFactory .Setup(m => m.GetRoutingKeyForCommand(It.IsAny <object>())) .Returns("MyCustomQueue"); var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom); networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("CQELight")); networkInfos.ServiceQueueDescriptions.Add(new RabbitQueueDescription("MyCustomQueue")); var config = new RabbitPublisherConfiguration { ConnectionInfos = GetConnectionInfos(), NetworkInfos = networkInfos, RoutingKeyFactory = fakeRoutingKeyFactory.Object }; var publisher = new RabbitPublisher( loggerFactory, config); await publisher.DispatchAsync(new TestCommand()); channel.BasicGet("CQELight", true).Should().BeNull(); var result = channel.BasicGet("MyCustomQueue", true); result.Should().NotBeNull(); Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestCommand>().Should().NotBeNull(); } finally { DeleteData(); channel.QueueDelete("MyCustomQueue"); } }
static void Main(string[] args) { Console.WriteLine("Trying to connect to RabbitMQ instance @locahost with guest:guest"); var loggerFactory = new LoggerFactory(); var network = RabbitNetworkInfos.GetConfigurationFor("server", RabbitMQExchangeStrategy.SingleExchange); //This network will produce a client_queue queue, bound to cqelight_global_exchange new Bootstrapper() .UseRabbitMQ( ConnectionInfosHelper.GetConnectionInfos("server"), network, cfg => cfg.DispatchInMemory = true ) .UseInMemoryEventBus() .UseAutofacAsIoC(c => c.Register(_ => loggerFactory).AsImplementedInterfaces().ExternallyOwned()) .Bootstrapp(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfuly connected to RabbitMQ"); Console.ResetColor(); Console.WriteLine("Listening... Press any key to exit"); Console.ReadLine(); }