public void OptionalMessageId()
 {
     var converter = new SimpleMessageConverter();
     converter.CreateMessageIds = true;
     var message = converter.ToMessage("foo", null);
     Assert.IsNotNull(message.MessageProperties.MessageId);
 }
 public void BytesAsDefaultMessageBodyType()
 {
     var converter = new SimpleMessageConverter();
     var message = new Message("test".ToByteArrayWithEncoding("UTF-8"), new MessageProperties());
     var result = converter.FromMessage(message);
     Assert.AreEqual(typeof(byte[]), result.GetType());
     Assert.AreEqual("test", ((byte[])result).ToStringWithEncoding("UTF-8"));
 }
 public void BytesAsDefaultMessageBodyType()
 {
     SimpleMessageConverter converter = new SimpleMessageConverter();
     //template.CreateMessageProperties contains the default of using application/octet-stream as content-type
     Message message = new Message(Encoding.UTF8.GetBytes("test"), template.CreateMessageProperties());
     object result = converter.FromMessage(message);
     Assert.AreEqual(typeof (byte[]), result.GetType());
     Assert.AreEqual("test", ConvertToString((byte[]) result, SimpleMessageConverter.DEFAULT_CHARSET));
 }
 public void MessageToString()
 {
     var converter = new SimpleMessageConverter();
     var message = new Message("test".ToByteArrayWithEncoding("UTF-8"), new MessageProperties());
     message.MessageProperties.ContentType = MessageProperties.CONTENT_TYPE_TEXT_PLAIN;
     var result = converter.FromMessage(message);
     Assert.AreEqual(typeof(string), result.GetType());
     Assert.AreEqual("test", result);
 }
 public void MessageToBytes()
 {
     var converter = new SimpleMessageConverter();
     var message = new Message(new byte[] { 1, 2, 3 }, new MessageProperties());
     message.MessageProperties.ContentType = MessageProperties.CONTENT_TYPE_BYTES;
     var result = converter.FromMessage(message);
     Assert.AreEqual(typeof(byte[]), result.GetType());
     var resultBytes = (byte[])result;
     Assert.AreEqual(3, resultBytes.Length);
     Assert.AreEqual(1, resultBytes[0]);
     Assert.AreEqual(2, resultBytes[1]);
     Assert.AreEqual(3, resultBytes[2]);
 }
        public void BytesToMessage()
        {
            SimpleMessageConverter converter = new SimpleMessageConverter();
            Message message =
                template.Execute(delegate(IModel channel) { return converter.ToMessage(new byte[] { 1, 2, 3 }, new RabbitMessagePropertiesFactory(channel)); });
            string contentType = message.MessageProperties.ContentType;
            byte[] body = message.Body;

            Assert.AreEqual(MessageProperties.CONTENT_TYPE_BYTES, contentType);
            Assert.AreEqual(3, body.Length);
            Assert.AreEqual(1, body[0]);
            Assert.AreEqual(2, body[1]);
            Assert.AreEqual(3, body[2]);
        }
 public void testStatefulRetryWithNoMessageIds()
 {
     var messageCount = 2;
     var txSize = 1;
     var failFrequency = 1;
     var concurrentConsumers = 1;
     var messageConverter = new SimpleMessageConverter();
     // There will be no key for these messages so they cannot be recovered...
     messageConverter.CreateMessageIds = false;
     this.messageConverter = messageConverter;
     // Beware of context cache busting if retry policy fails...
     /* TODO: Once Spring Batch is implemented.
      * this.retryTemplate = new RetryTemplate();
     this.retryTemplate.setRetryContextCache(new MapRetryContextCache(1));
      */
     // The container should have shutdown, so there are now no active consumers
     //exception.expectMessage("expected:<1> but was:<0>");
     DoTestStatefulRetry(messageCount, txSize, failFrequency, concurrentConsumers);
 }
 public void MessageToString()
 {
     SimpleMessageConverter converter = new SimpleMessageConverter();
     Message message = new Message(Encoding.UTF8.GetBytes("test"), template.CreateMessageProperties());
     message.MessageProperties.ContentType = MessageProperties.CONTENT_TYPE_TEXT_PLAIN;
     object result = converter.FromMessage(message);
     Assert.AreEqual(typeof (string), result.GetType());
     Assert.AreEqual("test", result);
 }
 public void StringToMessage()
 {
     SimpleMessageConverter converter = new SimpleMessageConverter();
     Message message = template.Execute(delegate(IModel channel) { return converter.ToMessage("test", new RabbitMessagePropertiesFactory(channel)); });
     Assert.AreEqual("text/plain", message.MessageProperties.ContentType);
     Assert.AreEqual("test", ConvertToString(message.Body, SimpleMessageConverter.DEFAULT_CHARSET));
 }
        /// <summary>
        /// Does the test.
        /// </summary>
        /// <param name="concurrentConsumers">The concurrent consumers.</param>
        /// <param name="configurer">The configurer.</param>
        /// <remarks></remarks>
        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.ToString());
                template.ConvertAndSend(queue2.Name, i.ToString());
            }

            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.IsChannelTransacted = true;
            container.ConcurrentConsumers = concurrentConsumers;
            configurer.Configure(container);
            container.AfterPropertiesSet();
            container.Start();
            try
            {
                var timeout = Math.Min((1 + messageCount) / concurrentConsumers, 30);
                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));
        }
 public void MessageToSerializedObject()
 {
     var converter = new SimpleMessageConverter();
     var properties = new MessageProperties();
     properties.ContentType = MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT;
     var binaryFormatter = new BinaryFormatter();
     var byteStream = new MemoryStream();
     var testObject = new TestObject("foo");
     binaryFormatter.Serialize(byteStream, testObject);
     var bytes = byteStream.ToArray();
     var message = new Message(bytes, properties);
     var result = converter.FromMessage(message);
     Assert.AreEqual(typeof(TestObject), result.GetType());
     Assert.AreEqual(testObject, result);
 }
 public void NoMessageIdByDefault()
 {
     var converter = new SimpleMessageConverter();
     var message = converter.ToMessage("foo", null);
     Assert.IsNull(message.MessageProperties.MessageId);
 }
 public void SerializedObjectToMessage()
 {
     var converter = new SimpleMessageConverter();
     var testObject = new TestObject("foo");
     var message = converter.ToMessage(testObject, new MessageProperties());
     var contentType = message.MessageProperties.ContentType;
     var body = message.Body;
     Assert.AreEqual("application/x-java-serialized-object", contentType);
     var binaryFormatter = new BinaryFormatter();
     var byteStream = new MemoryStream(body);
     var deserializedObject = (TestObject)binaryFormatter.Deserialize(byteStream);
     Assert.AreEqual(testObject, deserializedObject);
 }
 public void BytesToMessage()
 {
     var converter = new SimpleMessageConverter();
     var message = converter.ToMessage(new byte[] { 1, 2, 3 }, new MessageProperties());
     var contentType = message.MessageProperties.ContentType;
     var body = message.Body;
     Assert.AreEqual("application/octet-stream", contentType);
     Assert.AreEqual(3, body.Length);
     Assert.AreEqual(1, body[0]);
     Assert.AreEqual(2, body[1]);
     Assert.AreEqual(3, body[2]);
 }
 public void StringToMessage()
 {
     var converter = new SimpleMessageConverter();
     var message = converter.ToMessage("test", new MessageProperties());
     var contentType = message.MessageProperties.ContentType;
     var content = message.Body.ToStringWithEncoding(message.MessageProperties.ContentEncoding);
     Assert.AreEqual("text/plain", contentType);
     Assert.AreEqual("test", content);
 }
 /// <summary>
 /// Creates the template.
 /// </summary>
 /// <param name="concurrentConsumers">The concurrent consumers.</param>
 /// <returns>The template.</returns>
 /// <remarks></remarks>
 private RabbitTemplate CreateTemplate(int concurrentConsumers)
 {
     var template = new RabbitTemplate();
     var connectionFactory = new CachingConnectionFactory();
     connectionFactory.ChannelCacheSize = concurrentConsumers;
     connectionFactory.Port = BrokerTestUtils.GetPort();
     template.ConnectionFactory = connectionFactory;
     if (this.messageConverter == null)
     {
         var internalmessageConverter = new SimpleMessageConverter();
         internalmessageConverter.CreateMessageIds = true;
         this.messageConverter = internalmessageConverter;
     }
     template.MessageConverter = this.messageConverter;
     return template;
 }