internal void Configure(RabbitMqTransport transport) { transport.AddClientProperties(_additionalClientProperties); if (DeclareExchanges.HasValue) { transport.SetDeclareExchanges(DeclareExchanges.Value); } if (DeclareInputQueue.HasValue) { transport.SetDeclareInputQueue(DeclareInputQueue.Value); } if (BindInputQueue.HasValue) { transport.SetBindInputQueue(BindInputQueue.Value); } if (DirectExchangeName != null) { transport.SetDirectExchangeName(DirectExchangeName); } if (TopicExchangeName != null) { transport.SetTopicExchangeName(TopicExchangeName); } if (MaxNumberOfMessagesToPrefetch != null) { transport.SetMaxMessagesToPrefetch(MaxNumberOfMessagesToPrefetch.Value); } }
public void SendFailureTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var delivered = new ManualResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(() => Console.WriteLine("onFailure called")); /*FieldInfo field = typeof(RabbitMqSession).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance); * var connection = field.GetValue(messagingSession) as IConnection; * connection.Abort(1, "All your base are belong to us");*/ while (true) { try { messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name }, 0); } catch (Exception e) { Console.WriteLine(e.Message + "!!!!!!!!!"); throw; } Thread.Sleep(1000); Console.WriteLine('.'); } } }
protected override void SetUp() { using (var transport = new RabbitMqTransport(ConnectionString, _receiverQueueName, new NullLoggerFactory())) { transport.PurgeInputQueue(); } _receiver = new BuiltinHandlerActivator(); Using(_receiver); Configure.With(_receiver) .Logging(l => l.Console(LogLevel.Info)) .Transport(t => t.UseRabbitMq(ConnectionString, _receiverQueueName).Prefetch(1)) .Options(o => { o.SetNumberOfWorkers(1); o.SetMaxParallelism(1); }) .Start(); _sender = Configure.With(new BuiltinHandlerActivator()) .Logging(l => l.Console(LogLevel.Info)) .Transport(t => t.UseRabbitMqAsOneWayClient(ConnectionString)) .Routing(r => r.TypeBased().MapFallback(_receiverQueueName)) .Start(); Using(_sender); }
static void PurgeInputQueue(string inputQueueName) { using (var transport = new RabbitMqTransport(RabbitMqConnectionString, inputQueueName, new NullLoggerFactory())) { transport.PurgeInputQueue(); } }
public void MessageOfUnknownTypeShouldPauseProcessingTillCorrespondingHandlerIsRegisteredTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); var type1Received = new AutoResetEvent(false); var type2Received = new AutoResetEvent(false); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { type1Received.Set(); acknowledge(true); }, "type1"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(type1Received.WaitOne(500), Is.True, "Message of subscribed type was not delivered"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2" }, 0); //Give time for type2 message to be pushed back by mq //Thread.Sleep(500); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(type1Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing"); Assert.That(type2Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing"); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { type2Received.Set(); acknowledge(true); }, "type2"); Assert.That(type1Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered"); Assert.That(type2Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered"); } }
public void UnknownMessageTypeHandlerWaitingDoesNotPreventTransportDisposeTest() { var received = new ManualResetEvent(false); Thread connectionThread = null; using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { connectionThread = Thread.CurrentThread; received.Set(); }, "type1"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(received.WaitOne(100), Is.True, "Message was not delivered"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2" }, 0); } GC.Collect(); Thread.Sleep(30000); Assert.That(connectionThread.ThreadState, Is.EqualTo(ThreadState.Stopped), "Processing thread is still active in spite of transport dispose"); }
public void SessionIsTreatedAsBrokenAfterSendFailureWithAlreadyClosedExceptionTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var onFailureCalled = new AutoResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(() => { onFailureCalled.Set(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); }); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType" }, 0); FieldInfo field = typeof(RabbitMqSession).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance); var connection = field.GetValue(messagingSession) as IConnection; connection.Abort(1, "All your base are belong to us"); AlreadyClosedException ex = null; try { messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType" }, 0); } catch (AlreadyClosedException e) { ex = e; } Assert.That(ex, Is.Not.Null, "Exception was not thrown on send fail"); Assert.That(ex, Is.InstanceOf <AlreadyClosedException>(), "Wrong exception type was thrown on send fail"); Assert.That(transport.SessionsCount, Is.EqualTo(0), "session was not removed after send failed AlreadyClosedException "); Assert.That(onFailureCalled.WaitOne(500), Is.True, "Subsciptionwas not notefied on failure"); } }
/// <summary> /// Configures Rebus to use RabbitMQ to move messages around /// </summary> public static RabbitMqOptionsBuilder UseRabbitMq(this StandardConfigurer <ITransport> configurer, string connectionString, string inputQueueName) { if (configurer == null) { throw new ArgumentNullException(nameof(configurer)); } if (connectionString == null) { throw new ArgumentNullException(nameof(connectionString)); } if (inputQueueName == null) { throw new ArgumentNullException(nameof(inputQueueName)); } var options = new RabbitMqOptionsBuilder(); configurer .OtherService <RabbitMqTransport>() .Register(c => { var rebusLoggerFactory = c.Get <IRebusLoggerFactory>(); var transport = new RabbitMqTransport(connectionString, inputQueueName, rebusLoggerFactory); options.Configure(transport); return(transport); }); configurer .OtherService <ISubscriptionStorage>() .Register(c => c.Get <RabbitMqTransport>(), description: RabbitMqSubText); configurer.Register(c => c.Get <RabbitMqTransport>()); return(options); }
public async Task CanUseAlternateCustomExchangeName() { const string connectionString = RabbitMqTransportFactory.ConnectionString; var rabbitMqTransport = new RabbitMqTransport(connectionString, "inputQueue", new ConsoleLoggerFactory(false)); var defaultTopicExchange = "defaultTopicExchange"; rabbitMqTransport.SetTopicExchangeName(defaultTopicExchange); var topic = "myTopic"; var alternateExchange = "alternateExchange"; var topicWithAlternateExchange = $"{topic}@{alternateExchange}"; var subscriberAddresses = await rabbitMqTransport.GetSubscriberAddresses(topicWithAlternateExchange); Assert.That(subscriberAddresses[0], Is.EqualTo(topicWithAlternateExchange)); subscriberAddresses = await rabbitMqTransport.GetSubscriberAddresses(topic); Assert.That(subscriberAddresses[0], Is.EqualTo($"{topic}@{defaultTopicExchange}")); subscriberAddresses = await rabbitMqTransport.GetSubscriberAddresses(topic + '@'); Assert.That(subscriberAddresses[0], Is.EqualTo($"{topic}@")); }
public void PerformanceTest(int messageSize, bool confirmedSending) { var messageBytes = new byte[messageSize]; new Random().NextBytes(messageBytes); using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null, confirmedSending); Stopwatch sw = Stopwatch.StartNew(); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = messageBytes, Type = typeof(byte[]).Name }, 0); int sendCounter; for (sendCounter = 0; sw.ElapsedMilliseconds < 4000; sendCounter++) { messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = messageBytes, Type = typeof(byte[]).Name }, 0); } int receiveCounter = 0; var ev = new ManualResetEvent(false); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => receiveCounter++, typeof(byte[]).Name); ev.WaitOne(2000); Console.WriteLine("Send: {0} per second. {1:0.00} Mbit/s", sendCounter / 4, 1.0 * sendCounter * messageSize / 4 / 1024 / 1024 * 8); Console.WriteLine("Receive: {0} per second. {1:0.00} Mbit/s", receiveCounter / 2, 1.0 * receiveCounter * messageSize / 2 / 1024 / 1024 * 8); } }
public void NackTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var delivered = new ManualResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name }, 0); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { Console.WriteLine("message:" + message.Type); delivered.Set(); acknowledge(false); }, typeof(byte[]).Name); Assert.That(delivered.WaitOne(300), Is.True, "Message was not delivered"); } using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var delivered = new ManualResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => delivered.Set(), typeof(byte[]).Name); Assert.That(delivered.WaitOne(1000), Is.True, "Message was not returned to queue"); } }
public MyTransport(IList <ConnectionEndpoint> endpoints, string inputQueueAddress, IRebusLoggerFactory rebusLoggerFactory, int maxMessagesToPrefetch = 50, Func <IConnectionFactory, IConnectionFactory> customizer = null, string delayExchangeName = "Delay.Exchange") { transport = new RabbitMqTransport(endpoints, inputQueueAddress, rebusLoggerFactory, maxMessagesToPrefetch, customizer); this.delayExchangeName = delayExchangeName; this.endpoints = endpoints; this.customizer = customizer; }
public MyTransport(string connectionString, string inputQueueAddress, IRebusLoggerFactory rebusLoggerFactory, int maxMessagesToPrefetch = 50, Func <IConnectionFactory, IConnectionFactory> customizer = null, string delayExchangeName = "Delay.Exchange") { transport = new RabbitMqTransport(connectionString, inputQueueAddress, rebusLoggerFactory, maxMessagesToPrefetch, customizer); this.delayExchangeName = delayExchangeName; this.connectionString = connectionString; this.customizer = customizer; }
public void AttemptToSubscribeSameDestinationAndMessageTypeTwiceFailureTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "type1"); Assert.That(() => messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "type1"), Throws.TypeOf <InvalidOperationException>()); } }
public string VerifyPublishEndpointFailureTest() { var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest"); var valid = transport.VerifyDestination("non.existing", EndpointUsage.Publish, false, out var error); Assert.That(valid, Is.False, "endpoint reported as valid"); Assert.That(error, Is.EqualTo(@"The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=404, text=""NOT_FOUND - no exchange 'non.existing' in vhost '/'"", classId=40, methodId=10, cause=")); return(error); }
public void EndpointVerificationTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var res = transport.VerifyDestination("unistream.processing.events", EndpointUsage.Publish | EndpointUsage.Subscribe, false, out var error); Console.WriteLine(error); Assert.That(res, Is.False); } }
public void Send() { var t = new RabbitMqTransport(new EndpointAddress(_address), _factory.CreateConnection(_rabbitAddress)); t.Send((s) => { var b = Encoding.UTF8.GetBytes("dru"); s.Write(b, 0, b.Length); }); }
internal void Configure(RabbitMqTransport transport) { transport.AddClientProperties(_additionalClientProperties); if (SslSettings != null) { transport.SetSslSettings(SslSettings); } if (DeclareExchanges.HasValue) { transport.SetDeclareExchanges(DeclareExchanges.Value); } if (DeclareInputQueue.HasValue) { transport.SetDeclareInputQueue(DeclareInputQueue.Value); } if (BindInputQueue.HasValue) { transport.SetBindInputQueue(BindInputQueue.Value); } if (DirectExchangeName != null) { transport.SetDirectExchangeName(DirectExchangeName); } if (TopicExchangeName != null) { transport.SetTopicExchangeName(TopicExchangeName); } if (MaxNumberOfMessagesToPrefetch != null) { transport.SetMaxMessagesToPrefetch(MaxNumberOfMessagesToPrefetch.Value); } if (CallbackOptionsBuilder != null) { transport.SetCallbackOptions(CallbackOptionsBuilder); } if (PublisherConfirmsEnabled.HasValue) { transport.EnablePublisherConfirms(PublisherConfirmsEnabled.Value); } transport.SetInputQueueOptions(InputQueueOptionsBuilder); transport.SetDefaultQueueOptions(DefaultQueueOptionsBuilder); transport.SetExchangeOptions(ExchangeOptions); transport.SetMaxPollingTimeout(MaxPollingTimeout); transport.SetMaxWriterPoolSize(MaxWriterPoolSize); }
public string VerifySubscriptionEndpointNoQueueFailureTest() { var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest"); var valid = transport.VerifyDestination(new Destination { Subscribe = "non.existing", Publish = "amq.direct" }, EndpointUsage.Subscribe, false, out var error); Assert.That(valid, Is.False, "endpoint reported as valid"); Assert.That(error, Is.EqualTo(@"The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=404, text=""NOT_FOUND - no queue 'non.existing' in vhost '/'"", classId=50, methodId=10, cause=")); return(error); }
protected override void DoRun() { using (var transport = new RabbitMqTransport(GetConnectionString(HostnameOrConnectionString), InputQueue, LoggerFactory)) { var returnToSourceQueue = new ReturnToSourceQueue(transport) { InputQueue = InputQueue, DefaultOutputQueue = DefaultOutputQueue }; returnToSourceQueue.Run(); } }
public ITransport Create(string inputQueueAddress) { var transport = new RabbitMqTransport(ConnectionString, inputQueueAddress); _disposables.Add(transport); transport.PurgeInputQueue(); transport.Initialize(); _queuesToDelete.Add(inputQueueAddress); return(transport); }
public ITransport Create(string inputQueueAddress) { var transport = new RabbitMqTransport(ConnectionString, inputQueueAddress); _disposables.Add(transport); transport.PurgeInputQueue(); transport.Initialize(); _queuesToDelete.Add(inputQueueAddress); return transport; }
public void UnsubscribeTest(string messageType) { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var ev = new AutoResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = messageType }, 0); IDisposable subscription = messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => ev.Set(), messageType); Assert.That(ev.WaitOne(500), Is.True, "Message was not delivered"); subscription.Dispose(); Assert.That(ev.WaitOne(500), Is.False, "Message was delivered for canceled subscription"); } }
public void DefaultExchangeVerificationTest() { var defaultExchangeDestination = new Destination() { Subscribe = m_TempQueue, Publish = new PublicationAddress("direct", "", m_TempQueue).ToString() }; using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var res = transport.VerifyDestination(defaultExchangeDestination, EndpointUsage.Publish | EndpointUsage.Subscribe, true, out var error); Console.WriteLine(error); Assert.That(res, Is.True); } }
public void Receive() { var t = new RabbitMqTransport(new EndpointAddress(new Uri("rabbitmq://localhost/dru")), _factory.CreateConnection(_rabbitAddress)); t.Receive(s => { return(ss => { var buff = new byte[3]; ss.Read(buff, 0, buff.Length); var name = Encoding.UTF8.GetString(buff); Assert.AreEqual("dru", name); Console.WriteLine(name); }); }); }
internal void Configure(RabbitMqTransport transport) { transport.AddClientProperties(_additionalClientProperties); if (SslSettings != null) { transport.SetSslSettings(SslSettings); } if (DeclareExchanges.HasValue) { transport.SetDeclareExchanges(DeclareExchanges.Value); } if (DeclareInputQueue.HasValue) { transport.SetDeclareInputQueue(DeclareInputQueue.Value); } if (BindInputQueue.HasValue) { transport.SetBindInputQueue(BindInputQueue.Value); } if (DirectExchangeName != null) { transport.SetDirectExchangeName(DirectExchangeName); } if (TopicExchangeName != null) { transport.SetTopicExchangeName(TopicExchangeName); } if (MaxNumberOfMessagesToPrefetch != null) { transport.SetMaxMessagesToPrefetch(MaxNumberOfMessagesToPrefetch.Value); } if (CallbackOptionsBuilder != null) { transport.SetCallbackOptions(CallbackOptionsBuilder); } transport.SetInputQueueOptions(QueueOptions); }
internal void ConfigureEvents(IModel model) { if (BasicReturnCallback != null) { model.BasicReturn += (sender, args) => { var transportMessage = RabbitMqTransport.CreateTransportMessage(args.BasicProperties, args.Body); var eventArgs = new BasicReturnEventArgs( transportMessage, args.Exchange, args.ReplyCode, args.ReplyText, args.RoutingKey ); BasicReturnCallback(sender, eventArgs); }; } }
public void ConnectionFailureTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var onFailureCalled = new AutoResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(() => { onFailureCalled.Set(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); }); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType" }, 0); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "messageType"); FieldInfo field = typeof(RabbitMqSession).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance); var connection = field.GetValue(messagingSession) as IConnection; connection.Abort(1, "All your base are belong to us"); Assert.That(onFailureCalled.WaitOne(500), Is.True, "Subsciptionwas not notefied on failure"); } }
public void HandlerWaitStopsAndMessageOfUnknownTypeReturnsToQueueOnUnsubscribeTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); var received = new AutoResetEvent(false); IDisposable subscription = messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { received.Set(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); }, "type2"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing"); subscription.Dispose(); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => received.Set(), "type1"); Assert.That(received.WaitOne(500), Is.True, "Message was not returned to queue"); } }
public ITransport Create(string inputQueueAddress) { var transport = new RabbitMqTransport(ConnectionString, inputQueueAddress, new ConsoleLoggerFactory(false)); _disposables.Add(transport); if (inputQueueAddress != null) { transport.PurgeInputQueue(); } transport.Initialize(); if (inputQueueAddress != null) { _queuesToDelete.Add(inputQueueAddress); } return transport; }
public ITransport Create(string inputQueueAddress) { var transport = new RabbitMqTransport(ConnectionString, inputQueueAddress, new ConsoleLoggerFactory(false)); _disposables.Add(transport); if (inputQueueAddress != null) { transport.PurgeInputQueue(); } transport.Initialize(); if (inputQueueAddress != null) { _queuesToDelete.Add(inputQueueAddress); } return(transport); }
public void RpcTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var request = new byte[] { 0x0, 0x1, 0x2 }; var response = new byte[] { 0x2, 0x1, 0x0 }; byte[] actualResponse = null; var received = new ManualResetEvent(false); var session = transport.CreateSession(null); session.RegisterHandler(TEST_QUEUE, message => new BinaryMessage { Bytes = response, Type = typeof(byte[]).Name }, null); session.SendRequest(TEST_EXCHANGE, new BinaryMessage { Bytes = request, Type = typeof(byte[]).Name }, message => { actualResponse = message.Bytes; received.Set(); }); Assert.That(received.WaitOne(500), Is.True, "Response was not received"); Assert.That(actualResponse, Is.EqualTo(response), "Received response does not match sent one"); } }