Esempio n. 1
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);

                //--------- Main body of program --------------------------------------------
                // Each client creates its own private queue, using the
                // session id to guarantee a unique name. It then routes
                // all messages from the fanout exchange to its own queue
                // by binding to the queue.
                //
                // The binding specifies a binding key, but for a fanout
                // exchange, the binding key is optional and is not used
                // for routing decisions. It can be useful for tracking
                // messages and routing in logs.

                string myQueue = session.Name;
                session.QueueDeclare(myQueue, Option.EXCLUSIVE, Option.AUTO_DELETE);
                session.ExchangeBind(myQueue, "amq.fanout", "my-key");

                lock (session)
                {
                    Console.WriteLine("Listening");
                    // Create a listener and subscribe it to my queue.
                    IMessageListener listener = new MessageListener(session);
                    session.AttachMessageListener(listener, myQueue);
                    session.MessageSubscribe(myQueue);
                    // Receive messages until all messages are received
                    Monitor.Wait(session);
                }

                //---------------------------------------------------------------------------

                connection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }
Esempio 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);

                //--------- Main body of program --------------------------------------------
                // Each client creates its own private queue, using the 
                // session id to guarantee a unique name. It then routes
                // all messages from the fanout exchange to its own queue
                // by binding to the queue.
                //
                // The binding specifies a binding key, but for a fanout
                // exchange, the binding key is optional and is not used
                // for routing decisions. It can be useful for tracking
                // messages and routing in logs.

                string myQueue = session.Name;
                session.QueueDeclare(myQueue, Option.EXCLUSIVE, Option.AUTO_DELETE);
                session.ExchangeBind(myQueue, "amq.fanout", "my-key");

                lock (session)
                {
                    Console.WriteLine("Listening");
                    // Create a listener and subscribe it to my queue.
                    IMessageListener listener = new MessageListener(session);
                    session.AttachMessageListener(listener, myQueue);
                    session.MessageSubscribe(myQueue);
                    // Receive messages until all messages are received
                    Monitor.Wait(session);
                }

                //---------------------------------------------------------------------------

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