RABBITMQ Guide (C #) (i) Hello world

Source: Internet
Author: User
Tags rabbitmq

RabbitMQ is a message agent, the basic idea is simple: receive and push messages. You can think of it as a post office, and when you put the letter in your mailbox, you can be very confident that the postman will hand the letter to the recipient . RabbitMQ is a mailbox, post Office and Courier.

rabbitmq is a mailbox, post Office and Courier.

Unlike the Post office,RabbitMQInstead of writing stationery, it is receiving, storing, and forwarding binary message data.

The following are some of the terms in RABBITMQ:

    • Production (producing) is sent. The program that sends the message is the producer (Producer). The producer uses "P" to represent, as

    • The queue is the name of the mailbox in RABBITMQ. Although the message is passed between the RABBITMQ and the application, they can also be stored in the queue. There is no limit to the size of the queue, you can store any number of messages into the queue into the infinite cache. Multiple producers can send messages to the same queue, or multiple consumers can receive messages from the same queue. The queue represents as

    • Consumption (consuming) means to receive. A program waiting to receive a message is called a consumer (Consumer), represented by a "C", such as

Note: Producers, consumers, agents are generally placed on different servers.

Hello World

This part implements two programs, the producer sends a simple message, and then the receiver receives the message and displays it on the screen, sending the message content "Hello World".

As shown, "P" is the producer, "C" is the consumer. The middle box is a queue, used as a message cache for RABBITMQ, for consumer use.

RABBITMQ Client packages can be installed through NuGet

Send

The named message sender is Sende.cs, and the message receiver is Receive.cs. The sender connects RABBITMQ, sends a message, and then exits.

To create a connection in Send.cs, the code is as follows

usingSystem;usingrabbitmq.client;usingSystem.Text;classsend{ Public Static voidMain () {varFactory =NewConnectionFactory () {HostName ="localhost" }; using(varConnection =Factory. CreateConnection ()) {using(varChannel =connection.        Createmodel ()) {...} }    }}

This connection encapsulates the socket connection, provides protocol version development and authentication, and so on. Here we connect the agent on this machine, if you want to connect to another server, just specify the machine name or IP address.

The next step is to create the channel, and most of the APIs are already encapsulated.

In order to send a message, you need to define a queue and then send a message to that queue.

usingSystem;usingrabbitmq.client;usingSystem.Text;classsend{ Public Static voidMain () {varFactory =NewConnectionFactory () {HostName ="localhost" }; using(varConnection =Factory. CreateConnection ())using(varChannel =connection. Createmodel ()) {channel. Queuedeclare (Queue:"Hello", Durable:false, Exclusive:false, Autodelete:false, arguments:NULL); stringMessage ="Hello world!"; varBODY =Encoding.UTF8.GetBytes (message); Channel. Basicpublish (Exchange:"", Routingkey:"Hello", Basicproperties:NULL, Body:body); Console.WriteLine ("[x] Sent {0}", message); } Console.WriteLine ("Press [Enter] to exit.");    Console.ReadLine (); }}

The defined queue is created if it is not saved. The content of the message is a byte array, so you can encode any message you want to send.

The channel and connection are freed when the upper code finishes running.

Receive

The receiver receives a message that RABBITMQ pushes over, so unlike a transmitter that sends only a single message, the receiver needs to listen for messages and display them.

The code in Receive.cs is similar to the sender, establishing a connection and channel, and defining the queue to use. Note that the queue name is the same as the queue in the transmitter.

classreceive{ Public Static voidMain () {varFactory =NewConnectionFactory () {HostName ="localhost" }; using(varConnection =Factory. CreateConnection ()) {using(varChannel =connection. Createmodel ()) {channel. Queuedeclare (Queue:"Hello", Durable:false, Exclusive:false, Autodelete:false, arguments:NULL);        ...            } }    }}

Note Here we define the queue. Since it is possible to start the sink before the transmitter, we need to make sure that the queue we need to use already exists.

We're going to tell the server to push the message from the queue because the message is sent by people, so we need to provide a callback event Eventingbasicconsumer to handle the received message.

usingrabbitmq.client;usingRabbitMQ.Client.Events;usingSystem;usingSystem.Text;classreceive{ Public Static voidMain () {varFactory =NewConnectionFactory () {HostName ="localhost" }; using(varConnection =Factory. CreateConnection ())using(varChannel =connection. Createmodel ()) {channel. Queuedeclare (Queue:"Hello", Durable:false, Exclusive:false, Autodelete:false, arguments:NULL); varConsumer =NewEventingbasicconsumer (channel); Consumer. Received+ = (model, ea) = =            {                varBODY =ea.                Body; varMessage =Encoding.UTF8.GetString (body); Console.WriteLine ("[x] Received {0}", message);            }; Channel. Basicconsume (Queue:"Hello", Noack:true, Consumer:consumer); Console.WriteLine ("Press [Enter] to exit.");        Console.ReadLine (); }    }}

Run

Set up two console project send and receive to run the above code separately.

The receiver receives a message from the sender via RABBITMQ and shows that the receiver is running waiting messages.

If you want to check the queue, you can use the command rabbitmqctl list_queues

RABBITMQ Guide (C #) (i) Hello world

Related Article

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.