コード例 #1
0
        public void MangledMessageIsNotReceived()
        {
            using (var messageQueue = new MessageQueue(MsmqUtil.GetPath(_inputQueueName)))
            {
                var transaction = new MessageQueueTransaction();
                transaction.Begin();
                messageQueue.Send(new Message
                {
                    Extension = Encoding.UTF32.GetBytes("this is definitely not valid UTF8-encoded JSON")
                }, transaction);
                transaction.Commit();
            }

            Thread.Sleep(5000);

            CleanUpDisposables();

            using (var messageQueue = new MessageQueue(MsmqUtil.GetPath(_inputQueueName)))
            {
                messageQueue.MessageReadPropertyFilter = new MessagePropertyFilter
                {
                    Extension = true
                };

                var transaction = new MessageQueueTransaction();
                transaction.Begin();

                var message = messageQueue.Receive(transaction);

                Assert.That(message, Is.Not.Null);
                Assert.That(Encoding.UTF32.GetString(message.Extension), Is.EqualTo("this is definitely not valid UTF8-encoded JSON"));

                transaction.Commit();
            }
        }
コード例 #2
0
 protected override IDuplexTransport CreateTransport(string inputQueueName)
 {
     RegisterForDisposal(new DisposableAction(() => MsmqUtil.Delete(inputQueueName)));
     RegisterForDisposal(new DisposableAction(() => MsmqUtil.Delete(ErrorQueueName)));
     MsmqUtil.EnsureMessageQueueExists(MsmqUtil.GetPath(ErrorQueueName));
     return(new MsmqMessageQueue(inputQueueName).PurgeInputQueue());
 }
コード例 #3
0
 static void DeleteQueueIfExists()
 {
     if (MessageQueue.Exists(MsmqUtil.GetPath(QueueName)))
     {
         MsmqUtil.Delete(QueueName);
     }
 }
コード例 #4
0
ファイル: MsmqMessageQueue.cs プロジェクト: ssboisen/Rebus
        public MsmqMessageQueue(string inputQueueName)
        {
            if (inputQueueName == null)
            {
                return;
            }

            try
            {
                machineAddress = GetMachineAddress();

                inputQueuePath = MsmqUtil.GetPath(inputQueueName);
                MsmqUtil.EnsureMessageQueueExists(inputQueuePath);
                MsmqUtil.EnsureMessageQueueIsTransactional(inputQueuePath);
                EnsureMessageQueueIsLocal(inputQueueName);

                inputQueue = GetMessageQueue(inputQueuePath);

                this.inputQueueName = inputQueueName;
            }
            catch (MessageQueueException e)
            {
                throw new ArgumentException(
                          string.Format(
                              @"An error occurred while initializing MsmqMessageQueue - attempted to use '{0}' as input queue",
                              inputQueueName), e);
            }
        }
コード例 #5
0
        static List <int> SendMessages(int messageCount)
        {
            var transport = new MsmqTransport(QueueName, new ConsoleLoggerFactory(true));

            MsmqUtil.EnsureQueueExists(MsmqUtil.GetPath(QueueName));

            var sendIds = new List <int>();

            Enumerable.Range(0, messageCount)
            .Select(id => new SomeMessage {
                Id = id
            })
            .ToList()
            .ForEach(msg =>
            {
                using (var scope = new RebusTransactionScope())
                {
                    transport.Send(QueueName, TransportMessageHelpers.FromString(JsonConvert.SerializeObject(msg)), scope.TransactionContext).Wait();

                    scope.Complete();

                    sendIds.Add(msg.Id);
                }
            });

            return(sendIds);
        }
コード例 #6
0
        public void MangledMessageIsReceived()
        {
            using (var messageQueue = new MessageQueue(MsmqUtil.GetPath(_inputQueueName)))
            {
                var transaction = new MessageQueueTransaction();
                transaction.Begin();
                messageQueue.Send(new Message
                {
                    Extension = Encoding.UTF32.GetBytes("this is definitely not valid UTF8-encoded JSON")
                }, transaction);
                transaction.Commit();
            }

            Thread.Sleep(5000);

            CleanUpDisposables();

            using (var messageQueue = new MessageQueue(MsmqUtil.GetPath(_inputQueueName)))
            {
                messageQueue.MessageReadPropertyFilter = new MessagePropertyFilter
                {
                    Extension = true
                };

                Assert.Catch <MessageQueueException>(() => messageQueue.Receive(TimeSpan.FromSeconds(2)));
            }
        }
コード例 #7
0
        protected override void DoSetUp()
        {
            MsmqUtil.Delete(SenderInputQueueName);
            MsmqUtil.Delete(ReceiverInputQueueName);
            MsmqUtil.Delete(ErrorQueueName);

            MsmqUtil.EnsureMessageQueueExists(MsmqUtil.GetPath(ErrorQueueName));
        }
コード例 #8
0
 public MsmqMessageQueue(string inputQueueName, string errorQueue)
 {
     inputQueuePath = MsmqUtil.GetPath(inputQueueName);
     inputQueue     = CreateMessageQueue(inputQueuePath, createIfNotExists: true);
     EnsureMessageQueueExists(MsmqUtil.GetPath(errorQueue), createIfNotExists: true);
     this.inputQueueName = inputQueueName;
     this.errorQueue     = errorQueue;
 }
コード例 #9
0
        static MessageQueue GetMessageQueue(string queueName)
        {
            var queue = new MessageQueue(MsmqUtil.GetPath(queueName));

            queue.MessageReadPropertyFilter = new MessagePropertyFilter
            {
                Id        = true,
                Extension = true,
                Body      = true,
            };
            return(queue);
        }
コード例 #10
0
        public void Send(string destinationQueueName, TransportMessageToSend message)
        {
            var recipientPath = MsmqUtil.GetPath(destinationQueueName);

            using (var outputQueue = CreateMessageQueue(recipientPath, createIfNotExists: false))
            {
                var transactionWrapper = GetOrCreateTransactionWrapper();

                outputQueue.Send(message, transactionWrapper.MessageQueueTransaction);

                transactionWrapper.Commit();
            }
        }
コード例 #11
0
ファイル: TimeoutService.cs プロジェクト: ninocrudele/Rebus
        void Initialize(IStoreTimeouts storeTimeouts, ISendMessages sendMessages, IReceiveMessages receiveMessages, string errorQueueName)
        {
            var errorQueuePath = MsmqUtil.GetPath(errorQueueName);

            MsmqUtil.EnsureMessageQueueExists(errorQueuePath);
            MsmqUtil.EnsureMessageQueueIsTransactional(errorQueuePath);

            rebusBus = new RebusBus(this, sendMessages, receiveMessages, null, null, null,
                                    new JsonMessageSerializer(),
                                    new TrivialPipelineInspector(),
                                    new ErrorTracker(errorQueueName),
                                    storeTimeouts, new ConfigureAdditionalBehavior());
        }
コード例 #12
0
        protected override void DoSetUp()
        {
            adapter = TrackDisposable(new BuiltinContainerAdapter());

            MsmqUtil.EnsureMessageQueueExists(MsmqUtil.GetPath(AuditQueueName));
            MsmqUtil.PurgeQueue(AuditQueueName);
            MsmqUtil.PurgeQueue(InputQueueName);

            Configure.With(adapter)
            .Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))
            .Transport(t => t.UseMsmq(InputQueueName, "error"))
            .Behavior(b => b.EnableMessageAudit(AuditQueueName))
            .CreateBus()
            .Start(1);
        }
コード例 #13
0
ファイル: MsmqTestHelper.cs プロジェクト: mgibas/Rebus
        public static IEnumerable <Message> GetMessagesFrom(string queueName)
        {
            using (var queue = new MessageQueue(MsmqUtil.GetPath(queueName)))
            {
                queue.Formatter = new RebusTransportMessageFormatter();
                queue.MessageReadPropertyFilter = RebusTransportMessageFormatter.PropertyFilter;

                bool gotMessage;

                do
                {
                    Message messageToReturn;
                    try
                    {
                        var msmqMessage = queue.Receive(3.Seconds());
                        if (msmqMessage == null)
                        {
                            yield break;
                        }
                        var receivedTransportMessage = (ReceivedTransportMessage)msmqMessage.Body;
                        var serializer = new JsonMessageSerializer();

                        messageToReturn = serializer.Deserialize(receivedTransportMessage);
                    }
                    catch (MessageQueueException exception)
                    {
                        if (exception.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                        {
                            yield break;
                        }

                        throw;
                    }

                    if (messageToReturn != null)
                    {
                        gotMessage = true;
                        yield return(messageToReturn);
                    }
                    else
                    {
                        gotMessage = false;
                    }
                } while (gotMessage);
            }
        }
コード例 #14
0
        static void DoIt(TransportConfigurer configurer, string inputQueueName, string errorQueueName)
        {
            if (string.IsNullOrEmpty(inputQueueName))
            {
                throw new ConfigurationErrorsException("You need to specify an input queue.");
            }

            var msmqMessageQueue = new MsmqMessageQueue(inputQueueName);

            var errorQueuePath = MsmqUtil.GetPath(errorQueueName);

            MsmqUtil.EnsureMessageQueueExists(errorQueuePath);
            MsmqUtil.EnsureMessageQueueIsTransactional(errorQueuePath);

            configurer.UseSender(msmqMessageQueue);
            configurer.UseReceiver(msmqMessageQueue);
            configurer.UseErrorTracker(new ErrorTracker(errorQueueName));
        }
コード例 #15
0
ファイル: TestMsmqUtil.cs プロジェクト: tminx/Rebus.Msmq
        public async Task CanGetCount()
        {
            var path = MsmqUtil.GetPath(QueueName);

            MsmqUtil.EnsureQueueExists(path);

            Console.WriteLine($"Checking {path}");

            var countBefore = MsmqUtil.GetCount(path);

            await SendMessageTo(QueueName);
            await SendMessageTo(QueueName);
            await SendMessageTo(QueueName);

            var countAfter = MsmqUtil.GetCount(path);

            Assert.That(countBefore, Is.EqualTo(0));
            Assert.That(countAfter, Is.EqualTo(3));
        }
コード例 #16
0
        protected override void SetUp()
        {
            _newEndpoint = TestConfig.QueueName("newendpoint");
            _oldEndpoint = TestConfig.QueueName("oldendpoint");

            MsmqUtil.EnsureQueueExists(MsmqUtil.GetPath(_newEndpoint));
            MsmqUtil.EnsureQueueExists(MsmqUtil.GetPath(_oldEndpoint));

            _activator = Using(new BuiltinHandlerActivator());

            _bus = Configure.With(_activator)
                   .Transport(t => t.UseMsmq(_newEndpoint))
                   .Options(o =>
            {
                o.EnableLegacyCompatibility();
                o.LogPipeline(true);
            })
                   .Start();
        }
コード例 #17
0
        public void ThrowsIfExistingQueueIsNotTransactional()
        {
            // arrange
            var queueName = "test.some.random.queue";
            var queuePath = MsmqUtil.GetPath(queueName);

            if (MessageQueue.Exists(queuePath))
            {
                MessageQueue.Delete(queuePath);
            }

            MessageQueue.Create(queuePath, transactional: false);

            // act
            var invalidOperationException = Assert.Throws <InvalidOperationException>(() => new MsmqMessageQueue(queueName));

            // assert
            invalidOperationException.Message.ShouldContain(queueName);
        }
コード例 #18
0
        MessageQueue NewRawMsmqQueue(string queueName)
        {
            var queuePath = MsmqUtil.GetPath(queueName);

            if (!MessageQueue.Exists(queuePath))
            {
                var messageQueue = MessageQueue.Create(queuePath, true);
                messageQueue.SetPermissions(Thread.CurrentPrincipal.Identity.Name, MessageQueueAccessRights.FullControl);
            }

            var newRawMsmqQueue = new MessageQueue(queuePath)
            {
                Formatter = new RebusTransportMessageFormatter(),
                MessageReadPropertyFilter = RebusTransportMessageFormatter.PropertyFilter,
            };

            newRawMsmqQueue.Purge();

            return(newRawMsmqQueue);
        }
コード例 #19
0
        protected override void SetUp()
        {
            _newEndpoint = TestConfig.GetName("newendpoint");
            _oldEndpoint = TestConfig.GetName("oldendpoint");

            MsmqUtil.EnsureQueueExists(MsmqUtil.GetPath(_newEndpoint));
            MsmqUtil.EnsureQueueExists(MsmqUtil.GetPath(_oldEndpoint));

            _activator = Using(new BuiltinHandlerActivator());

            _subscriptions = new Dictionary <string, HashSet <string> >();

            _bus = Configure.With(_activator)
                   .Transport(t => t.UseMsmq(_newEndpoint))
                   .Subscriptions(s => s.Decorate(c => new SubDec(c.Get <ISubscriptionStorage>(), _subscriptions)))
                   .Routing(m => m.TypeBased().Map <string>(_oldEndpoint))
                   .Options(o =>
            {
                o.EnableLegacyCompatibility();
                o.LogPipeline();
            })
                   .Start();
        }
コード例 #20
0
        static void DoIt(RebusTransportConfigurer configurer, string inputQueueName, string errorQueueName)
        {
            if (string.IsNullOrEmpty(inputQueueName))
            {
                throw new ConfigurationErrorsException("You need to specify an input queue.");
            }

            var msmqMessageQueue = new MsmqMessageQueue(inputQueueName);

            // since these operations only make sense to perform on a local queue, we'll skip it if the error queue is remote
            // (read http://blogs.msdn.com/b/johnbreakwell/archive/2008/07/31/checking-if-msmq-queues-exist-is-hard-work-so-should-you-bother.aspx
            // for more info...)
            if (MsmqUtil.IsLocal(errorQueueName))
            {
                var errorQueuePath = MsmqUtil.GetPath(errorQueueName);

                MsmqUtil.EnsureMessageQueueExists(errorQueuePath);
                MsmqUtil.EnsureMessageQueueIsTransactional(errorQueuePath);
            }

            configurer.UseSender(msmqMessageQueue);
            configurer.UseReceiver(msmqMessageQueue);
            configurer.UseErrorTracker(new ErrorTracker(errorQueueName));
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: mgibas/Rebus
        static void Run(Parameters parameters)
        {
            if (string.IsNullOrWhiteSpace(parameters.ErrorQueueName))
            {
                throw new NiceException("Please specify the name of an error queue");
            }

            using (var tx = new TransactionScope())
            {
                var transactionContext = new AmbientTransactionContext();

                if (!MessageQueue.Exists(MsmqUtil.GetPath(parameters.ErrorQueueName)))
                {
                    throw new NiceException("The MSMQ queue '{0}' does not exist!", parameters.ErrorQueueName);
                }

                var msmqMessageQueue = new MsmqMessageQueue(parameters.ErrorQueueName, allowRemoteQueue: true);
                var allTheMessages   = GetAllTheMessages(msmqMessageQueue, 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())
                        {
                            msmqMessageQueue.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')
                            {
                                msmqMessageQueue.Send(sourceQueue, transportMessageToSend, transactionContext);

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

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

                        msmqMessageQueue.Send(msmqMessageQueue.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");
            }
        }
コード例 #22
0
 protected override void DoSetUp()
 {
     MsmqUtil.EnsureMessageQueueExists(MsmqUtil.GetPath(AuditQueue));
 }
コード例 #23
0
        protected override void DoTearDown()
        {
            adapter.Dispose();

            MessageQueue.Delete(MsmqUtil.GetPath(InputQueueName));
        }
コード例 #24
0
 protected override IDuplexTransport CreateTransport(string inputQueueName)
 {
     MsmqUtil.EnsureMessageQueueExists(MsmqUtil.GetPath(ErrorQueueName));
     return(new MsmqMessageQueue(inputQueueName).PurgeInputQueue());
 }