2. Sending
First program Send.cs: Send Hello World to queue. As we mentioned in the previous article, the 9th line of your program is to establish a connection, the 12th line is to create a channel, and the 14th line creates a queue named Hello.
1 usingSystem;2 usingrabbitmq.client;3 usingSystem.Text;4 5 classSend6 {7 Public Static voidMain ()8 {9 varFactory =NewConnectionFactory () {HostName ="localhost" };Ten using(varConnection =Factory. CreateConnection ()) One { A using(varChannel =connection. Createmodel ()) - { -Channel. Queuedeclare ("Hello",false,false,false,NULL); the stringMessage ="Hello world!"; - varBODY =Encoding.UTF8.GetBytes (message); -Channel. Basicpublish ("","Hello",NULL, body); -Console.WriteLine ("[x] Sent {0}", message); + } - } + } A}
As you can see from the architecture diagram, producer can only be sent to exchange, and it cannot be sent directly to the queue.
Line 17th: Now we use the default Exchange (the name is a null character). This default Exchange allows us to send to the specified queue. Routing_key is the specified queue name.
3. Receiving
The second program Receive.cs will get a message from the queue and print to the screen.
1 usingrabbitmq.client;2 usingRabbitMQ.Client.Events;3 usingSystem;4 usingSystem.Text;5 6 classReceive7 {8 Public Static voidMain ()9 {Ten varFactory =NewConnectionFactory () {HostName ="localhost" }; One using(varConnection =Factory. CreateConnection ()) A { - using(varChannel =connection. Createmodel ()) - { theChannel. Queuedeclare ( "hello" ,false,false,false,NULL); - varConsumer =New Queueingbasicconsumer(channel); -Channel.Basicconsume("Hello",true, consumer); -Console.WriteLine ("[*] waiting for messages."+"To exit Press CTRL + C"); + while(true) - { + varEA =(Basicdelivereventargs) consumer. Queue.dequeue ();//Blocking A varBODY =ea. Body; at varMessage =Encoding.UTF8.GetString (body); -Console.WriteLine ("[x] Received {0}", message); - } - } - } - } in}
4. Final Run
Run Send.cs First, andSend.cs will stop each time it finishes running. Note: The data is now stored in the queue. After receiving it receive. cs.
Turn:
Http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html (official website)
http://blog.csdn.net/anzhsoft/article/details/19570187 (translation)
RABBITMQ Message Queue (ii): "Hello, World" [Turn]