示例#1
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);
            }
        }
示例#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();
        }
示例#3
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);
            }
        }
示例#4
0
 private static void publishMessages(IClientSession session, string 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.topic", routing_key, message);
     }
 }
        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);
            }
        }
示例#6
0
        public override void Start()
        {
            byte[] data = new byte[Options.Size];
            // randomly populate data 
            Random r = new Random(34);
            r.NextBytes(data);
            IMessage message = new Message();
            message.AppendData(data);

            message.DeliveryProperties.SetRoutingKey(_key);

            if (Options.Durable)
                message.DeliveryProperties.SetDeliveryMode(MessageDeliveryMode.PERSISTENT);

            if (Options.Tx > 0)
            {
                Session.TxSelect();
                Session.Sync();
            }

            CircularBuffer<IMessage> buffer = new CircularBuffer<IMessage>(100);
            // Create a listener and subscribe it to the queue named "pub_start"          
            IMessageListener listener = new SyncListener(buffer);
            string localQueue = "localQueue-" + UUID.RandomUuid().ToString();
            Session.QueueDeclare(localQueue, null, null, Option.AUTO_DELETE);
            Session.ExchangeBind(localQueue, "amq.direct", "pub_start");
            Session.AttachMessageListener(listener, localQueue);
            Session.MessageSubscribe(localQueue);
            if (Options.Tx > 0)
            {
                Session.TxCommit();
                Session.Sync();
            }
            buffer.Dequeue();

            for (int j = 0; j < Options.Iterations; ++j)
            {
                DateTime start = DateTime.Now;                
                for (long i = 0; i < Options.Count; ++i)
                {
                    Session.MessageTransfer(_exchange, message);

                    if (Options.SyncPub)
                    {
                        Session.Sync();
                    }
                    if (Options.Tx > 0 && (i + 1)%Options.Tx == 0)
                    {
                        Session.TxSelect();
                        Session.Sync();
                    }
                    if (Options.IntervalPub > 0)
                    {
                        Thread.Sleep((int) Options.IntervalSub*1000);
                    }
                }
                Session.Sync();
                DateTime end = DateTime.Now;

                // Report to publisher.
                message.DeliveryProperties.SetRoutingKey("pub_done");
                message.ClearData();
                double time = end.Subtract(start).TotalMilliseconds;
                byte[] rate = BitConverter.GetBytes( Options.Count / time );
                message.AppendData(rate);
                Session.MessageTransfer("amq.direct", message);
                if (Options.Tx > 0)
                {
                    Session.TxSelect();
                    Session.Sync();
                }
            }
            Session.Close();
        }
示例#7
0
        public override void Start()
        {
            if (Options.Tx > 0)
            {
                Session.TxSelect();
                Session.Sync();
            }
            CircularBuffer<IMessage> buffer = new CircularBuffer<IMessage>(100);
            // Create a listener and subscribe it to the queue named "message_queue"
            IMessageListener listener = new SyncListener(buffer);

            string dest = "dest" + UUID.RandomUuid();
            Session.AttachMessageListener(listener, dest);
            Session.MessageSubscribe(_queue, dest,
                                     Options.Tx > 0 || Options.SubAck > 0
                                         ? MessageAcceptMode.EXPLICIT
                                         : MessageAcceptMode.NONE,
                                     MessageAcquireMode.PRE_ACQUIRED, null, 0, null);
            // issue credits     
            Session.MessageSetFlowMode(dest, MessageFlowMode.WINDOW);
            Session.MessageFlow(dest, MessageCreditUnit.BYTE, ClientSession.MESSAGE_FLOW_MAX_BYTES);

            // Notify controller we are ready.
            IMessage message = new Message();
            message.DeliveryProperties.SetRoutingKey("sub_ready");

            message.AppendData(Encoding.UTF8.GetBytes("ready"));
            Session.MessageTransfer("amq.direct", message);

            if (Options.Tx > 0)
            {
                Session.TxCommit();
                Session.Sync();
            }


            for (int j = 0; j < Options.Iterations; ++j)
            {
               
                //need to allocate some more credit
                Session.MessageFlow(dest, MessageCreditUnit.MESSAGE, (long)Options.SubQuota);
                
                RangeSet range = new RangeSet();
                IMessage msg;
                DateTime start = DateTime.Now;
                for (long i = 0; i < Options.SubQuota; ++i)
                {                   
                    msg = buffer.Dequeue();
                    if (Options.Tx > 0 && ((i + 1)%Options.Tx == 0))
                    {
                        Session.TxCommit();
                        Session.Sync();
                    }
                    if (Options.IntervalSub > 0)
                    {
                        Thread.Sleep((int) Options.IntervalSub*1000);
                    }
                    range.Add(msg.Id);
                }
                if (Options.Tx > 0 || Options.SubAck > 0)
                    Session.MessageAccept(range);
                range.Clear();
                if (Options.Tx > 0)
                {
                    Session.TxSelect();
                    Session.Sync();
                }
                DateTime end = DateTime.Now;

                // Report to publisher.
                message.DeliveryProperties.SetRoutingKey("sub_done");
                message.ClearData();
                message.AppendData(BitConverter.GetBytes(Options.SubQuota / end.Subtract(start).TotalMilliseconds ));
                Session.MessageTransfer("amq.direct", message);
                if (Options.Tx > 0)
                {
                    Session.TxSelect();
                    Session.Sync();
                }
            }
            Session.Close();
        }
示例#8
0
 private static void noMoreMessages(IClientSession session)
 {
     IMessage message = new Message();
     // And send a syncrhonous final message to indicate termination.
     message.ClearData();
     message.AppendData(Encoding.UTF8.GetBytes("That's all, folks!"));
     session.MessageTransfer("amq.topic", "control", message);
     session.Sync();
 }
示例#9
0
        public void MessageTransfer(IMessage request)
        {
            IMessage response = new Message();

            // Get routing key for response from the request's replyTo property
            string routingKey;
            if( request.MessageProperties.HasReplyTo() )
            {
                routingKey = request.MessageProperties.GetReplyTo().GetRoutingKey();
            }
            else
            {
                Console.WriteLine("Error: \n No routing key for request " + request);
                return;
            }

            BinaryReader reader = new BinaryReader(request.Body, Encoding.UTF8);
            byte[] body = new byte[request.Body.Length - request.Body.Position];
            reader.Read(body, 0, body.Length);
            ASCIIEncoding enc = new ASCIIEncoding();
            string message = enc.GetString(body);
            Console.WriteLine("Request: " + message);
            
            // Transform message content to upper case
            string responseBody = message.ToUpper();

            // Send it back to the user
            response.ClearData();
            response.AppendData(Encoding.UTF8.GetBytes(responseBody));
            _session.MessageTransfer("amq.direct", routingKey, response);

            // Add this message to the list of message to be acknowledged 
            _range.Add(request.Id);
            if (message.Equals("That's all, folks!"))
            {
                // Acknowledge all the received messages 
                _session.MessageAccept(_range);
                lock (_session)
                {
                    Monitor.Pulse(_session);
                }
            }
        }