public void TestDebatchByContainerPerformance() { var provider = new ServiceCollection().BuildServiceProvider(); var config = new ConfigurationBuilder().Build(); var received = new List <IMessage>(); var count = 100000; var latch = new CountdownEvent(count); var context = new GenericApplicationContext(provider, config); context.ServiceExpressionResolver = new StandardServiceExpressionResolver(); var container = new DirectMessageListenerContainer(context, connectionFactory); container.SetQueueNames(ROUTE); var lastInBatch = new List <bool>(); var batchSize = new AtomicInteger(); container.MessageListener = new TestDebatchListener(received, null, null, latch); container.PrefetchCount = 1000; container.BatchingStrategy = new SimpleBatchingStrategy(1000, int.MaxValue, 30000); container.Initialize(); container.Start(); try { var batchingStrategy = new SimpleBatchingStrategy(1000, int.MaxValue, 30000); var template = new BatchingRabbitTemplate(batchingStrategy) { ConnectionFactory = connectionFactory }; var accessor = RabbitHeaderAccessor.GetMutableAccessor(new MessageHeaders()); accessor.DeliveryMode = MessageDeliveryMode.NON_PERSISTENT; var message = Message.Create(new byte[256], accessor.MessageHeaders); var watch = new Stopwatch(); watch.Start(); for (var i = 0; i < count; i++) { template.Send(string.Empty, ROUTE, message); } Assert.True(latch.Wait(TimeSpan.FromSeconds(60))); watch.Stop(); testOutputHelper.WriteLine(watch.ElapsedMilliseconds.ToString()); Assert.Equal(count, received.Count); } finally { container.Stop(); } }
public void TestDebatchByContainer() { var provider = new ServiceCollection().BuildServiceProvider(); var config = new ConfigurationBuilder().Build(); var received = new List <IMessage>(); var latch = new CountdownEvent(2); var context = new GenericApplicationContext(provider, config); context.ServiceExpressionResolver = new StandardServiceExpressionResolver(); var container = new DirectMessageListenerContainer(context, connectionFactory); container.SetQueueNames(ROUTE); var lastInBatch = new List <bool>(); var batchSize = new AtomicInteger(); container.MessageListener = new TestDebatchListener(received, lastInBatch, batchSize, latch); container.Initialize(); container.Start(); try { var batchingStrategy = new SimpleBatchingStrategy(2, int.MaxValue, 30000); var template = new BatchingRabbitTemplate(batchingStrategy) { ConnectionFactory = connectionFactory }; var message = Message.Create(Encoding.UTF8.GetBytes("foo")); template.Send(string.Empty, ROUTE, message); message = Message.Create(Encoding.UTF8.GetBytes("bar")); template.Send(string.Empty, ROUTE, message); Assert.True(latch.Wait(TimeSpan.FromSeconds(10))); Assert.Equal(2, received.Count); Assert.Equal("foo", Encoding.UTF8.GetString((byte[])received[0].Payload)); Assert.Equal(3, received[0].Headers.ContentLength()); Assert.False(lastInBatch[0]); Assert.Equal("bar", Encoding.UTF8.GetString((byte[])received[1].Payload)); Assert.Equal(3, received[1].Headers.ContentLength()); Assert.True(lastInBatch[1]); Assert.Equal(2, batchSize.Value); } finally { container.Stop(); } }
public void TestDebatchByContainerBadMessageRejected() { var provider = new ServiceCollection().BuildServiceProvider(); var config = new ConfigurationBuilder().Build(); var context = new GenericApplicationContext(provider, config); context.ServiceExpressionResolver = new StandardServiceExpressionResolver(); var container = new DirectMessageListenerContainer(context, connectionFactory); container.SetQueueNames(ROUTE); var listener = new EmptyListener(); container.MessageListener = listener; var errorHandler = new TestConditionalRejectingErrorHandler(); container.ErrorHandler = errorHandler; container.Initialize(); container.Start(); try { var template = new RabbitTemplate { ConnectionFactory = connectionFactory }; var headers = new MessageHeaders(new Dictionary <string, object>() { { RabbitMessageHeaders.SPRING_BATCH_FORMAT, RabbitMessageHeaders.BATCH_FORMAT_LENGTH_HEADER4 } }); var message = Message.Create(Encoding.UTF8.GetBytes("\u0000\u0000\u0000\u0004foo"), headers); template.Send(string.Empty, ROUTE, message); Thread.Sleep(1000); Assert.Equal(0, listener.Count); Assert.True(errorHandler.HandleErrorCalled); } finally { container.Stop(); } }
public async Task TestAbstractRoutingConnectionFactoryWithListenerContainer() { var connectionFactory1 = new Mock <IConnectionFactory>(); var connectionFactory2 = new Mock <IConnectionFactory>(); var defaultConnectionFactory = new Mock <IConnectionFactory>(); var connection1 = new Mock <IConnection>(); var connection2 = new Mock <IConnection>(); var defautConnection = new Mock <IConnection>(); var channel1 = new Mock <RC.IModel>(); var channel2 = new Mock <RC.IModel>(); var defaultChannel = new Mock <RC.IModel>(); connectionFactory1.SetupSequence((f) => f.CreateConnection()) .Returns(connection1.Object); connectionFactory2.SetupSequence((f) => f.CreateConnection()) .Returns(connection1.Object) .Returns(connection2.Object); defaultConnectionFactory.SetupSequence((f) => f.CreateConnection()) .Returns(defautConnection.Object); connection1.Setup((c) => c.IsOpen).Returns(true); connection1.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(channel1.Object); connection2.Setup((c) => c.IsOpen).Returns(true); connection2.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(channel2.Object); defautConnection.Setup((c) => c.IsOpen).Returns(true); defautConnection.Setup(c => c.CreateChannel(It.IsAny <bool>())).Returns(defaultChannel.Object); channel1.Setup(c => c.IsOpen).Returns(true); channel2.Setup(c => c.IsOpen).Returns(true); defaultChannel.Setup(c => c.IsOpen).Returns(true); var factories = new Dictionary <object, IConnectionFactory> { { "[baz]", connectionFactory1.Object }, { "[foo,bar]", connectionFactory2.Object } }; var connectionFactory = new SimpleRoutingConnectionFactory { LenientFallback = true, DefaultTargetConnectionFactory = defaultConnectionFactory.Object }; connectionFactory.SetTargetConnectionFactories(factories); var container = new DirectMessageListenerContainer(null, connectionFactory); container.SetQueueNames("foo", "bar"); container.Initialize(); await container.Start(); Assert.True(container._startedLatch.Wait(TimeSpan.FromSeconds(10))); connectionFactory1.Verify((f) => f.CreateConnection(), Times.Never); connectionFactory2.Verify((f) => f.CreateConnection(), Times.Exactly(2)); defaultConnectionFactory.Verify((f) => f.CreateConnection(), Times.Once); // Checks connection connectionFactory1.Invocations.Clear(); connectionFactory2.Invocations.Clear(); defaultConnectionFactory.Invocations.Clear(); container.SetQueueNames("baz"); connectionFactory1.Verify((f) => f.CreateConnection()); connectionFactory2.Verify((f) => f.CreateConnection(), Times.Never); defaultConnectionFactory.Verify((f) => f.CreateConnection(), Times.Never); connectionFactory1.Invocations.Clear(); connectionFactory2.Invocations.Clear(); defaultConnectionFactory.Invocations.Clear(); container.SetQueueNames("qux"); connectionFactory1.Verify((f) => f.CreateConnection(), Times.Never); connectionFactory2.Verify((f) => f.CreateConnection(), Times.Never); defaultConnectionFactory.Verify((f) => f.CreateConnection()); await container.Stop(); }