public void TestSimpleBatchDeflaterWithEncoding()
        {
            var batchingStrategy = new SimpleBatchingStrategy(2, int.MaxValue, 30000);
            var template         = new BatchingRabbitTemplate(batchingStrategy)
            {
                ConnectionFactory = connectionFactory
            };
            var deflatorPostProcessor = new DeflaterPostProcessor
            {
                Level = CompressionLevel.Fastest
            };

            template.SetBeforePublishPostProcessors(deflatorPostProcessor);
            var accessor = new RabbitHeaderAccessor(new MessageHeaders())
            {
                ContentEncoding = "foo"
            };
            var props   = accessor.ToMessageHeaders();
            var message = Message.Create(Encoding.UTF8.GetBytes("foo"), props);

            template.Send(string.Empty, ROUTE, message);
            message = Message.Create(Encoding.UTF8.GetBytes("bar"), props);
            template.Send(string.Empty, ROUTE, message);
            var result = Receive(template);

            Assert.Equal("deflate:foo", result.Headers.ContentEncoding());
            var unzipper = new InflaterPostProcessor();
            var unzip    = unzipper.PostProcessMessage(result);

            Assert.Equal("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", Encoding.UTF8.GetString((byte[])unzip.Payload));
        }
        public void TestSimpleBatchGZippedConfiguredUnzipperUsingAdd()
        {
            var batchingStrategy = new SimpleBatchingStrategy(2, int.MaxValue, 30000);
            var template         = new BatchingRabbitTemplate(batchingStrategy)
            {
                ConnectionFactory = connectionFactory
            };
            var gZipPostProcessor = new GZipPostProcessor
            {
                Level = CompressionLevel.Optimal
            };

            Assert.Equal(CompressionLevel.Optimal, gZipPostProcessor.Level);
            template.AddBeforePublishPostProcessors(gZipPostProcessor);
            template.AddAfterReceivePostProcessors(new GUnzipPostProcessor());
            var props   = new MessageHeaders();
            var message = Message.Create(Encoding.UTF8.GetBytes("foo"), props);

            template.Send(string.Empty, ROUTE, message);
            message = Message.Create(Encoding.UTF8.GetBytes("bar"), props);
            template.Send(string.Empty, ROUTE, message);
            var result = Receive(template);

            Assert.Null(result.Headers.ContentEncoding());
            Assert.Equal("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", Encoding.UTF8.GetString((byte[])result.Payload));
        }
        public void TestSimpleBatchGZippedWithEncodingInflated()
        {
            var batchingStrategy = new SimpleBatchingStrategy(2, int.MaxValue, 30000);
            var template         = new BatchingRabbitTemplate(batchingStrategy)
            {
                ConnectionFactory = connectionFactory
            };
            var gZipPostProcessor = new GZipPostProcessor();

            template.SetBeforePublishPostProcessors(gZipPostProcessor);
            template.SetAfterReceivePostProcessors(new DelegatingDecompressingPostProcessor());
            var accessor = new RabbitHeaderAccessor(new MessageHeaders())
            {
                ContentEncoding = "foo"
            };
            var props   = accessor.ToMessageHeaders();
            var message = Message.Create(Encoding.UTF8.GetBytes("foo"), props);

            template.Send(string.Empty, ROUTE, message);
            message = Message.Create(Encoding.UTF8.GetBytes("bar"), props);
            template.Send(string.Empty, ROUTE, message);
            Thread.Sleep(100);
            var output = template.ReceiveAndConvert <byte[]>(ROUTE);

            Assert.NotNull(output);
            Assert.Equal("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", Encoding.UTF8.GetString(output));
        }
        public void TestSimpleBatchGZippedUsingAddAndRemove()
        {
            var batchingStrategy = new SimpleBatchingStrategy(2, int.MaxValue, 30000);
            var template         = new BatchingRabbitTemplate(batchingStrategy)
            {
                ConnectionFactory = connectionFactory
            };
            var gZipPostProcessor = new GZipPostProcessor();

            Assert.Equal(CompressionLevel.Fastest, gZipPostProcessor.Level);
            template.AddBeforePublishPostProcessors(gZipPostProcessor);
            var headerPostProcessor = new HeaderPostProcessor();

            template.AddBeforePublishPostProcessors(headerPostProcessor);
            template.RemoveBeforePublishPostProcessor(headerPostProcessor);
            var props   = new MessageHeaders();
            var message = Message.Create(Encoding.UTF8.GetBytes("foo"), props);

            template.Send(string.Empty, ROUTE, message);
            message = Message.Create(Encoding.UTF8.GetBytes("bar"), props);
            template.Send(string.Empty, ROUTE, message);
            var result = Receive(template);

            Assert.Equal("gzip", result.Headers.ContentEncoding());
            var unzipper = new GUnzipPostProcessor();
            var unzip    = unzipper.PostProcessMessage(result);

            Assert.Equal("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", Encoding.UTF8.GetString((byte[])unzip.Payload));
            Assert.Null(unzip.Headers.Get <string>("someHeader"));
        }
예제 #5
0
 protected AbstractMessageListenerContainer(IApplicationContext applicationContext, IConnectionFactory connectionFactory, string name = null, ILogger logger = null)
     : base(connectionFactory)
 {
     ApplicationContext               = applicationContext;
     _logger                          = logger;
     ErrorHandler                     = new ConditionalRejectingErrorHandler(_logger);
     MessagePropertiesConverter       = new DefaultMessagePropertiesConverter(_logger);
     ExclusiveConsumerExceptionLogger = new DefaultExclusiveConsumerLogger();
     BatchingStrategy                 = new SimpleBatchingStrategy(0, 0, 0L);
     TransactionAttribute             = new DefaultTransactionAttribute();
     Name = name ?? GetType().Name + "@" + GetHashCode();
 }
 protected AbstractMessageListenerContainer(IApplicationContext applicationContext, IConnectionFactory connectionFactory, string name = null, ILoggerFactory loggerFactory = null)
 {
     _loggerFactory                   = loggerFactory;
     _logger                          = _loggerFactory?.CreateLogger(GetType());
     ApplicationContext               = applicationContext;
     ConnectionFactory                = connectionFactory;
     ErrorHandler                     = new ConditionalRejectingErrorHandler(_logger);
     MessageHeadersConverter          = new DefaultMessageHeadersConverter(_logger);
     ExclusiveConsumerExceptionLogger = new DefaultExclusiveConsumerLogger();
     BatchingStrategy                 = new SimpleBatchingStrategy(0, 0, 0L);
     TransactionAttribute             = new DefaultTransactionAttribute();
     ServiceName                      = name ?? GetType().Name + "@" + GetHashCode();
 }
        public void TestSimpleBatchTimeout()
        {
            var batchingStrategy = new SimpleBatchingStrategy(2, int.MaxValue, 50);
            var template         = new BatchingRabbitTemplate(batchingStrategy)
            {
                ConnectionFactory = connectionFactory
            };
            var message = Message.Create(Encoding.UTF8.GetBytes("foo"));

            template.Send(string.Empty, ROUTE, message);
            var recv = Receive(template);

            Assert.Equal("foo", Encoding.UTF8.GetString((byte[])recv.Payload));
        }
        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 TestSimpleBatchTwoEqualBufferLimit()
        {
            var batchingStrategy = new SimpleBatchingStrategy(10, 14, 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);
            var recv = Receive(template);

            Assert.Equal("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", Encoding.UTF8.GetString((byte[])recv.Payload));
        }
        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 TestSimpleBatchBiggerThanBufferLimitMultiple()
        {
            var batchingStrategy = new SimpleBatchingStrategy(2, 6, 30000);
            var template         = new BatchingRabbitTemplate(batchingStrategy)
            {
                ConnectionFactory = connectionFactory
            };
            var message = Message.Create(Encoding.UTF8.GetBytes("f"));

            template.Send(string.Empty, ROUTE, message);
            message = Message.Create(Encoding.UTF8.GetBytes("bar"));
            template.Send(string.Empty, ROUTE, message);
            var recv = Receive(template);

            Assert.Equal("f", Encoding.UTF8.GetString((byte[])recv.Payload));
            recv = Receive(template);
            Assert.Equal("bar", Encoding.UTF8.GetString((byte[])recv.Payload));
        }