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(); } }
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(); } }
/// <summary> /// Use RabbitMQ as Pub/Sub system. /// </summary> /// <param name="bootstrapper">Bootstrapper instance to configure</param> /// <returns>Configured bootstrapper instance</returns> public static Bootstrapper UseRabbitMQ( this Bootstrapper bootstrapper, RabbitConnectionInfos connectionInfos, RabbitNetworkInfos networkInfos, Action <RabbitSubscriberConfiguration> subscriberConfiguration = null, Action <RabbitPublisherConfiguration> publisherConfiguration = null) { var service = RabbitMQBootstrappService.Instance; var subscriberConf = new RabbitSubscriberConfiguration { ConnectionInfos = connectionInfos, NetworkInfos = networkInfos }; subscriberConfiguration?.Invoke(subscriberConf); service.BootstrappAction += (ctx) => { var publisherConf = new RabbitPublisherConfiguration() { ConnectionInfos = connectionInfos, NetworkInfos = networkInfos }; publisherConfiguration?.Invoke(publisherConf); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(subscriberConf, typeof(RabbitSubscriberConfiguration))); bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(publisherConf, typeof(RabbitPublisherConfiguration))); bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(RabbitPublisher), true)); bootstrapper.AddIoCRegistration(new TypeRegistration(typeof(RabbitSubscriber), true)); if (publisherConf.RoutingKeyFactory != null) { bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(publisherConf.RoutingKeyFactory, typeof(IRoutingKeyFactory))); } }; bootstrapper.AddService(service); bootstrapper.OnPostBootstrapping += (c) => { ILoggerFactory loggerFactory = null; IScopeFactory scopeFactory = null; if (c.Scope != null) { loggerFactory = c.Scope.Resolve <ILoggerFactory>(); scopeFactory = c.Scope.Resolve <IScopeFactory>(); } if (loggerFactory == null) { loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new DebugLoggerProvider()); } RabbitMQBootstrappService.RabbitSubscriber = new RabbitSubscriber( loggerFactory, subscriberConf, scopeFactory); RabbitMQBootstrappService.RabbitSubscriber.Start(); }; return(bootstrapper); }
public static void DeclareExchangesAndQueueForPublisher( IModel channel, RabbitPublisherConfiguration config) { DeclareExchanges(channel, config.NetworkInfos.ServiceExchangeDescriptions.Concat(config.NetworkInfos.DistantExchangeDescriptions)); config.NetworkInfos.ServiceQueueDescriptions.DoForEach(q => DeclareQueue(channel, q)); }
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 static void PublishMessageTest() { IRabbitPublisherConfiguration publishConfig = new RabbitPublisherConfiguration(); RabbitPublisher <CommandArgs> publisher = new RabbitPublisher <CommandArgs>(publishConfig); IRabbitSubscriberConfiguration subscriberConfig = new RabbitSubscriberConfiguration(); SandboxConsumer consumer = new SandboxConsumer(); RabbitSubscriber <SandboxConsumer> subscriber = new RabbitSubscriber <SandboxConsumer>(consumer, subscriberConfig); subscriber.Start(); publisher.Publish(new CommandArgs { Value = "some data" }); Console.ReadKey(); }
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"); } }
public ModelResultPublisher(RabbitPublisherConfiguration configuration) : base(configuration) { }
public ModelPublisher(RabbitPublisherConfiguration configuration, ILoggerFactory logger = null, bool overrideLogs = false, bool hideLogs = false) : base(configuration, logger?.CreateLogger(typeof(ModelPublisher))) { _overrideLogs = overrideLogs; _hideLogs = hideLogs; }