예제 #1
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();
 }
예제 #2
0
        public void TestReplyToTwoDeep()
        {
            var mockConnectionFactory = new Mock <ConnectionFactory>();
            var mockConnection        = new Mock <IConnection>();
            var mockChannel           = new Mock <IModel>();

            mockConnectionFactory.Setup(m => m.CreateConnection()).Returns(mockConnection.Object);
            mockConnection.Setup(m => m.IsOpen).Returns(true);
            mockConnection.Setup(m => m.CreateModel()).Returns(mockChannel.Object);
            mockChannel.Setup(m => m.CreateBasicProperties()).Returns(() => new BasicProperties());

            var template   = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory.Object));
            var replyQueue = new Queue("new.replyTo");

            template.ReplyQueue = replyQueue;

            var messageProperties = new MessageProperties();

            messageProperties.ReplyTo = "replyTo2";
            messageProperties.SetHeader(RabbitTemplate.STACKED_REPLY_TO_HEADER, "replyTo1");
            messageProperties.SetHeader(RabbitTemplate.STACKED_CORRELATION_HEADER, "a");
            var message = new Message(Encoding.UTF8.GetBytes("Hello, world!"), messageProperties);
            var props   = new List <IBasicProperties>();

            mockChannel.Setup(m => m.BasicPublish(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IBasicProperties>(), It.IsAny <byte[]>())).Callback <string, string, bool, bool, IBasicProperties, byte[]>
            (
                (a1, a2, a3, a4, a5, a6) =>
            {
                var basicProps = a5;
                props.Add(basicProps);
                var springProps  = new DefaultMessagePropertiesConverter().ToMessageProperties(basicProps, null, "UTF-8");
                var replyMessage = new Message(Encoding.UTF8.GetBytes("!dlrow olleH"), springProps);
                template.OnMessage(replyMessage);
            });

            var reply = template.SendAndReceive(message);

            Assert.AreEqual(1, props.Count);
            var basicProperties = props[0];

            Assert.AreEqual("new.replyTo", basicProperties.ReplyTo);
            Assert.AreEqual("replyTo2:replyTo1", basicProperties.Headers[RabbitTemplate.STACKED_REPLY_TO_HEADER]);
            Assert.IsTrue(((string)basicProperties.Headers[RabbitTemplate.STACKED_CORRELATION_HEADER]).EndsWith(":a"));

            Assert.AreEqual("replyTo1", reply.MessageProperties.Headers[RabbitTemplate.STACKED_REPLY_TO_HEADER]);
            Assert.AreEqual("a", reply.MessageProperties.Headers[RabbitTemplate.STACKED_CORRELATION_HEADER]);
        }
        public void TestSendAndReceiveInCallback()
        {
            this.template.ConvertAndSend(ROUTE, "message");
            var messagePropertiesConverter = new DefaultMessagePropertiesConverter();
            var result = this.template.Execute(
                delegate(IModel channel)
            {
                // We need noAck=false here for the message to be expicitly
                // acked
                var response     = channel.BasicGet(ROUTE, false);
                var messageProps = messagePropertiesConverter.ToMessageProperties(response.BasicProperties, response, "UTF-8");

                // Explicit ack
                channel.BasicAck(response.DeliveryTag, false);
                return((string)new SimpleMessageConverter().FromMessage(new Message(response.Body, messageProps)));
            });

            Assert.AreEqual("message", result);
            result = (string)this.template.ReceiveAndConvert(ROUTE);
            Assert.AreEqual(null, result);
        }