RABBITMQ Message Queue (ii): "Hello, World" [Turn]

Source: Internet
Author: User
Tags rabbitmq

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.