Using Message Queuing in C # RABBITMQ

Source: Internet
Author: User
Tags rabbitmq

1, what is RABBITMQ. See http://www.rabbitmq.com/.

function is to improve the concurrency of the system, some do not need to respond to the client in a timely manner and occupy more resources of the operation, put into the queue, and then by another thread, to deal with these queues asynchronously, can greatly improve the system's concurrency capability.

2. Installation

RABBITMQ Service: http://www.rabbitmq.com/download.html.
(After you install the RABBITMQ service, you will see it in the Windows service.) If you do not have an Erlang runtime environment, you will be alerted to install the Erlang environment during the installation process. Http://www.erlang.org/downloads)

. NET Client class Library: http://www.rabbitmq.com/dotnet.html

3. Plugins

RABBITMQ provides a lot of good plug-ins, the most common is the Web management tools, launch this plugin.

CMD Run command: Rabbitmq-plugins enable Rabbitmq_management

Note: Rabbitmq-plugins is located on the path: D:\Program files\rabbitmq Server\rabbitmq_server-3.4.0\sbin

The address of the Web management tool is: http://localhost:15672, initial user name: Guest initial Password: Guest

4. Configuration

The configuration file address is: C:\Documents and Settings\administrator\application data\rabbitmq\ Rabbitmq.config, there is no Rabbit.config file by default, you need to create it manually (the default is rabbitmq.config.example for reference). Based on security, two configurations were made, as follows:

[{rabbit,[{loopback_users, [<< "Guest" >>]},{tcp_listeners, [{"127.0.0.1", 1234},{"10.121.1.48", 8009}]} ]}].

Loopback_users: Sets the user to access the service only on the same machine as the RABBITMQ service.

Tcp_listeners: Sets the IP address and port of the RABBITMQ listener. Only listen to LAN intranet IP, modify the default port, to prevent intrusion attacks.

When you're done, don't forget the following, otherwise the configuration will not work.

    • Stop RABBITMQ Service;
    • Reinstall the service to make the configuration effective: Rabbitmq-service.bat Install

This command to switch to path: D:\Program files\rabbitmq Server\rabbitmq_server-3.4.0\sbin

    • Start the RABBITMQ service;

5, demo practice.

Message Producer: Class Program {static void Main (string[] args) {try {Connec                Tionfactory factory = new ConnectionFactory (); Factory.                HostName = Constants.mqhost; Factory.                Port = Constants.mqport; Factory.                UserName = Constants.mqusername; Factory.                Password = constants.mqpwd; using (iconnection conn = factory. CreateConnection ()) {using (IModel CHANNEL = conn. Createmodel ()) {//defines a persistent queue on MQ and does not repeat the channel creation if the name is the same.                        Queuedeclare ("Myfirstqueue", True, False, false, NULL);                            while (true) {string customstr = Console.ReadLine ();                            Requestmsg requestmsg = new Requestmsg (); Requestmsg.name = string.                            Format ("name_{0}", CUSTOMSTR); RequestmsG.code = string.                            Format ("code_{0}", CUSTOMSTR);                            String jsonstr = Jsonconvert.serializeobject (requestmsg);                                                        byte[] bytes = Encoding.UTF8.GetBytes (JSONSTR); Set Message Persistence ibasicproperties properties = Channel.                            Createbasicproperties (); Properties.                            DeliveryMode = 2; Channel.                            Basicpublish ("", "Myfirstqueue", properties, bytes); Channel.                            Basicpublish ("", "Myfirstqueue", null, bytes);                        Console.WriteLine ("Message Sent:" + requestmsg.tostring ()); }}}} catch (Exception E1) {CONSOLE.W Riteline (E1.            ToString ());        } console.readline (); }    }

Class Program {static void Main (string[] args) {try {Connectionfa                Ctory factory = new ConnectionFactory (); Factory.                HostName = Constants.mqhost; Factory.                Port = Constants.mqport; Factory.                UserName = Constants.mqusername; Factory.                Password = constants.mqpwd; using (iconnection conn = factory. CreateConnection ()) {using (IModel CHANNEL = conn. Createmodel ()) {//defines a persistent queue on MQ and does not repeat the channel creation if the name is the same.                        Queuedeclare ("Myfirstqueue", True, False, false, NULL); Enter 1, that if a message is received, but no answer is received, the client does not receive the next message channel.                                                Basicqos (0, 1, false);                        Console.WriteLine ("Listening ..."); Define a consumer queueingbasicconsumer consumer = new Queueingbasicconsumer on the queue (ChanneL); Consumption queue, and set the answer mode to actively answer the channel for the program.                        Basicconsume ("Myfirstqueue", false, consumer); while (true) {//block function, gets the message in the queue Basicdeliverev Entargs ea = (basicdelivereventargs) consumer.                            Queue.dequeue (); byte[] bytes = ea.                            Body;                            String str = Encoding.UTF8.GetString (bytes);                            Requestmsg msg = jsonconvert.deserializeobject<requestmsg> (str); Console.WriteLine ("Handlemsg:" + MSG.)                            ToString ()); Reply to confirm channel. Basicack (ea.                        Deliverytag, false); }}}} catch (Exception E1) {CONSOLE.W Riteline (E1.            ToString ());        } console.readline (); }    }

Original from: http://www.cnblogs.com/qy1141/p/4054135.html

Using Message Queuing in C # RABBITMQ

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.