Ejemplo n.º 1
0
        public void AutomatiallyCreatesRecipientQueue()
        {
            // arrange
            const string senderInputQueue = "test.autocreate.sender";
            const string recipientInputQueue = "test.autocreate.recipient";
            const string someText = "whoa! as if by magic!";

            // ensure recipient queue does not exist
            DeleteQueue(senderInputQueue);
            DeleteQueue(recipientInputQueue);

            using (var sender = new RabbitMqMessageQueue(ConnectionString, senderInputQueue))
            {
                // act
                sender.Send(recipientInputQueue, new TransportMessageToSend
                    {
                        Body = Encoding.GetBytes(someText)
                    }, new NoTransaction());
            }

            using (var recipient = new RabbitMqMessageQueue(ConnectionString, recipientInputQueue))
            {
                // assert
                var receivedTransportMessage = recipient.ReceiveMessage(new NoTransaction());
                receivedTransportMessage.ShouldNotBe(null);
                Encoding.GetString(receivedTransportMessage.Body).ShouldBe(someText);
            }
        }
Ejemplo n.º 2
0
        public void CanSendToQueueWithEmptyExchangeAfterAtSign()
        {
            const string recipientInputQueueName = "test.atsimbol.recipient@";
            const string senderInputQueueName = "test.atsimbol.sender";

            using (var recipientQueue = new RabbitMqMessageQueue(ConnectionString, recipientInputQueueName))
            using (var senderQueue = new RabbitMqMessageQueue(ConnectionString, senderInputQueueName))
            {
                recipientQueue.PurgeInputQueue();
                senderQueue.PurgeInputQueue();

                var id = Guid.NewGuid();
                senderQueue.Send(recipientInputQueueName,
                                 serializer.Serialize(new Message
                                 {
                                     Messages = new object[] { "HELLO WORLD!" },
                                     Headers =
                                         new Dictionary<string, object> { 
                                            { Headers.MessageId, id.ToString() }
                                         },
                                 }),
                                 new NoTransaction());

                // act
                Thread.Sleep(2.Seconds() + 1.Seconds());

                // assert
                var receivedTransportMessage = recipientQueue.ReceiveMessage(new NoTransaction());
                Assert.That(receivedTransportMessage, Is.Not.Null);
            }
        }
Ejemplo n.º 3
0
        public void TransportLevelMessageIdIsPreserved()
        {
            const string recipientInputQueueName = "test.tlid.recipient";
            const string senderInputQueueName = "test.tlid.sender";

            using (var recipientQueue = new RabbitMqMessageQueue(ConnectionString, recipientInputQueueName))
            using (var senderQueue = new RabbitMqMessageQueue(ConnectionString, senderInputQueueName))
            {
                recipientQueue.PurgeInputQueue();
                senderQueue.PurgeInputQueue();

                var id = Guid.NewGuid();
                senderQueue.Send(recipientInputQueueName,
                                 serializer.Serialize(new Message
                                 {
                                     Messages = new object[] { "HELLO WORLD!" },
                                     Headers =
                                         new Dictionary<string, object> { 
                                            { Headers.MessageId, id.ToString() }
                                         },
                                 }),
                                 new NoTransaction());

                // act
                Thread.Sleep(2.Seconds() + 1.Seconds());

                // assert
                var receivedTransportMessage = recipientQueue.ReceiveMessage(new NoTransaction());
                Assert.That(receivedTransportMessage, Is.Not.Null);
                Assert.That(receivedTransportMessage.Headers, Is.Not.Null);
                Assert.That(receivedTransportMessage.Headers.ContainsKey(Headers.MessageId), Is.True);
                Assert.That(receivedTransportMessage.Headers[Headers.MessageId], Is.EqualTo(id.ToString()));
            }
        }
Ejemplo n.º 4
0
        public void MessageExpirationWorks()
        {
            // arrange
            var timeToBeReceived = 2.Seconds().ToString();

            const string recipientInputQueueName = "test.expiration.recipient";
            const string senderInputQueueName = "test.expiration.sender";

            using (var recipientQueue = new RabbitMqMessageQueue(ConnectionString, recipientInputQueueName))
            using (var senderQueue = new RabbitMqMessageQueue(ConnectionString, senderInputQueueName))
            {
                senderQueue.Send(recipientInputQueueName,
                                 serializer.Serialize(new Message
                                                          {
                                                              Messages = new object[] { "HELLO WORLD!" },
                                                              Headers =
                                                                  new Dictionary<string, object>
                                                                      {
                                                                          {
                                                                              Headers.TimeToBeReceived,
                                                                              timeToBeReceived
                                                                          }
                                                                      },
                                                          }),
                                 new NoTransaction());

                // act
                Thread.Sleep(2.Seconds() + 1.Seconds());

                // assert
                var receivedTransportMessage = recipientQueue.ReceiveMessage(new NoTransaction());
                Assert.That(receivedTransportMessage, Is.Null);
            }
        }
Ejemplo n.º 5
0
        static void Run(Parameters parameters)
        {
            if (string.IsNullOrWhiteSpace(parameters.ErrorQueueName))
            {
                throw new NiceException("Please specify the name of an error queue");
            }
            if (string.IsNullOrWhiteSpace(parameters.Host))
            {
                throw new NiceException("Please specify the hostname");
            }

            using (var rabbitMqMessageQueue = new RabbitMqMessageQueue(parameters.Host, parameters.ErrorQueueName))
            {
                using (var tx = new TransactionScope())
                {
                    var transactionContext = new AmbientTransactionContext();

                    var allTheMessages = GetAllTheMessages(rabbitMqMessageQueue, transactionContext);

                    foreach (var message in allTheMessages)
                    {
                        var transportMessageToSend = message.ToForwardableMessage();
                        try
                        {
                            if (!transportMessageToSend.Headers.ContainsKey(Headers.SourceQueue))
                            {
                                throw new NiceException("Message {0} does not have a source queue header - it will be moved back to the input queue",
                                    message.Id);
                            }

                            var sourceQueue = (string)transportMessageToSend.Headers[Headers.SourceQueue];

                            if (parameters.AutoMoveAllMessages.GetValueOrDefault())
                            {
                                rabbitMqMessageQueue.Send(sourceQueue, transportMessageToSend, transactionContext);

                                Print("Moved {0} to {1}", message.Id, sourceQueue);
                            }
                            else
                            {
                                var answer = PromptChar(new[] { 'y', 'n' }, "Would you like to move {0} to {1}? (y/n)",
                                                        message.Id, sourceQueue);

                                if (answer == 'y')
                                {
                                    rabbitMqMessageQueue.Send(sourceQueue, transportMessageToSend, transactionContext);

                                    Print("Moved {0} to {1}", message.Id, sourceQueue);
                                }
                                else
                                {
                                    rabbitMqMessageQueue.Send(rabbitMqMessageQueue.InputQueueAddress,
                                                          transportMessageToSend,
                                                          transactionContext);

                                    Print("Moved {0} to {1}", message.Id, rabbitMqMessageQueue.InputQueueAddress);
                                }
                            }
                        }
                        catch (NiceException e)
                        {
                            Print(e.Message);

                            rabbitMqMessageQueue.Send(rabbitMqMessageQueue.InputQueueAddress,
                                                  transportMessageToSend,
                                                  transactionContext);
                        }
                    }

                    if (parameters.DryRun.GetValueOrDefault())
                    {
                        Print("Aborting queue transaction");
                        return;
                    }

                    if (!parameters.Interactive)
                    {
                        tx.Complete();
                        return;
                    }

                    var commitAnswer = PromptChar(new[] { 'y', 'n' }, "Would you like to commit the queue transaction?");

                    if (commitAnswer == 'y')
                    {
                        Print("Committing queue transaction");

                        tx.Complete();
                        return;
                    }

                    Print("Queue transaction aborted");
                }
            }
        }