private static void Main(string[] args)
        {
            string host        = ConfigurationManager.AppSettings["Host"];
            int    port        = int.Parse(ConfigurationManager.AppSettings["Port"]);
            string virtualhost = ConfigurationManager.AppSettings["VirtualHost"];
            string username    = ConfigurationManager.AppSettings["Username"];
            string password    = ConfigurationManager.AppSettings["Password"];

            Client connection = new Client();

            try
            {
                connection.Connect(host, port, virtualhost, username, password);
                IClientSession session = connection.CreateSession(50000);
                IMessage       request = new Message();

                //--------- Main body of program --------------------------------------------
                // Create a response queue so the server can send us responses
                // to our requests. Use the client's session ID as the name
                // of the response queue.
                string response_queue = "client" + session.GetName();
                // Use the name of the response queue as the routing key
                session.QueueDeclare(response_queue);
                session.ExchangeBind(response_queue, "amq.direct", response_queue);

                // Each client sends the name of their own response queue so
                // the service knows where to route messages.
                request.DeliveryProperties.SetRoutingKey("request");
                request.MessageProperties.SetReplyTo(new ReplyTo("amq.direct", response_queue));

                lock (session)
                {
                    // Create a listener for the response queue and listen for response messages.
                    Console.WriteLine("Activating response queue listener for: " + response_queue);
                    IMessageListener listener = new ClientMessageListener(session);
                    session.AttachMessageListener(listener, response_queue);
                    session.MessageSubscribe(response_queue);

                    // Now send some requests ...
                    string[] strs =
                    {
                        "Twas brillig, and the slithy toves",
                        "Did gire and gymble in the wabe.",
                        "All mimsy were the borogroves,",
                        "And the mome raths outgrabe.",
                        "That's all, folks!"
                    };
                    foreach (string s in strs)
                    {
                        request.ClearData();
                        request.AppendData(Encoding.UTF8.GetBytes(s));
                        session.MessageTransfer("amq.direct", request);
                    }
                    Console.WriteLine("Waiting for all responses to arrive ...");
                    Monitor.Wait(session);
                }
                //---------------------------------------------------------------------------

                connection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            string host = ConfigurationManager.AppSettings["Host"];
            int port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            string virtualhost = ConfigurationManager.AppSettings["VirtualHost"];
            string username = ConfigurationManager.AppSettings["Username"];
            string password = ConfigurationManager.AppSettings["Password"];

            Client connection = new Client();
            try
            {
                connection.Connect(host, port, virtualhost, username, password);
                IClientSession session = connection.CreateSession(50000);
                IMessage request = new Message();

                //--------- Main body of program --------------------------------------------
                // Create a response queue so the server can send us responses
                // to our requests. Use the client's session ID as the name
                // of the response queue.
                string response_queue = "client" + session.GetName();
                // Use the name of the response queue as the routing key
                session.QueueDeclare(response_queue);
                session.ExchangeBind(response_queue, "amq.direct", response_queue);

                // Each client sends the name of their own response queue so
                // the service knows where to route messages.
                request.DeliveryProperties.SetRoutingKey("request");
                request.MessageProperties.SetReplyTo(new ReplyTo("amq.direct", response_queue));

                lock (session)
                {
                    // Create a listener for the response queue and listen for response messages.
                    Console.WriteLine("Activating response queue listener for: " + response_queue);
                    IMessageListener listener = new ClientMessageListener(session);
                    session.AttachMessageListener(listener, response_queue);
                    session.MessageSubscribe(response_queue);

                    // Now send some requests ...
                    string[] strs = {
                                        "Twas brillig, and the slithy toves",
                                        "Did gire and gymble in the wabe.",
                                        "All mimsy were the borogroves,",
                                        "And the mome raths outgrabe.",
                                        "That's all, folks!"
                                    };
                    foreach (string s in strs)
                    {
                        request.ClearData();
                        request.AppendData(Encoding.UTF8.GetBytes(s));
                        session.MessageTransfer("amq.direct", request);
                    }
                    Console.WriteLine("Waiting for all responses to arrive ...");
                    Monitor.Wait(session);
                }
                //---------------------------------------------------------------------------

                connection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }