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(); }
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); } }
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); } }
public void setHeaders() { _log.Debug("Running: setHeaders"); IClientSession ssn = Client.CreateSession(0); ssn.QueueDeclare("queue1"); ssn.ExchangeBind("queue1", "amq.direct", "queue1"); ssn.Sync(); CircularBuffer<IMessage> buff = new CircularBuffer<IMessage>(10); SyncListener listener = new SyncListener(ssn, buff); ssn.AttachMessageListener(listener, "queue1"); ssn.MessageSubscribe("queue1"); IMessage message = new org.apache.qpid.client.Message(); message.DeliveryProperties.SetRoutingKey("queue1"); const long someLong = 14444444; message.ApplicationHeaders.Add("someLong", someLong); const int someInt = 14; message.ApplicationHeaders.Add("soneInt", someInt); const float someFloat = 14.001F; message.ApplicationHeaders.Add("soneFloat", someFloat); const double someDouble = 14.5555555; message.ApplicationHeaders.Add("someDouble", someDouble); const byte someByte = 2; message.ApplicationHeaders.Add("someByte", someByte); const string someString = "someString"; message.ApplicationHeaders.Add("someString", someString); const char someChar = 'a'; message.ApplicationHeaders.Add("someChar", someChar); const Boolean someBoolean = true; message.ApplicationHeaders.Add("someBoolean", someBoolean); // transfer the message ssn.MessageTransfer("amq.direct", message); // get the message and check the headers IMessage messageBack = buff.Dequeue(); Assert.IsTrue(((string) messageBack.ApplicationHeaders["someString"]).Equals(someString)); Assert.IsTrue(((char)messageBack.ApplicationHeaders["someChar"]).Equals(someChar)); Assert.IsTrue((long)messageBack.ApplicationHeaders["someLong"] == someLong); Assert.IsTrue((int)messageBack.ApplicationHeaders["soneInt"] == someInt); Assert.IsTrue((double)messageBack.ApplicationHeaders["someDouble"] == someDouble); Assert.IsTrue((byte) messageBack.ApplicationHeaders["someByte"] == someByte); Assert.IsTrue((Boolean)messageBack.ApplicationHeaders["someBoolean"]); // c# has an conversion precision issue with decimal Assert.IsTrue((float) messageBack.ApplicationHeaders["soneFloat"] <= someFloat); float b = (float) messageBack.ApplicationHeaders["soneFloat"]; Assert.IsTrue(Convert.ToInt32(b) == Convert.ToInt32(someFloat)); }
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); } }
public void setHeaders() { _log.Debug("Running: setHeaders"); IClientSession ssn = Client.CreateSession(0); ssn.QueueDeclare("queue1"); ssn.ExchangeBind("queue1", "amq.direct", "queue1"); ssn.Sync(); CircularBuffer <IMessage> buff = new CircularBuffer <IMessage>(10); SyncListener listener = new SyncListener(ssn, buff); ssn.AttachMessageListener(listener, "queue1"); ssn.MessageSubscribe("queue1"); IMessage message = new org.apache.qpid.client.Message(); message.DeliveryProperties.SetRoutingKey("queue1"); const long someLong = 14444444; message.ApplicationHeaders.Add("someLong", someLong); const int someInt = 14; message.ApplicationHeaders.Add("soneInt", someInt); const float someFloat = 14.001F; message.ApplicationHeaders.Add("soneFloat", someFloat); const double someDouble = 14.5555555; message.ApplicationHeaders.Add("someDouble", someDouble); const byte someByte = 2; message.ApplicationHeaders.Add("someByte", someByte); const string someString = "someString"; message.ApplicationHeaders.Add("someString", someString); const char someChar = 'a'; message.ApplicationHeaders.Add("someChar", someChar); const Boolean someBoolean = true; message.ApplicationHeaders.Add("someBoolean", someBoolean); // transfer the message ssn.MessageTransfer("amq.direct", message); // get the message and check the headers IMessage messageBack = buff.Dequeue(); Assert.IsTrue(((string)messageBack.ApplicationHeaders["someString"]).Equals(someString)); Assert.IsTrue(((char)messageBack.ApplicationHeaders["someChar"]).Equals(someChar)); Assert.IsTrue((long)messageBack.ApplicationHeaders["someLong"] == someLong); Assert.IsTrue((int)messageBack.ApplicationHeaders["soneInt"] == someInt); Assert.IsTrue((double)messageBack.ApplicationHeaders["someDouble"] == someDouble); Assert.IsTrue((byte)messageBack.ApplicationHeaders["someByte"] == someByte); Assert.IsTrue((Boolean)messageBack.ApplicationHeaders["someBoolean"]); // c# has an conversion precision issue with decimal Assert.IsTrue((float)messageBack.ApplicationHeaders["soneFloat"] <= someFloat); float b = (float)messageBack.ApplicationHeaders["soneFloat"]; Assert.IsTrue(Convert.ToInt32(b) == Convert.ToInt32(someFloat)); }
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); } }
private void send(int size, string queue, string data) { IMessage message = new Message(); message.DeliveryProperties.SetRoutingKey(queue); message.AppendData(Encoding.UTF8.GetBytes(data)); for (int i = 0; i < size; ++i) { Session.MessageTransfer("amq.direct", message); } }
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(); }
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(); }
public void Send(Message msg) { lock (lockObject) { log.Debug(String.Format("Sending message to routing key '{0}'", msg.DeliveryProperties.GetRoutingKey())) ; //log.Debug(System.Text.Encoding.UTF8.GetString(msg.Body.ToArray())) ; outSession.MessageTransfer("qpid.management", msg) ; //clientSession.sync() ; } }
public Message CreateMessage(IEncoder enc, string routingKey, long ttl) { Message msg = new Message() ; msg.Body = ((MSEncoder)enc).Segment() ; msg.DeliveryProperties.SetRoutingKey(routingKey) ; if (-1 != ttl) { msg.DeliveryProperties.SetTtl(ttl) ; } msg.MessageProperties.SetContentType("x-application/qmf") ; msg.MessageProperties.SetReplyTo(new ReplyTo("amq.direct", replyName)) ; return msg ; }
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(); }
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); } } }