Exemple #1
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();

            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.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
            {
                Apache.NMS.AMQP.NMSConnectionFactory providerFactory = new Apache.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!");

                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 #2
0
 static void Main(string[] args)
 {
     CommandLineOpts opts = new CommandLineOpts();
     ParserResult <CommandLineOpts> result = CommandLine.Parser.Default.ParseArguments <CommandLineOpts>(args)
                                             .WithParsed <CommandLineOpts>(options => RunWithOptions(options));
 }