Exemple #1
0
        //[Test]
        public virtual void TestDoChangeSentMessage(
            string testDestRef,
            //[Values(AcknowledgementMode.AutoAcknowledge, AcknowledgementMode.ClientAcknowledge,
            //	AcknowledgementMode.DupsOkAcknowledge, AcknowledgementMode.Transactional)]
            AcknowledgementMode ackMode,
            //[Values(true, false)]
            bool doClear)
        {
            using (IConnection connection = CreateConnection())
            {
                connection.Start();
                using (ISession session = connection.CreateSession(ackMode))
                {
                    IDestination destination = GetClearDestinationByNodeReference(session, testDestRef);
                    using (IMessageConsumer consumer = session.CreateConsumer(destination))
                    {
                        IMessageProducer producer = session.CreateProducer(destination);
                        ITextMessage     message  = session.CreateTextMessage();

                        string prefix = "ConsumerTest - TestDoChangeSentMessage: ";

                        for (int i = 0; i < COUNT; i++)
                        {
                            message.Properties[VALUE_NAME] = i;
                            message.Text = prefix + Convert.ToString(i);

                            producer.Send(message);

                            if (doClear)
                            {
                                message.ClearBody();
                                message.ClearProperties();
                            }
                        }

                        if (ackMode == AcknowledgementMode.Transactional)
                        {
                            session.Commit();
                        }

                        for (int i = 0; i < COUNT; i++)
                        {
                            ITextMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as ITextMessage;
                            Assert.AreEqual(msg.Text, prefix + Convert.ToString(i));
                            Assert.AreEqual(msg.Properties.GetInt(VALUE_NAME), i);
                        }

                        if (ackMode == AcknowledgementMode.Transactional)
                        {
                            session.Commit();
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void PublishMsgToTopic(string brokerUrl, string topicName)
        {
            Apache.NMS.IConnection con = null;
            ITextMessage           msg = null;

            try
            {
                var factory = new ConnectionFactory(brokerUrl);
//                factory.PrefetchPolicy = new PrefetchPolicy() { DurableTopicPrefetch = 1, TopicPrefetch = 1 };

                con = factory.CreateConnection("admin", "admin");



                con.ClientId       = "OrderMsgProducer-1";
                con.RequestTimeout = TimeSpan.FromMinutes(5);

                con.Start();

                using (var session = con.CreateSession(Apache.NMS.AcknowledgementMode.ClientAcknowledge))
                {
                    var dest = (IDestination)session.GetTopic(topicName);

                    var prod = session.CreateProducer(dest);
                    prod.DeliveryMode = MsgDeliveryMode.NonPersistent;
                    prod.TimeToLive   = TimeSpan.FromMinutes(20);
                    do
                    {
                        Console.WriteLine("Please enter msg");
                        var textMsg = Console.ReadLine();
                        msg = prod.CreateTextMessage(textMsg);
                        msg.Properties.SetLong("x-opt-delivery-delay", 10000);
                        prod.Send(msg);
                        msg.ClearBody();
                        Console.WriteLine("Please press esc key to exit.");
                    } while (Console.ReadKey().Key != ConsoleKey.Escape);
                    prod.Close();
                }
            }
            catch (Exception ex)
            {
                string emsg = ex.Message;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
Exemple #3
0
        public void TestDoChangeSentMessage(
            [Values(AcknowledgementMode.AutoAcknowledge, AcknowledgementMode.ClientAcknowledge,
                    AcknowledgementMode.DupsOkAcknowledge, AcknowledgementMode.Transactional)]
            AcknowledgementMode ackMode)
        {
            using (IConnection connection = CreateConnection())
            {
                connection.Start();
                using (ISession session = connection.CreateSession(ackMode))
                {
                    ITemporaryQueue queue = session.CreateTemporaryQueue();
                    using (IMessageConsumer consumer = session.CreateConsumer(queue))
                    {
                        IMessageProducer producer = session.CreateProducer(queue);
                        ITextMessage     message  = session.CreateTextMessage();

                        string prefix = "ConsumerTest - TestDoChangeSentMessage: ";

                        for (int i = 0; i < COUNT; i++)
                        {
                            message.Properties[VALUE_NAME] = i;
                            message.Text = prefix + Convert.ToString(i);

                            producer.Send(message);

                            message.ClearBody();
                            message.ClearProperties();
                        }

                        if (ackMode == AcknowledgementMode.Transactional)
                        {
                            session.Commit();
                        }

                        for (int i = 0; i < COUNT; i++)
                        {
                            ITextMessage msg = consumer.Receive(TimeSpan.FromMilliseconds(2000)) as ITextMessage;
                            Assert.AreEqual(msg.Text, prefix + Convert.ToString(i));
                            Assert.AreEqual(msg.Properties.GetInt(VALUE_NAME), i);
                        }

                        if (ackMode == AcknowledgementMode.Transactional)
                        {
                            session.Commit();
                        }
                    }
                }
            }
        }
        private static void RunWithOptions(CommandLineOpts opts)
        {
            ITrace logger = new Logger(Logger.ToLogLevel(opts.logLevel));

            Tracer.Trace = logger;

            string ip          = opts.host;
            Uri    providerUri = new Uri(ip);

            Console.WriteLine("scheme: {0}", providerUri.Scheme);

            IConnection conn = null;

            if (opts.topic == null && opts.queue == null)
            {
                Console.WriteLine("ERROR: Must specify a topic or queue destination");
                return;
            }
            try
            {
                NmsConnectionFactory factory = new NmsConnectionFactory(ip);
                if (opts.username != null)
                {
                    factory.UserName = opts.username;
                }
                if (opts.password != null)
                {
                    factory.Password = opts.password;
                }
                if (opts.clientId != null)
                {
                    factory.ClientId = opts.clientId;
                }

                if (opts.connTimeout != default)
                {
                    factory.SendTimeout = opts.connTimeout;
                }

                Console.WriteLine("Creating Connection...");
                conn = factory.CreateConnection();
                conn.ExceptionListener += (logger as Logger).LogException;
                Console.WriteLine("Created Connection.");
                Console.WriteLine("Version: {0}", conn.MetaData);
                Console.WriteLine("Creating Session...");
                ISession ses = conn.CreateSession();
                Console.WriteLine("Session Created.");

                conn.Start();
                IDestination dest = (opts.topic == null) ? (IDestination)ses.GetQueue(opts.queue) : (IDestination)ses.GetTopic(opts.topic);
                Console.WriteLine("Creating Message Producer for : {0}...", dest);
                IMessageProducer prod     = ses.CreateProducer(dest);
                IMessageConsumer consumer = ses.CreateConsumer(dest);
                Console.WriteLine("Created Message Producer.");
                prod.DeliveryMode = opts.mode == 0 ? MsgDeliveryMode.NonPersistent : MsgDeliveryMode.Persistent;
                prod.TimeToLive   = TimeSpan.FromSeconds(20);
                ITextMessage msg = prod.CreateTextMessage("Hello World!");

                Console.WriteLine("Sending Msg: {0}", msg.ToString());
                Console.WriteLine("Starting Connection...");
                conn.Start();
                Console.WriteLine("Connection Started: {0} Resquest Timeout: {1}", conn.IsStarted, conn.RequestTimeout);
                Console.WriteLine("Sending {0} Messages...", opts.NUM_MSG);
                for (int i = 0; i < opts.NUM_MSG; i++)
                {
                    Tracer.InfoFormat("Sending Msg {0}", i + 1);
                    // Text Msg Body
                    msg.Text = "Hello World! n: " + i;
                    prod.Send(msg);
                    msg.ClearBody();
                }

                IMessage rmsg = null;
                for (int i = 0; i < opts.NUM_MSG; i++)
                {
                    Tracer.InfoFormat("Waiting to receive message {0} from consumer.", i);
                    rmsg = consumer.Receive(TimeSpan.FromMilliseconds(opts.connTimeout));
                    if (rmsg == null)
                    {
                        Console.WriteLine("Failed to receive Message in {0}ms.", opts.connTimeout);
                    }
                    else
                    {
                        Console.WriteLine("Received Message with id {0} and contents {1}.", rmsg.NMSMessageId, rmsg.ToString());
                    }
                }
                if (conn.IsStarted)
                {
                    Console.WriteLine("Closing Connection...");
                    conn.Close();
                    Console.WriteLine("Connection Closed.");
                }
            }
            catch (NMSException ne)
            {
                Console.WriteLine("Caught NMSException : {0} \nStack: {1}", ne.Message, ne);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught unexpected exception : {0}", e);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Dispose();
                }
            }
        }
Exemple #5
0
        public void TestSendToTemporaryOnClosedSession()
        {
            const int NUM_MSGS  = 100;
            string    errString = null;
            const int TIMEOUT   = NUM_MSGS * 100;

            try
            {
                using (IConnection connection = GetConnection("c1"))
                    using (ISession tFactory = GetSession("tFactory"))
                        using (IMessageProducer producer = GetProducer("sender"))
                            using (IMessageConsumer consumer = GetConsumer("receiver"))
                            {
                                IDestination    destination = GetDestination("temp");
                                ITextMessage    sendMessage = producer.CreateTextMessage();
                                MessageListener ackCallback = CreateListener(NUM_MSGS);
                                MessageListener callback    = (message) =>
                                {
                                    if (errString == null)
                                    {
                                        if (!destination.Equals(message.NMSReplyTo))
                                        {
                                            errString = string.Format("Received message, id = {0}, has incorrect ReplyTo property.", ExtractMsgId(message.NMSMessageId));
                                            waiter.Set();
                                        }

                                        ackCallback(message);
                                    }
                                };
                                consumer.Listener            += callback;
                                connection.ExceptionListener += DefaultExceptionListener;

                                sendMessage.NMSReplyTo = destination;

                                connection.Start();

                                // close session
                                tFactory.Close();

                                for (int i = 0; i < NUM_MSGS; i++)
                                {
                                    sendMessage.Text = string.Format("Link:{0},count:{1}", "temp", i);
                                    producer.Send(sendMessage);

                                    sendMessage.ClearBody();
                                }

                                if (!waiter.WaitOne(TIMEOUT))
                                {
                                    if (errString == null)
                                    {
                                        Assert.Fail("Timed out waiting messages. Received, {0} of {1} messages in {2}ms.", msgCount, NUM_MSGS, TIMEOUT);
                                    }
                                    else
                                    {
                                        Assert.Fail(errString);
                                    }
                                }

                                Assert.AreEqual(NUM_MSGS, msgCount, "Did not receive expected number of messages.");
                            }
            }
            catch (Exception ex)
            {
                this.PrintTestFailureAndAssert(GetTestMethodName(), "Unexpected exception.", ex);
            }
        }
Exemple #6
0
        private static void RunWithOptions(CommandLineOpts opts)
        {
            ITrace logger = new Logger(Logger.ToLogLevel(opts.logLevel));

            Tracer.Trace = logger;

            string ip          = opts.host;
            Uri    providerUri = new Uri(ip);

            Console.WriteLine("scheme: {0}", providerUri.Scheme);

            StringDictionary properties = new StringDictionary();

            //properties["clientId"] = "guest";
            if (opts.username != null)
            {
                properties["NMS.username"] = opts.username;
            }
            if (opts.password != null)
            {
                properties["nms.password"] = opts.password;
            }
            if (opts.clientId != null)
            {
                properties["NMS.CLIENTID"] = opts.clientId;
            }
            //properties["nms.clientid"] = "myclientid1";
            properties["NMS.sendtimeout"] = opts.connTimeout + "";
            IConnection conn = null;

            if (opts.topic == null && opts.queue == null)
            {
                Console.WriteLine("ERROR: Must specify a topic or queue destination");
                return;
            }
            try
            {
                NMS.AMQP.NMSConnectionFactory providerFactory = new NMS.AMQP.NMSConnectionFactory(providerUri, properties);

                IConnectionFactory factory = providerFactory.ConnectionFactory;


                Console.WriteLine("Creating Connection...");
                conn = factory.CreateConnection();
                conn.ExceptionListener += (logger as Logger).LogException;

                Console.WriteLine("Created Connection.");
                Console.WriteLine("Version: {0}", conn.MetaData);

                Console.WriteLine("Creating Session...");
                ISession ses = conn.CreateSession();
                Console.WriteLine("Session Created.");

                conn.Start();



                IDestination dest = (opts.topic == null) ? (IDestination)ses.GetQueue(opts.queue) : (IDestination)ses.GetTopic(opts.topic);

                Console.WriteLine("Creating Message Producer for : {0}...", dest);
                IMessageProducer prod     = ses.CreateProducer(dest);
                IMessageConsumer consumer = ses.CreateConsumer(dest);
                Console.WriteLine("Created Message Producer.");
                prod.DeliveryMode = opts.mode == 0 ? MsgDeliveryMode.NonPersistent : MsgDeliveryMode.Persistent;
                prod.TimeToLive   = TimeSpan.FromSeconds(20);
                ITextMessage msg = prod.CreateTextMessage("Hello World!");

                //IMapMessage msg = prod.CreateMapMessage();

                //IStreamMessage msg = prod.CreateStreamMessage();

                Console.WriteLine("Sending Msg: {0}", msg.ToString());
                Console.WriteLine("Starting Connection...");
                conn.Start();
                Console.WriteLine("Connection Started: {0} Resquest Timeout: {1}", conn.IsStarted, conn.RequestTimeout);

                Console.WriteLine("Sending {0} Messages...", opts.NUM_MSG);
                //
                for (int i = 0; i < opts.NUM_MSG; i++)
                {
                    Tracer.InfoFormat("Sending Msg {0}", i + 1);
                    // Text Msg Body
                    msg.Text = "Hello World! n: " + i;
                    // Map Msg Body
                    //msg.Body.SetString("mykey", "Hello World! n:" + i);
                    //msg.Body.SetBytes("myBytesKey", new byte[] { 0x65, 0x66, 0x54, (byte)(i & 0xFF) });

                    // Stream  Msg Body
                    //msg.WriteBytes(new byte[] { 0x65, 0x66, 0x54, Convert.ToByte(i & 0xff) });
                    //msg.WriteInt64(1354684651565648484L);
                    //msg.WriteObject("barboo");
                    //msg.Properties["foobar"] = i + "";
                    prod.Send(msg);
                    msg.ClearBody();
                }

                IMessage rmsg = null;

                for (int i = 0; i < opts.NUM_MSG; i++)
                {
                    Tracer.InfoFormat("Waiting to receive message {0} from consumer.", i);
                    rmsg = consumer.Receive(TimeSpan.FromMilliseconds(opts.connTimeout));
                    if (rmsg == null)
                    {
                        Console.WriteLine("Failed to receive Message in {0}ms.", opts.connTimeout);
                    }
                    else
                    {
                        Console.WriteLine("Received Message with id {0} and contents {1}.", rmsg.NMSMessageId, rmsg.ToString());
                    }
                }
                //*/
                if (conn.IsStarted)
                {
                    Console.WriteLine("Closing Connection...");
                    conn.Close();
                    Console.WriteLine("Connection Closed.");
                }
            }
            catch (NMSException ne)
            {
                Console.WriteLine("Caught NMSException : {0} \nStack: {1}", ne.Message, ne);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught unexpected exception : {0}", e);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Dispose();
                }
            }
        }