public void TestBroserIteratively()
        {
            using (IConnection connection = CreateConnection())
                using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
                {
                    connection.Start();

                    IQueue queue = session.CreateTemporaryQueue();
                    // enqueue a message
                    using (IMessageProducer producer = session.CreateProducer(queue))
                    {
                        IMessage message = producer.CreateMessage();
                        producer.Send(message);
                    }

                    Thread.Sleep(2000);

                    // browse queue several times
                    for (int j = 0; j < 1000; j++)
                    {
                        using (QueueBrowser browser = session.CreateBrowser(queue) as QueueBrowser)
                        {
                            Tracer.DebugFormat("Running Iterative QueueBrowser sample #{0}", j);
                            IEnumerator enumeration = browser.GetEnumerator();
                            Assert.IsTrue(enumeration.MoveNext(), "should have received the second message");
                        }
                    }
                }
        }
Пример #2
0
 /// <summary>
 /// Closes the given queue browser and ignore any thrown exception.
 /// This is useful for typical <code>finally</code> blocks in manual EMS code.
 /// </summary>
 /// <param name="browser">The queue browser to close (may be <code>null</code>.</param>
 public static void CloseQueueBrowser(QueueBrowser browser)
 {
     if (browser != null)
     {
         try
         {
             browser.Close();
         } catch (EMSException ex)
         {
             logger.Debug("Could not close EMS QueueBrowser", ex);
         } catch (Exception ex)
         {
             logger.Debug("Unexpected exception on closing EMS QueueBrowser", ex);
         }
     }
 }
Пример #3
0
    public csBrowser(string[] args)
    {
        ParseArgs(args);

        try {
            tibemsUtilities.initSSLParams(serverUrl, args);
        }
        catch (Exception e)
        {
            System.Console.WriteLine("Exception: " + e.Message);
            System.Console.WriteLine(e.StackTrace);
            System.Environment.Exit(-1);
        }

        Console.WriteLine("\n------------------------------------------------------------------------");
        Console.WriteLine("csBrowser SAMPLE");
        Console.WriteLine("------------------------------------------------------------------------");
        Console.WriteLine("Server....................... " + ((serverUrl != null)?serverUrl:"localhost"));
        Console.WriteLine("User......................... " + ((userName != null)?userName:"******"));
        Console.WriteLine("Queue........................ " + queueName);
        Console.WriteLine("------------------------------------------------------------------------\n");

        try {
            ConnectionFactory factory = new TIBCO.EMS.ConnectionFactory(serverUrl);

            Connection      connection = factory.CreateConnection(userName, password);
            Session         session    = connection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);
            TIBCO.EMS.Queue queue      = session.CreateQueue(queueName);
            MessageProducer producer   = session.CreateProducer(queue);
            Message         message    = null;

            connection.Start();

            // drain the queue
            MessageConsumer consumer = session.CreateConsumer(queue);

            int drain_count = 0;

            Console.WriteLine("Draining the queue " + queueName);

            // read queue until empty
            while (consumer.Receive(1000) != null)
            {
                drain_count++;
            }
            Console.WriteLine("Drained " + drain_count + " messages from the queue");

            // close consumer to prevent any queue messages from being delivered
            consumer.Close();

            int message_number = 0;

            // send 5 messages into queue
            Console.WriteLine("Sending 5 messages into queue.");
            for (int i = 0; i < 5; i++)
            {
                message_number++;
                message = session.CreateMessage();
                message.SetIntProperty("msg_num", message_number);
                producer.Send(message);
            }

            // create browser and browse what is there in the queue
            Console.WriteLine("--- Browsing the queue.");

            QueueBrowser browser = session.CreateBrowser(queue);

            IEnumerator msgs = browser.GetEnumerator();

            int browseCount = 0;

            while (msgs.MoveNext())
            {
                message = (Message)msgs.Current;
                Console.WriteLine("Browsed message: number=" + message.GetIntProperty("msg_num"));
                browseCount++;
            }

            Console.WriteLine("--- No more messages in the queue.");

            // send 5 more messages into queue
            Console.WriteLine("Sending 5 more messages into queue.");
            for (int i = 0; i < 5; i++)
            {
                message_number++;
                message = session.CreateMessage();
                message.SetIntProperty("msg_num", message_number);
                producer.Send(message);
            }

            // try to browse again, if no success for some time then quit

            // notice that we will NOT receive new messages instantly. It
            // happens because QueueBrowser limits the frequency of query
            // requests sent into the queue after the queue was
            // empty. Internal engine only queries the queue every so many
            // seconds, so we'll likely have to wait here for some time.

            int attemptCount = 0;
            while (!msgs.MoveNext())
            {
                attemptCount++;
                Console.WriteLine("Waiting for messages to arrive, count=" + attemptCount);
                Thread.Sleep(1000);
                if (attemptCount > 30)
                {
                    Console.WriteLine("Still no messages in the queue after " + attemptCount + " seconds");
                    Environment.Exit(0);
                }
            }

            // got more messages, continue browsing
            Console.WriteLine("Found more messages. Continue browsing.");
            do
            {
                message = (Message)msgs.Current;
                Console.WriteLine("Browsed message: number=" + message.GetIntProperty("msg_num"));
            }  while (msgs.MoveNext());

            // close all and quit
            browser.Close();

            connection.Close();
        } catch (EMSException e) {
            Console.Error.WriteLine("Exception in csBrowser: " + e.Message);
            Console.Error.WriteLine(e.StackTrace);
            Environment.Exit(0);
        } catch (ThreadInterruptedException e) {
            Console.Error.WriteLine("Exception in csBrowser: " + e.Message);
            Console.Error.WriteLine(e.StackTrace);
            Environment.Exit(0);
        }
    }