/// <summary>Does the test.</summary> /// <param name="concurrentConsumers">The concurrent consumers.</param> /// <param name="configurer">The configurer.</param> private void DoTest(int concurrentConsumers, IContainerConfigurer configurer) { var messageCount = 10; var template = new RabbitTemplate(); var connectionFactory = new CachingConnectionFactory(); connectionFactory.ChannelCacheSize = concurrentConsumers; connectionFactory.Port = BrokerTestUtils.GetPort(); template.ConnectionFactory = connectionFactory; var messageConverter = new SimpleMessageConverter(); messageConverter.CreateMessageIds = true; template.MessageConverter = messageConverter; for (var i = 0; i < messageCount; i++) { template.ConvertAndSend(queue1.Name, i); template.ConvertAndSend(queue2.Name, i); } var container = new SimpleMessageListenerContainer(connectionFactory); var latch = new CountdownEvent(messageCount * 2); var listener = new MultiplePocoListener(latch); container.MessageListener = new MessageListenerAdapter(listener); container.AcknowledgeMode = AcknowledgeModeUtils.AcknowledgeMode.Auto; container.ChannelTransacted = true; container.ConcurrentConsumers = concurrentConsumers; configurer.Configure(container); container.AfterPropertiesSet(); container.Start(); try { var timeout = Math.Min((1 + messageCount) / concurrentConsumers, 50); Logger.Info("Timeout: " + timeout); var waited = latch.Wait(timeout * 1000); Logger.Info("All messages recovered: " + waited); Assert.AreEqual(concurrentConsumers, container.ActiveConsumerCount); Assert.True(waited, "Timed out waiting for messages"); } catch (ThreadInterruptedException e) { Thread.CurrentThread.Interrupt(); throw new ThreadStateException("unexpected interruption"); } finally { container.Shutdown(); Assert.AreEqual(0, container.ActiveConsumerCount); } Assert.Null(template.ReceiveAndConvert(queue1.Name)); Assert.Null(template.ReceiveAndConvert(queue2.Name)); }
/// <summary>Does the test.</summary> /// <param name="messageCount">The message count.</param> /// <param name="errorHandler">The error handler.</param> /// <param name="latch">The latch.</param> /// <param name="listener">The listener.</param> public void DoTest(int messageCount, IErrorHandler errorHandler, CountdownEvent latch, object listener) { var concurrentConsumers = 1; var template = this.CreateTemplate(concurrentConsumers); // Send messages to the queue for (var i = 0; i < messageCount; i++) { template.ConvertAndSend(queue.Name, i + "foo"); } var container = new SimpleMessageListenerContainer(template.ConnectionFactory); container.MessageListener = listener; container.AcknowledgeMode = AcknowledgeModeUtils.AcknowledgeMode.None; container.ChannelTransacted = false; container.ConcurrentConsumers = concurrentConsumers; container.PrefetchCount = messageCount; container.TxSize = messageCount; container.QueueNames = new[] { queue.Name }; container.ErrorHandler = errorHandler; container.AfterPropertiesSet(); container.Start(); var waited = latch.Wait(1000); if (messageCount > 1) { Assert.True(waited, "Expected to receive all messages before stop"); } try { Assert.Null(template.ReceiveAndConvert(queue.Name)); } finally { container.Shutdown(); } }
public void SendAndAsyncReceive() { SimpleMessageListenerContainer container = (SimpleMessageListenerContainer)applicationContext["SimpleMessageListenerContainer"]; SimpleMessageListener listener = applicationContext["SimpleMessageListener"] as SimpleMessageListener; Assert.IsNotNull(container); Assert.IsNotNull(listener); Admin admin = new Admin("tcp://localhost:7222", "admin", null); admin.PurgeQueue("test.queue"); EmsTemplate emsTemplate = (EmsTemplate)applicationContext["MessageTemplate"] as EmsTemplate; Assert.IsNotNull(emsTemplate); Assert.AreEqual(0, listener.MessageCount); emsTemplate.ConvertAndSend("Hello World 1"); int waitInMillis = 2000; Thread.Sleep(waitInMillis); Assert.AreEqual(1, listener.MessageCount); container.Stop(); Console.WriteLine("container stopped."); emsTemplate.ConvertAndSend("Hello World 2"); Thread.Sleep(waitInMillis); Assert.AreEqual(1, listener.MessageCount); container.Start(); Console.WriteLine("container started."); Thread.Sleep(waitInMillis); Assert.AreEqual(2, listener.MessageCount); container.Shutdown(); Thread.Sleep(waitInMillis); }
public void SendAndAsyncReceive() { SimpleMessageListenerContainer container = (SimpleMessageListenerContainer)applicationContext["SimpleMessageListenerContainer"]; SimpleMessageListener listener = applicationContext["SimpleMessageListener"] as SimpleMessageListener; Assert.IsNotNull(container); Assert.IsNotNull(listener); NmsTemplate nmsTemplate = (NmsTemplate)applicationContext["MessageTemplate"] as NmsTemplate; Assert.IsNotNull(nmsTemplate); Assert.AreEqual(0, listener.MessageCount); nmsTemplate.ConvertAndSend("Hello World 1"); int waitInMillis = 2000; Thread.Sleep(waitInMillis); Assert.AreEqual(1, listener.MessageCount); container.Stop(); Console.WriteLine("container stopped."); nmsTemplate.ConvertAndSend("Hello World 2"); Thread.Sleep(waitInMillis); Assert.AreEqual(1, listener.MessageCount); container.Start(); Console.WriteLine("container started."); Thread.Sleep(waitInMillis); Assert.AreEqual(2, listener.MessageCount); container.Shutdown(); Thread.Sleep(waitInMillis); }
/// <summary>Does the test.</summary> /// <param name="level">The level.</param> /// <param name="concurrency">The concurrency.</param> /// <param name="transactionMode">The transaction mode.</param> private void DoTest(MessageCount level, Concurrency concurrency, TransactionModeUtils.TransactionMode transactionMode) { var messageCount = (int)level; var concurrentConsumers = (int)concurrency; var transactional = transactionMode.IsTransactional(); var template = this.CreateTemplate(concurrentConsumers); var latch = new CountdownEvent(messageCount); for (var i = 0; i < messageCount; i++) { template.ConvertAndSend(queue.Name, i + "foo"); } var container = new SimpleMessageListenerContainer(template.ConnectionFactory); var listener = new LifecyclePocoListener(latch); container.MessageListener = new MessageListenerAdapter(listener); container.AcknowledgeMode = transactionMode.AcknowledgeMode(); container.ChannelTransacted = transactionMode.IsTransactional(); container.ConcurrentConsumers = concurrentConsumers; if (transactionMode.Prefetch() > 0) { container.PrefetchCount = transactionMode.Prefetch(); container.TxSize = transactionMode.TxSize(); } container.QueueNames = new[] { queue.Name }; container.AfterPropertiesSet(); container.Start(); try { var waited = latch.Wait(50); Logger.Info("All messages received before stop: " + waited); if (messageCount > 1) { Assert.False(waited, "Expected not to receive all messages before stop"); } Assert.AreEqual(concurrentConsumers, container.ActiveConsumerCount); container.Stop(); Thread.Sleep(500); Assert.AreEqual(0, container.ActiveConsumerCount); if (!transactional) { var messagesReceivedAfterStop = listener.Count; waited = latch.Wait(500); Logger.Info("All messages received after stop: " + waited); if (messageCount < 100) { Assert.True(waited, "Expected to receive all messages after stop"); } Assert.AreEqual(messagesReceivedAfterStop, listener.Count, "Unexpected additional messages received after stop"); for (var i = 0; i < messageCount; i++) { template.ConvertAndSend(queue.Name, i + "bar"); } latch = new CountdownEvent(messageCount); listener.Reset(latch); } var messagesReceivedBeforeStart = listener.Count; container.Start(); var timeout = Math.Min(1 + messageCount / (4 * concurrentConsumers), 30); Logger.Debug("Waiting for messages with timeout = " + timeout + " (s)"); waited = latch.Wait(timeout * 1000); Logger.Info("All messages received after start: " + waited); Assert.AreEqual(concurrentConsumers, container.ActiveConsumerCount); if (transactional) { Assert.True(waited, "Timed out waiting for message"); } else { var count = listener.Count; Assert.True(messagesReceivedBeforeStart < count, "Expected additional messages received after start: " + messagesReceivedBeforeStart + ">=" + count); Assert.Null(template.Receive(queue.Name), "Messages still available"); } Assert.AreEqual(concurrentConsumers, container.ActiveConsumerCount); } finally { // Wait for broker communication to finish before trying to stop // container Thread.Sleep(500); container.Shutdown(); Assert.AreEqual(0, container.ActiveConsumerCount); } Assert.Null(template.ReceiveAndConvert(queue.Name)); }
private void DoTestRetry(int messageCount, int txSize, int failFrequency, int concurrentConsumers, bool stateful) { var failedMessageCount = messageCount / failFrequency + (messageCount % failFrequency == 0 ? 0 : 1); this.template = this.CreateTemplate(concurrentConsumers); for (var i = 0; i < messageCount; i++) { this.template.ConvertAndSend(queue.Name, i.ToString()); } var container = new SimpleMessageListenerContainer(this.template.ConnectionFactory); var listener = new RetryPocoListener(failFrequency); container.MessageListener = new MessageListenerAdapter(listener); container.AcknowledgeMode = AcknowledgeModeUtils.AcknowledgeMode.Auto; container.ChannelTransacted = true; container.TxSize = txSize; container.ConcurrentConsumers = concurrentConsumers; var latch = new CountdownEvent(failedMessageCount); // container.AdviceChain = new IAdvice[] { CreateRetryInterceptor(latch, stateful) }; container.QueueNames = new[] { queue.Name }; container.AfterPropertiesSet(); container.Start(); try { var timeout = Math.Min(1 + messageCount / concurrentConsumers, 30); var count = messageCount; Logger.Debug("Waiting for messages with timeout = " + timeout + " (s)"); Task.Factory.StartNew( () => { while (container.ActiveConsumerCount > 0) { try { Thread.Sleep(100); } catch (ThreadInterruptedException e) { if (latch.CurrentCount > 0) { latch.Signal(); } Thread.CurrentThread.Interrupt(); return; } } for (var i = 0; i < count; i++) { if (latch.CurrentCount > 0) { latch.Signal(); } } }); var waited = latch.Wait(timeout * 1000); Logger.Info("All messages recovered: " + waited); Assert.AreEqual(concurrentConsumers, container.ActiveConsumerCount); Assert.True(waited, "Timed out waiting for messages"); // Retried each failure 3 times (default retry policy)... Assert.AreEqual(3 * failedMessageCount, listener.Count); // All failed messages recovered Assert.AreEqual(null, this.template.ReceiveAndConvert(queue.Name)); } finally { container.Shutdown(); Assert.AreEqual(0, container.ActiveConsumerCount); } }