Exemplo n.º 1
0
        public void RawApiConsumer()
        {
            string host = "localhost";
            int port = 5672;
            org.apache.qpid.client.Client client = new org.apache.qpid.client.Client();
            try
            {
                client.Connect(host, port, "dev-only", "guest", "guest");
                IClientSession session = client.CreateSession(50000);

                //

                session.QueueDeclare("message_queue");
                session.ExchangeBind("message_queue", "amq.direct", "routing_key");
                IClientSession session2 = session;
                lock (session2)
                {
                    IMessageListener listener = new MessageListener(session);
                    session.AttachMessageListener(listener, "message_queue");
                    session.MessageSubscribe("message_queue");
                    Monitor.Wait(session);
                }


                //
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }
Exemplo n.º 2
0
        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 client = new Client();
            Console.WriteLine("Client created");
            client.Connect(host, port, virtualhost, username, password);
            Console.WriteLine("Connection established");

            IClientSession ssn = client.CreateSession(50000);
            Console.WriteLine("Session created");
            ssn.QueueDeclare("queue1", null, null);
            ssn.ExchangeBind("queue1", "amq.direct", "queue1", null);
            IMessage message = new Message();
            message.ApplicationHeaders.Add("price", 0);
            for (int i = 0; i < 100; i++)
            {
                message.ClearData();
                message.AppendData( Encoding.UTF8.GetBytes("test: " + i));
                message.ApplicationHeaders["price"] =  i;
                ssn.MessageTransfer("amq.direct", "queue1", message);               
                Thread.Sleep(1000);
            }

            client.Close();
        }
Exemplo n.º 3
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 --------------------------------------------

                publishMessages(session, "usa.news");
                publishMessages(session, "usa.weather");
                publishMessages(session, "europe.news");
                publishMessages(session, "europe.weather");

                noMoreMessages(session);

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

                connection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }
        public void RawApiConsumer()
        {
            string host = "localhost";
            int    port = 5672;

            org.apache.qpid.client.Client client = new org.apache.qpid.client.Client();
            try
            {
                client.Connect(host, port, "dev-only", "guest", "guest");
                IClientSession session = client.CreateSession(50000);

                //

                session.QueueDeclare("message_queue");
                session.ExchangeBind("message_queue", "amq.direct", "routing_key");
                IClientSession session2 = session;
                lock (session2)
                {
                    IMessageListener listener = new MessageListener(session);
                    session.AttachMessageListener(listener, "message_queue");
                    session.MessageSubscribe("message_queue");
                    Monitor.Wait(session);
                }


                //
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }
Exemplo n.º 5
0
        public void should_raise_exception_in_calling_thread_on_authentification_failure()
        {
            var properties = ConfigHelpers.LoadConfig();

            var client = new Client();
            client.Connect(properties["Host"], Convert.ToInt16(properties["Port"]), properties["VirtualHost"],
                           properties["Username"], "some silly password to make sure the authentification fail");      
        }
Exemplo n.º 6
0
      public void Init()
      {
          var properties = ConfigHelpers.LoadConfig();
          // create a client and connect to the broker
          _client = new Client();
          _client.Connect(properties["Host"], Convert.ToInt16(properties["Port"]), properties["VirtualHost"],
                         properties["Username"], properties["Password"]);           
 
      }
Exemplo n.º 7
0
        public void should_not_block_on_close()
        {
            var properties = ConfigHelpers.LoadConfig();

            var client = new Client();
            client.Connect(properties["Host"], Convert.ToInt16(properties["Port"]), properties["VirtualHost"],
                           properties["Username"], properties["Password"]);
            client.Close();
        }
 /// <summary>
 /// Eagerly creates a new client.
 /// </summary>
 /// <returns></returns>
 public IClient CreateClient()
 {
     IClient client = new org.apache.qpid.client.Client();
     client.Connect(connectionParameters.Host,
                    connectionParameters.Port,
                    connectionParameters.VirtualHostName,
                    connectionParameters.Username,
                    connectionParameters.Password);
     return client;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Eagerly creates a new client.
        /// </summary>
        /// <returns></returns>
        public IClient CreateClient()
        {
            IClient client = new org.apache.qpid.client.Client();

            client.Connect(connectionParameters.Host,
                           connectionParameters.Port,
                           connectionParameters.VirtualHostName,
                           connectionParameters.Username,
                           connectionParameters.Password);
            return(client);
        }
Exemplo n.º 10
0
        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 --------------------------------------------
                
                IMessage message = new Message();

                // The routing key is a message property. We will use the same
                // routing key for each message, so we'll set this property
                // just once. (In most simple cases, there is no need to set
                // other message properties.)

                message.DeliveryProperties.SetRoutingKey("routing_key"); 

                // Asynchronous transfer sends messages as quickly as
                // possible without waiting for confirmation.
                for (int i = 0; i < 10; i++)
                {
                    message.ClearData();
                    message.AppendData(Encoding.UTF8.GetBytes("Message " + i));                  
                    session.MessageTransfer("amq.direct", message);                    
                }

                // And send a syncrhonous final message to indicate termination.
                message.ClearData();
                message.AppendData(Encoding.UTF8.GetBytes("That's all, folks!"));
                session.MessageTransfer("amq.direct", "routing_key", message); 
                session.Sync();

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

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

                // Unlike topic exchanges and direct exchanges, a fanout
                // exchange need not set a routing key. 
                IMessage message = new Message();

                // Asynchronous transfer sends messages as quickly as
                // possible without waiting for confirmation.
                for (int i = 0; i < 10; i++)
                {
                    message.ClearData();
                    message.AppendData(Encoding.UTF8.GetBytes("Message " + i));
                    session.MessageTransfer("amq.fanout", message);
                }

                // And send a syncrhonous final message to indicate termination.
                message.ClearData();
                message.AppendData(Encoding.UTF8.GetBytes("That's all, folks!"));
                session.MessageTransfer("amq.fanout", message);
                session.Sync();

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

                connection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }
Exemplo n.º 13
0
        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 --------------------------------------------
                // Create a request queue for clients to use when making
                // requests.
                const string request_queue = "request";
                // Use the name of the request queue as the routing key
                session.QueueDeclare(request_queue);
                session.ExchangeBind(request_queue, "amq.direct", request_queue);

                lock (session)
                {
                    // Create a listener and subscribe it to the request_queue      
                    IMessageListener listener = new MessageListener(session);
                    session.AttachMessageListener(listener, request_queue);
                    session.MessageSubscribe(request_queue);
                    // Receive messages until all messages are received
                    Console.WriteLine("Waiting for requests");
                    Monitor.Wait(session);
                }

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

                connection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: \n" + e.StackTrace);
            }
        }
Exemplo n.º 14
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 --------------------------------------------
                // Create a queue named "message_queue", and route all messages whose
                // routing key is "routing_key" to this newly created queue.

                session.QueueDeclare("message_queue");
                session.ExchangeBind("message_queue", "amq.direct", "routing_key");
         
                lock (session)
                {
                    // Create a listener and subscribe it to the queue named "message_queue"
                    IMessageListener listener = new MessageListener(session);
                    session.AttachMessageListener(listener, "message_queue");                              
                    session.MessageSubscribe("message_queue");
                    // Receive messages until all messages are received
                    Monitor.Wait(session);
                }

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

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

                lock (session)
                {
                    Console.WriteLine("Listening for messages ...");
                    // Create a listener                    
                    prepareQueue("usa", "usa.#", session);
                    prepareQueue("europe", "europe.#", session);
                    prepareQueue("news", "#.news", session);
                    prepareQueue("weather", "#.weather", session);
                    while (_count > 0)
                    {
                        Monitor.Wait(session);
                    }
                }

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

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