Example #1
0
 internal void OnQueueBrowserClosed(IQueueBrowser browser)
 {
     lock (browsers)
     {
         browsers.Remove(browser);
     }
 }
Example #2
0
        protected static void VerifyBrokerQueueCountNoRecovery(int expectedNumberOfMessages)
        {
            IConnectionFactory factory = new ConnectionFactory(ReplaceEnvVar(connectionUri));

            using (IConnection connection = factory.CreateConnection())
            {
                // check messages are present in the queue
                using (ISession session = connection.CreateSession())
                {
                    IQueue queue = session.GetQueue(testQueueName);

                    using (IQueueBrowser browser = session.CreateBrowser(queue))
                    {
                        connection.Start();
                        int         count      = 0;
                        IEnumerator enumerator = browser.GetEnumerator();

                        while (enumerator.MoveNext())
                        {
                            IMessage msg = enumerator.Current as IMessage;
                            Assert.IsNotNull(msg, "message is not in the queue !");
                            count++;
                        }

                        // count should match the expected count
                        Assert.AreEqual(expectedNumberOfMessages, count);
                    }
                }
            }
        }
Example #3
0
        protected void VerifyBrokerQueueCount(int expectedCount, string connectionUri)
        {
            using (INetTxConnection connection = dtcFactory.CreateNetTxConnection())
            {
                // check messages are present in the queue
                using (INetTxSession session = connection.CreateNetTxSession())
                {
                    IQueue queue = session.GetQueue(testQueueName);

                    using (IQueueBrowser browser = session.CreateBrowser(queue))
                    {
                        connection.Start();
                        int         count      = 0;
                        IEnumerator enumerator = browser.GetEnumerator();

                        while (enumerator.MoveNext())
                        {
                            IMessage msg = enumerator.Current as IMessage;
                            Assert.IsNotNull(msg, "message is not in the queue !");
                            count++;
                        }

                        // count should match the expected count
                        Assert.AreEqual(expectedCount, count);
                    }
                }
            }
        }
        public void TestRedelivered()
        {
            // enqueue several messages
            PurgeDatabase();
            PurgeAndFillQueue();

            // receive just one
            using (INetTxConnection connection = dtcFactory.CreateNetTxConnection())
            {
                connection.Start();

                using (INetTxSession session = connection.CreateNetTxSession())
                {
                    IQueue queue = session.GetQueue(testQueueName);

                    // read message from queue and insert into db table
                    using (IMessageConsumer consumer = session.CreateConsumer(queue))
                    {
                        using (TransactionScope scoped = new TransactionScope(TransactionScopeOption.RequiresNew))
                            using (SqlConnection sqlConnection = new SqlConnection(createDbConnectionString))
                                using (SqlCommand sqlInsertCommand = new SqlCommand())
                                {
                                    sqlConnection.Open();
                                    sqlInsertCommand.Connection = sqlConnection;

                                    ITextMessage message = consumer.Receive(TimeSpan.FromMilliseconds(10000)) as ITextMessage;
                                    sqlInsertCommand.CommandText =
                                        string.Format("INSERT INTO {0} VALUES ({1})", testTable, Convert.ToInt32(message.Text));
                                    sqlInsertCommand.ExecuteNonQuery();

                                    scoped.Complete();
                                }
                    }

                    session.Close();
                }
            }

            // check that others message have status redelivered = false
            IConnectionFactory checkFactory = new ConnectionFactory(ReplaceEnvVar(connectionUri));

            using (IConnection connection = checkFactory.CreateConnection())
            {
                connection.Start();

                using (ISession session = connection.CreateSession())
                    using (IQueueBrowser browser = session.CreateBrowser(session.GetQueue(testQueueName)))
                    {
                        IEnumerator enumerator = browser.GetEnumerator();

                        while (enumerator.MoveNext())
                        {
                            IMessage msg = enumerator.Current as IMessage;
                            Assert.IsNotNull(msg, "message is not in the queue!");
                            Assert.IsFalse(msg.NMSRedelivered, "message is redelivered!");
                        }
                    }
            }
        }
Example #5
0
        private IQueueBrowser AddQueueBrowser(IQueueBrowser browser)
        {
            lock (browsers)
            {
                browsers.AddLast(browser);
            }

            return(new PooledQueueBrowser(this, browser));
        }
 public void TestCreateBrowserFailsWithZeroPrefetch()
 {
     using (Connection connection = CreateConnection() as Connection)
         using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
         {
             connection.PrefetchPolicy.QueueBrowserPrefetch = 0;
             IQueue        queue   = session.CreateTemporaryQueue();
             IQueueBrowser browser = session.CreateBrowser(queue);
             browser.Close();
         }
 }
        public void TestSelectByLookupId(
            [Values(SELECTOR_TEST_QUEUE)]
            string testDestRef)
        {
            using (IConnection connection = CreateConnection())
            {
                connection.Start();
                using (ISession session = connection.CreateSession())
                {
                    IDestination destination = GetClearDestinationByNodeReference(session, testDestRef);

                    using (IMessageProducer producer = session.CreateProducer(destination))
                    {
                        ITextMessage message = null;

                        int COUNT = 5;
                        for (int i = 1; i <= COUNT; i++)
                        {
                            message = session.CreateTextMessage("MessageSelectorTest - TestSelectByLookupId: " + i.ToString());
                            producer.Send(message);
                        }

                        using (IQueueBrowser browser = session.CreateBrowser((IQueue)destination))
                        {
                            int i = 0;
                            foreach (IMessage message0 in browser)
                            {
                                if (++i == COUNT / 2)
                                {
                                    message = message0 as ITextMessage;
                                    break;
                                }
                            }
                        }

                        long   lookupId = (long)(message.Properties["LookupId"]);
                        string selector = "LookupId = " + lookupId.ToString(CultureInfo.InvariantCulture);

                        using (IMessageConsumer consumer = session.CreateConsumer(destination, selector))
                        {
                            ITextMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as ITextMessage;
                            Assert.IsNotNull(msg);
                            Assert.AreEqual(msg.Text, message.Text);
                            Assert.AreEqual(msg.Properties["LookupId"], lookupId);

                            msg = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as ITextMessage;
                            Assert.IsNull(msg);
                        }
                    }
                }
            }
        }
        public void TestBrowseReceive()
        {
            using (IConnection connection = CreateConnection())
            {
                using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
                {
                    IDestination destination = session.GetQueue("TEST.BrowseReceive");

                    connection.Start();

                    using (IMessageConsumer purger = session.CreateConsumer(destination))
                    {
                        // lets consume any outstanding messages from previous test runs
                        while (purger.Receive(TimeSpan.FromMilliseconds(1000)) != null)
                        {
                        }

                        purger.Close();
                    }

                    IMessage[] outbound = new IMessage[] { session.CreateTextMessage("First Message"),
                                                           session.CreateTextMessage("Second Message"),
                                                           session.CreateTextMessage("Third Message") };

                    IMessageProducer producer = session.CreateProducer(destination);
                    producer.Send(outbound[0]);

                    // create browser first
                    IQueueBrowser browser     = session.CreateBrowser((IQueue)destination);
                    IEnumerator   enumeration = browser.GetEnumerator();

                    // create consumer
                    IMessageConsumer consumer = session.CreateConsumer(destination);

                    // browse the first message
                    Assert.IsTrue(enumeration.MoveNext(), "should have received the first message");
                    Assert.AreEqual(((ITextMessage)outbound[0]).Text, ((ITextMessage)enumeration.Current).Text);

                    // Receive the first message.
                    Assert.AreEqual(((ITextMessage)outbound[0]).Text, ((ITextMessage)consumer.Receive(TimeSpan.FromMilliseconds(1000))).Text);
                    consumer.Close();
                    browser.Close();
                    producer.Close();
                }
            }
        }
Example #9
0
        private int Browse(String queueName, Connection connection)
        {
            int browsed = 0;

            using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
                using (IQueue queue = session.GetQueue(queueName))
                    using (IQueueBrowser browser = session.CreateBrowser(queue))
                    {
                        IEnumerator enumeration = browser.GetEnumerator();
                        while (enumeration.MoveNext())
                        {
                            ITextMessage message = enumeration.Current as ITextMessage;
                            Tracer.DebugFormat("Browsed message: {0}", message.NMSMessageId);
                            browsed++;
                        }
                    }

            return(browsed);
        }
Example #10
0
        public static int GetPendingMessagesCount(string queueDestination)
        {
            int messageCount = 0;

            IConnectionFactory factory = new ConnectionFactory(BrokerUri);

            using IConnection connection = factory.CreateConnection(UserName, Password);
            connection.Start();

            using ISession session = connection.CreateSession();
            using IDestination requestDestination = SessionUtil.GetDestination(session, queueDestination);
            using IQueueBrowser queueBrowser      = session.CreateBrowser((IQueue)requestDestination);

            IEnumerator messages = queueBrowser.GetEnumerator();

            while (messages.MoveNext())
            {
                IMessage message = (IMessage)messages.Current;
                messageCount++;
            }

            return(messageCount);
        }
Example #11
0
        public static List <string> GetPendingMessages(string queueDestination)
        {
            var resultMessages = new List <string>();

            IConnectionFactory factory = new ConnectionFactory(BrokerUri);

            using IConnection connection = factory.CreateConnection(UserName, Password);
            connection.Start();

            using ISession session = connection.CreateSession();
            using IDestination requestDestination = SessionUtil.GetDestination(session, queueDestination);
            using IQueueBrowser queueBrowser      = session.CreateBrowser((IQueue)requestDestination);

            IEnumerator messages = queueBrowser.GetEnumerator();

            while (messages.MoveNext())
            {
                IMessage     message     = (IMessage)messages.Current;
                ITextMessage textMessage = message as ITextMessage;
                resultMessages.Add(textMessage.Text);
            }

            return(resultMessages);
        }
        public void TestReceiveBrowseReceive()
        {
            using (IConnection connection = CreateConnection())
            {
                using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
                {
                    IDestination     destination = session.GetQueue("TEST.ReceiveBrowseReceive");
                    IMessageProducer producer    = session.CreateProducer(destination);
                    IMessageConsumer consumer    = session.CreateConsumer(destination);
                    connection.Start();

                    IMessage[] outbound = new IMessage[] { session.CreateTextMessage("First Message"),
                                                           session.CreateTextMessage("Second Message"),
                                                           session.CreateTextMessage("Third Message") };

                    // lets consume any outstanding messages from previous test runs
                    while (consumer.Receive(TimeSpan.FromMilliseconds(1000)) != null)
                    {
                    }

                    producer.Send(outbound[0]);
                    producer.Send(outbound[1]);
                    producer.Send(outbound[2]);

                    IMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(1000));

                    // Get the first.
                    Assert.AreEqual(((ITextMessage)outbound[0]).Text, ((ITextMessage)msg).Text);
                    consumer.Close();

                    IQueueBrowser browser     = session.CreateBrowser((IQueue)destination);
                    IEnumerator   enumeration = browser.GetEnumerator();

                    // browse the second
                    Assert.IsTrue(enumeration.MoveNext(), "should have received the second message");
                    Assert.AreEqual(((ITextMessage)outbound[1]).Text, ((ITextMessage)enumeration.Current).Text);

                    // browse the third.
                    Assert.IsTrue(enumeration.MoveNext(), "Should have received the third message");
                    Assert.AreEqual(((ITextMessage)outbound[2]).Text, ((ITextMessage)enumeration.Current).Text);

                    // There should be no more.
                    bool tooMany = false;
                    while (enumeration.MoveNext())
                    {
                        Debug.WriteLine("Got extra message: " + ((ITextMessage)enumeration.Current).Text);
                        tooMany = true;
                    }
                    Assert.IsFalse(tooMany);

                    //Reset should take us back to the start.
                    enumeration.Reset();

                    // browse the second
                    Assert.IsTrue(enumeration.MoveNext(), "should have received the second message");
                    Assert.AreEqual(((ITextMessage)outbound[1]).Text, ((ITextMessage)enumeration.Current).Text);

                    // browse the third.
                    Assert.IsTrue(enumeration.MoveNext(), "Should have received the third message");
                    Assert.AreEqual(((ITextMessage)outbound[2]).Text, ((ITextMessage)enumeration.Current).Text);

                    // There should be no more.
                    tooMany = false;
                    while (enumeration.MoveNext())
                    {
                        Debug.WriteLine("Got extra message: " + ((ITextMessage)enumeration.Current).Text);
                        tooMany = true;
                    }
                    Assert.IsFalse(tooMany);

                    browser.Close();

                    // Re-open the consumer.
                    consumer = session.CreateConsumer(destination);

                    // Receive the second.
                    Assert.AreEqual(((ITextMessage)outbound[1]).Text, ((ITextMessage)consumer.Receive(TimeSpan.FromMilliseconds(1000))).Text);
                    // Receive the third.
                    Assert.AreEqual(((ITextMessage)outbound[2]).Text, ((ITextMessage)consumer.Receive(TimeSpan.FromMilliseconds(1000))).Text);
                    consumer.Close();
                }
            }
        }
Example #13
0
 internal void OnQueueBrowserClosed(IQueueBrowser browser)
 {
     lock(browsers)
     {
         browsers.Remove(browser);
     }
 }
Example #14
0
        /// <summary>
        /// �������
        /// </summary>
        protected override void Open()
        {
            // �ȴ�Session
            base.Open();

            // ����Ŀ�ĵ�
            _destination = Session.CreateQueue(_queueName);
            _destination.SetIntProperty(XMSC.DELIVERY_MODE, XMSC.DELIVERY_PERSISTENT);

            // ���������
            _browser = Session.CreateBrowser(_destination);
        }
Example #15
0
        /// <summary>
        /// �رն�����
        /// </summary>
        private void CloseBrowser()
        {
            try
            {
                if (_browser != null)
                {
                    _browser.Close();
                    _browser.Dispose();
                }
            }
            catch
            {
                // ����ԭ�ȵ������ѶϿ����ɺ��Դ��쳣��
            }

            _browser = null;
        }
 public PooledQueueBrowser(PooledSession session, IQueueBrowser browser)
 {
     this.session = session;
     this.browser = browser;
 }
        public void TestRedeliveredNoComplete()
        {
            const int messageCount = 300;
            const int receiveCount = 150;

            // enqueue several messages
            PurgeDatabase();
            PurgeAndFillQueue(messageCount);

            INetTxConnectionFactory factory = new NetTxConnectionFactory(ReplaceEnvVar(connectionURI));

            using (INetTxConnection connection = factory.CreateNetTxConnection())
            {
                // allow no redelivery so that message immediatly goes to the DLQ if first read fails
                connection.RedeliveryPolicy.MaximumRedeliveries = 0;
                connection.Start();

                // receive half of total messages
                using (INetTxSession session = connection.CreateNetTxSession())
                {
                    IQueue queue = session.GetQueue(testQueueName);

                    // read message from queue and insert into db table
                    using (IMessageConsumer consumer = session.CreateConsumer(queue))
                    {
                        for (int i = 0; i < receiveCount; i++)
                        {
                            using (TransactionScope scoped = new TransactionScope(TransactionScopeOption.RequiresNew))
                                using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
                                    using (SqlCommand sqlInsertCommand = new SqlCommand())
                                    {
                                        sqlConnection.Open();
                                        sqlInsertCommand.Connection = sqlConnection;

                                        ITextMessage message =
                                            consumer.Receive(TimeSpan.FromMilliseconds(10000)) as ITextMessage;
                                        sqlInsertCommand.CommandText =
                                            string.Format("INSERT INTO {0} VALUES ({1})", testTable,
                                                          Convert.ToInt32(message.Text));
                                        sqlInsertCommand.ExecuteNonQuery();
                                    }
                        }
                    }

                    session.Close();
                }
            }

            Tracer.Debug("First stage ok");

            // check that others message have status redelivered = false
            IConnectionFactory checkFactory = new ConnectionFactory(ReplaceEnvVar(connectionURI));

            using (IConnection connection = checkFactory.CreateConnection())
            {
                connection.Start();

                using (ISession session = connection.CreateSession())
                    using (IQueueBrowser browser = session.CreateBrowser(session.GetQueue(testQueueName)))
                    {
                        IEnumerator enumerator = browser.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            IMessage msg = enumerator.Current as IMessage;
                            Assert.IsNotNull(msg, "message is not in the queue!");
                            Assert.IsFalse(msg.NMSRedelivered, "message is redelivered!");
                        }
                    }
            }
        }
Example #18
0
        private IQueueBrowser AddQueueBrowser(IQueueBrowser browser)
        {
            lock(browsers)
            {
                browsers.AddLast(browser);
            }

            return new PooledQueueBrowser(this, browser);
        }
 public PooledQueueBrowser(PooledSession session, IQueueBrowser browser)
 {
     this.session = session;
     this.browser = browser;
 }